iText使用入门 生成HelloWorld PDF详解
先看例子的最简单的版本,后面再详细解释
1 第一行 new Document()的源代码
可用的页面大小在PageSize里面定义,比如如下代码片段
默认的版面都是竖版的,如果你要横版,可以使用Rectangle的roate方法,比如
2 第二行PdfWriter
有多种写入类,比如
PdfWriter
产生pdf格式的文档
RtfWriter
产生RTF格式的文档
HtmlWriter
产生Html格式的文档(一开始是用于调试目的)
3 打开文档 document.open();
在打开文档前,我们需要设置好各种参数,比如你要进行加密
这个加密算法需要额外的包,到这里下载: http://www.bouncycastle.org/latest_releases.html
加密后的打开效果如下:

还可以设置标题等信息,比如
生成后,在File菜单的属性下面可以查看设置的内容

4 增加内容的部分后面会详细介绍,这里就不讲了
5 关闭文档
刷新并关闭文档,数据被全部输出。这一步是必需的。
完整的代码如下:
最后的效果

中文需要 iTextAsian.jar 从这里下载:
http://jaist.dl.sourceforge.net/sourceforge/itext/iTextAsian.jar
- package com.laozizhu.lowagie.itext;
-
- import java.io.FileOutputStream;
-
- import com.lowagie.text.Document;
- import com.lowagie.text.DocumentException;
- import com.lowagie.text.Paragraph;
- import com.lowagie.text.pdf.PdfWriter;
-
- /**
- * iText学习笔记:入门的HelloWorld
- *
- * @author 老紫竹(laozizhu.com)
- */
- public class HelloWorld {
-
- public static void main(String[] args) throws Exception, DocumentException {
- // 新建一个文档,默认是A4纸的大小,4个边框为36
- Document document = new Document();
-
- // 将文档输出,我们写到文件里面
- PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf"));
-
- // 打开文档
- document.open();
-
- // 写入数据
- document.add(new Paragraph("Hello World"));
-
- // 关闭文档
- document.close();
- }
- }
-
1 第一行 new Document()的源代码
- public Document() {
- this(PageSize.A4);
- }
- public Document(Rectangle pageSize) {
- this(pageSize, 36, 36, 36, 36);
- }
- /** This is the a0 format */
- public static final Rectangle A0 = new RectangleReadOnly(2384,3370);
- /** This is the a1 format */
- public static final Rectangle A1 = new RectangleReadOnly(1684,2384);
- /** This is the a2 format */
- public static final Rectangle A2 = new RectangleReadOnly(1191,1684);
- /** This is the a3 format */
- public static final Rectangle A3 = new RectangleReadOnly(842,1191);
- Document document = new Document(PageSize.A4.rotate());
2 第二行PdfWriter
有多种写入类,比如
PdfWriter
产生pdf格式的文档
RtfWriter
产生RTF格式的文档
HtmlWriter
产生Html格式的文档(一开始是用于调试目的)
3 打开文档 document.open();
在打开文档前,我们需要设置好各种参数,比如你要进行加密
- PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf"));
- writer.setEncryption("Hello".getBytes(), "World".getBytes(), PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
加密后的打开效果如下:
还可以设置标题等信息,比如
- document.addTitle("Hello World example");
- document.addAuthor("老紫竹");
- document.addSubject("This example explains how to add metadata.");
- document.addKeywords("iText, Hello World, step 3, metadata");
- document.addCreator("My program using iText");
生成后,在File菜单的属性下面可以查看设置的内容
4 增加内容的部分后面会详细介绍,这里就不讲了
5 关闭文档
刷新并关闭文档,数据被全部输出。这一步是必需的。
完整的代码如下:
- package com.laozizhu.lowagie.itext;
- import java.io.FileOutputStream;
- import com.lowagie.text.Document;
- import com.lowagie.text.DocumentException;
- import com.lowagie.text.Font;
- import com.lowagie.text.Paragraph;
- import com.lowagie.text.pdf.BaseFont;
- import com.lowagie.text.pdf.PdfWriter;
- /**
- * iText学习笔记:HelloWorld
- *
- * @author 老紫竹(laozizhu.com)
- */
- public class HelloWorld {
- public static void main(String[] args) throws Exception, DocumentException {
- // 新建一个文档,默认是A4纸的大小,4个边框为36
- Document document = new Document();
- // 将文档输出,我们写到文件里面
- PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf"));
- writer.setEncryption("Hello".getBytes(), "World".getBytes(), PdfWriter.ALLOW_COPY
- | PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
- document.addTitle("Hello World example");
- document.addAuthor("老紫竹");
- document.addSubject("This example explains how to add metadata.");
- document.addKeywords("iText, Hello World, step 3, metadata");
- document.addCreator("My program using iText");
- // 打开文档
- document.open();
- // 写入数据
- document.add(new Paragraph("Hello World"));
- BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
- Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
- document.add(new Paragraph("老紫竹祝大家新年好!", FontChinese));
- // 关闭文档
- document.close();
- }
- }
最后的效果
中文需要 iTextAsian.jar 从这里下载:
http://jaist.dl.sourceforge.net/sourceforge/itext/iTextAsian.jar
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.****.net/jiangjunshow