利用iText寫PDF

(原貼地址)http://www.javaresearch.org/article/31218.htm 

 

最近由於項目需要,開始使用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. }



寫iText需要iText包 下載地址爲http://prdownloads.sourceforge.net/itext/itext-1.3.jar
如果要加入中文,請再下載一個包,地址爲:http://itext.sourceforge.net/downloads/iTextAsian.jar

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章