`
anson_xu
  • 浏览: 502960 次
  • 性别: Icon_minigender_1
  • 来自: 惠州
社区版块
存档分类

利用iText写PDF心得

    博客分类:
  • java
阅读更多
<!-- end of article title -->
利用iText写PDF心得
 
<!--start of article content -->

最近由于项目需要,开始使用iText写PDF文件,从网上搜索到一些信息,但都是零碎的一些,现在稍微整理一下,仅限于写pdf文件部分。

首先创建一个pdfWriter的模板
  1. /*
  2.  * Created on 2005-7-1
  3.  *
  4.  * TODO To change the template for this generated file go to
  5.  * Window - Preferences - Java - Code Style - Code Templates
  6.  */
  7. package javax.print.PDF;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import com.lowagie.text.Cell;
  12. import com.lowagie.text.Document;
  13. import com.lowagie.text.DocumentException;
  14. import com.lowagie.text.Paragraph;
  15. import com.lowagie.text.Rectangle;
  16. import com.lowagie.text.Table;
  17. import com.lowagie.text.pdf.PdfWriter;
  18. /**
  19.  * @author jcoder
  20.  * 
  21.  * TODO To change the template for this generated type comment go to Window -
  22.  * Preferences - Java - Code Style - Code Templates
  23.  */
  24. abstract public class PDFWriter {
  25.     protected Document document = null;
  26.     protected FileOutputStream out = null;
  27.     protected Rectangle pageSize = null;
  28.     protected String filePath = null;
  29.     protected Cell cell = null;
  30.     protected Paragraph header = null;
  31.     protected Paragraph prg = null;
  32.     protected Table table = null;
  33.     public PDFWriter(String filePath) {
  34.         try {
  35.             this.filePath = filePath;
  36.             document = new Document();
  37.             out = new FileOutputStream(filePath);
  38.             PdfWriter.getInstance(document, out);
  39.             document.open();
  40.         } catch (FileNotFoundException e) {
  41.             // TODO Auto-generated catch block
  42.             e.printStackTrace();
  43.         } catch (DocumentException e) {
  44.             // TODO Auto-generated catch block
  45.             e.printStackTrace();
  46.         }
  47.     }
  48.     public void close() {
  49.         try {
  50.             document.close();
  51.             out.close();
  52.         } catch (IOException e) {
  53.             // TODO Auto-generated catch block
  54.             e.printStackTrace();
  55.         }
  56.     }
  57. }

由于我需要在pdf中创建表格,要使用到com.lowagie.text.Cell,com.lowagie.text.Paragraph, com.lowagie.text.Table,com.lowagie.text.Cell,
com.lowagie.text.Chunk,com.lowagie.text.Font等类,cell为表格中的每个单元格的内容,paragraph为段落内容,cell的构造函数有很多,这里不一一列举了,因为我要用到中文字符,所以特别使用了cell(Element e)这个构造函数,Element为一个接口,实现此接口的类有很多,包含chunk,meta等,表明cell里可以添加很多不同的内容,可以实现自己的定制,chunk的构造函数为Chunk(String content,Font f),在这里我定制了自己的cell,代码如下:

  1. /*
  2.  * Created on 2005-7-1
  3.  *
  4.  * TODO To change the template for this generated file go to
  5.  * Window - Preferences - Java - Code Style - Code Templates
  6.  */
  7. package javax.print.PDF;
  8. import com.lowagie.text.BadElementException;
  9. import com.lowagie.text.Cell;
  10. import com.lowagie.text.Chunk;
  11. import com.lowagie.text.Font;
  12. /**
  13.  * @author jcoder
  14.  * 
  15.  * TODO To change the template for this generated type comment go to Window -
  16.  * Preferences - Java - Code Style - Code Templates
  17.  */
  18. public class PDFCell extends Cell {
  19.     public PDFCell(String content, int rowspan, int colspan)
  20.             throws BadElementException {
  21.         super(new Chunk(content, PDFChineseFont
  22.                 .createChineseFont(10, Font.NORMAL)));
  23.         setRowspan(rowspan);
  24.         setColspan(colspan);
  25.         setHeader(false);
  26.     }
  27. }

稍许解释一下,rowspan和colspan为Cell的两个属性,写过网页的朋友都知道,表格中的行和列有的时候有必要进行合并,这里就实现了这个功能。

Paragraph类我也进行了封装:
  1. /*
  2.  * Created on 2005-7-5
  3.  *
  4.  * TODO To change the template for this generated file go to
  5.  * Window - Preferences - Java - Code Style - Code Templates
  6.  */
  7. package javax.print.PDF;
  8. import com.lowagie.text.Element;
  9. import com.lowagie.text.Font;
  10. import com.lowagie.text.Paragraph;
  11. /**
  12.  * @author Administrator
  13.  * 
  14.  * TODO To change the template for this generated type comment go to Window -
  15.  * Preferences - Java - Code Style - Code Templates
  16.  */
  17. public class PDFParagragh extends Paragraph {
  18.     public PDFParagragh(String content, int alignment, int fontSize) {
  19.         super(content, PDFChineseFont.createChineseFont(fontSize, Font.NORMAL));
  20.         setAlignment(alignment);
  21.     }
  22.     public static final int CENTER = Element.ALIGN_CENTER;
  23.     public static final int LEFT = Element.ALIGN_LEFT;
  24.     public static final int RIGHT = Element.ALIGN_RIGHT;
  25.     public static final int TOP = Element.ALIGN_TOP;
  26.     public static final int MIDDLE = Element.ALIGN_MIDDLE;
  27.     public static final int BOTTOM = Element.ALIGN_BOTTOM;
  28. }


从以上两个代码段中可以看到PDFChineseFont.createChineseFont(int fontSize,int fontStyle)函数 这个也进行了封装,为自定义函数:

  1. /*
  2.  * Created on 2005-7-1
  3.  *
  4.  * TODO To change the template for this generated file go to
  5.  * Window - Preferences - Java - Code Style - Code Templates
  6.  */
  7. package javax.print.PDF;
  8. import java.io.IOException;
  9. import com.lowagie.text.DocumentException;
  10. import com.lowagie.text.Font;
  11. import com.lowagie.text.pdf.BaseFont;
  12. /**
  13.  * @author jcoder
  14.  * 
  15.  * TODO To change the template for this generated type comment go to Window -
  16.  * Preferences - Java - Code Style - Code Templates
  17.  */
  18. public class PDFChineseFont {
  19.     private static Font chineseFont;
  20.     public final static Font createChineseFont(int size, int style) {
  21.         try {
  22.             chineseFont = new Font(BaseFont.createFont("STSong-Light",
  23.                     "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), size, style);
  24.         } catch (DocumentException e) {
  25.             // TODO Auto-generated catch block
  26.             e.printStackTrace();
  27.         } catch (IOException e) {
  28.             // TODO Auto-generated catch block
  29.             e.printStackTrace();
  30.         }
  31.         return chineseFont;
  32.     }
  33. }
如果无此函数定义,生成的pdf文件中的中文字符将不显示。

最后实现自己定制好的pdf文档格式

  1. /*
  2.  * Created on 2005-7-1
  3.  *
  4.  * TODO To change the template for this generated file go to
  5.  * Window - Preferences - Java - Code Style - Code Templates
  6.  */
  7. package javax.print.PDF;
  8. import com.lowagie.text.BadElementException;
  9. import com.lowagie.text.DocumentException;
  10. import com.lowagie.text.Table;
  11. /**
  12.  * @author jcoder
  13.  * 
  14.  * TODO To change the template for this generated type comment go to Window -
  15.  * Preferences - Java - Code Style - Code Templates
  16.  */
  17. public class MyWriter extends PDFWriter {
  18.     public MyWriter(String path) {
  19.         super(path);
  20.         try {
  21.             header = new PDFParagraph("仪器设备调拨单");
  22.             document.add(header);
  23.             table = new Table(14);
  24. table.setBorderWidth(0);
  25.             table.addCell(new PDFCell("(单价:500元以上含500元)", 1, 5));
  26.             table.addCell(new PDFCell("2005年7月1号", 1, 9));
  27.             document.add(table);
  28.             table = new Table(14);
  29. table.setBorderWidth(1);
  30.             table.addCell(new PDFCell("设备编号", 1, 2));
  31.             table.addCell(new PDFCell("设备名称", 1, 3));
  32.             table.addCell(new PDFCell("型号规格", 1, 2));
  33.             table.addCell(new PDFCell("数量", 1, 1));
  34.             table.addCell(new PDFCell("单价", 1, 1));
  35.             table.addCell(new PDFCell("总价", 1, 1));
  36.             table.addCell(new PDFCell("附件", 1, 2));
  37.             table.addCell(new PDFCell("备注", 1, 2));
  38.             table.endHeaders();//换行
  39.             table.addCell(new PDFCell("0126242245", 1, 2));
  40.             table.addCell(new PDFCell("IBM大型机", 1, 3));
  41.             table.addCell(new PDFCell("5465-445GH", 1, 2));
  42.             table.addCell(new PDFCell("3", 1, 1));
  43.             table.addCell(new PDFCell("299,000", 1, 1));
  44.             table.addCell(new PDFCell("2,230,200", 1, 1));
  45.             table.addCell(new PDFCell("无", 1, 2));
  46.             table.addCell(new PDFCell("软件学院买入", 1, 2));
  47.             table.endHeaders();
  48.             table.addCell(new PDFCell("调出单位意见:", 1, 11));
  49.             table.addCell(new PDFCell("院(系)签章", 1, 3));
  50.             table.endHeaders();
  51.             table.addCell(new PDFCell("申请调入单位意见:", 1, 11));
  52.             table.addCell(new PDFCell("院(系)签章", 1, 3));
  53.             table.endHeaders();
  54.             table.addCell(new PDFCell("设备管理科审批:", 1, 5));
  55.             table.addCell(new PDFCell("实验室与设备管理处审批", 1, 4));
  56.             table.addCell(new PDFCell("校部审批:", 1, 5));
  57.             table.endHeaders();
  58.             document.add(table);
  59.             close();//别忘记关闭
  60.         } catch (BadElementException e) {
  61.             // TODO Auto-generated catch block
  62.             e.printStackTrace();
  63.         } catch (DocumentException e) {
  64.             // TODO Auto-generated catch block
  65.             e.printStackTrace();
  66.         }
  67.     }
  68. }


测试类:
  1. /*
  2.  * Created on 2005-7-1
  3.  *
  4.  * TODO To change the template for this generated file go to
  5.  * Window - Preferences - Java - Code Style - Code Templates
  6.  */
  7. package javax.print.PDF;
  8. /**
  9.  * @author jcoder
  10.  * 
  11.  * TODO To change the template for this generated type comment go to Window -
  12.  * Preferences - Java - Code Style - Code Templates
  13.  */
  14. public class Test {
  15.     public static void main(String[] args) {
  16.         PDFWriter pdf = new MyWriter("mine.pdf");
  17.     }
  18. }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics