iText使用入门,生成HelloWorld.PDF详解

来源:互联网 发布:蜘蛛池源码下载 编辑:程序博客网 时间:2024/04/30 03:22
先看例子的最简单的版本,后面再详细解释
  1. package com.laozizhu.lowagie.itext;

  2. import java.io.FileOutputStream;

  3. import com.lowagie.text.Document;
  4. import com.lowagie.text.DocumentException;
  5. import com.lowagie.text.Paragraph;
  6. import com.lowagie.text.pdf.PdfWriter;

  7. /**
  8.  * iText学习笔记:入门的HelloWorld
  9.  * 
  10.  * @author 老紫竹(laozizhu.com)
  11.  */
  12. public class HelloWorld {

  13.   public static void main(String[] args) throws Exception, DocumentException {
  14.     // 新建一个文档,默认是A4纸的大小,4个边框为36
  15.     Document document = new Document();

  16.     // 将文档输出,我们写到文件里面
  17.     PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf"));

  18.     // 打开文档
  19.     document.open();

  20.     // 写入数据
  21.     document.add(new Paragraph("Hello World"));

  22.     // 关闭文档
  23.     document.close();
  24.   }
  25. }


1 第一行 new Document()的源代码
  1.     public Document() {
  2.         this(PageSize.A4);
  3.     }
  1.     public Document(Rectangle pageSize) {
  2.         this(pageSize, 36363636);
  3.     }
可用的页面大小在PageSize里面定义,比如如下代码片段
  1.     /** This is the a0 format */
  2.     public static final Rectangle A0 = new RectangleReadOnly(2384,3370);
  3.     
  4.     /** This is the a1 format */
  5.     public static final Rectangle A1 = new RectangleReadOnly(1684,2384);
  6.     
  7.     /** This is the a2 format */
  8.     public static final Rectangle A2 = new RectangleReadOnly(1191,1684);
  9.     
  10.     /** This is the a3 format */
  11.     public static final Rectangle A3 = new RectangleReadOnly(842,1191);
  12.     
默认的版面都是竖版的,如果你要横版,可以使用Rectangle的roate方法,比如
  1. Document document = new Document(PageSize.A4.rotate());


2 第二行PdfWriter

有多种写入类,比如
PdfWriter
  产生pdf格式的文档

RtfWriter
  产生RTF格式的文档

HtmlWriter
  产生Html格式的文档(一开始是用于调试目的)


3 打开文档 document.open();

在打开文档前,我们需要设置好各种参数,比如你要进行加密
  1. PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf"));
  2. writer.setEncryption("Hello".getBytes(), "World".getBytes(), PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
这个加密算法需要额外的包,到这里下载: http://www.bouncycastle.org/latest_releases.html
加密后的打开效果如下:


还可以设置标题等信息,比如
  1.     document.addTitle("Hello World example");
  2.     document.addAuthor("老紫竹");
  3.     document.addSubject("This example explains how to add metadata.");
  4.     document.addKeywords("iText, Hello World, step 3, metadata");
  5.     document.addCreator("My program using iText");


生成后,在File菜单的属性下面可以查看设置的内容




4 增加内容的部分后面会详细介绍,这里就不讲了

5 关闭文档
刷新并关闭文档,数据被全部输出。这一步是必需的。


完整的代码如下:
  1. package com.laozizhu.lowagie.itext;
  2. import java.io.FileOutputStream;
  3. import com.lowagie.text.Document;
  4. import com.lowagie.text.DocumentException;
  5. import com.lowagie.text.Font;
  6. import com.lowagie.text.Paragraph;
  7. import com.lowagie.text.pdf.BaseFont;
  8. import com.lowagie.text.pdf.PdfWriter;
  9. /**
  10.  * iText学习笔记:HelloWorld
  11.  * 
  12.  * @author 老紫竹(laozizhu.com)
  13.  */
  14. public class HelloWorld {
  15.   public static void main(String[] args) throws Exception, DocumentException {
  16.     // 新建一个文档,默认是A4纸的大小,4个边框为36
  17.     Document document = new Document();
  18.     // 将文档输出,我们写到文件里面
  19.     PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf"));
  20.     writer.setEncryption("Hello".getBytes(), "World".getBytes(), PdfWriter.ALLOW_COPY
  21.         | PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
  22.     document.addTitle("Hello World example");
  23.     document.addAuthor("老紫竹");
  24.     document.addSubject("This example explains how to add metadata.");
  25.     document.addKeywords("iText, Hello World, step 3, metadata");
  26.     document.addCreator("My program using iText");
  27.     // 打开文档
  28.     document.open();
  29.     // 写入数据
  30.     document.add(new Paragraph("Hello World"));
  31.     BaseFont bfChinese = BaseFont.createFont("STSong-Light""UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
  32.     Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
  33.     document.add(new Paragraph("老紫竹祝大家新年好!", FontChinese));
  34.     // 关闭文档
  35.     document.close();
  36.   }
  37. }


最后的效果



中文需要 iTextAsian.jar 从这里下载:
http://jaist.dl.sourceforge.net/sourceforge/itext/iTextAsian.jar

原创粉丝点击