(原貼地址)http://www.javaresearch.org/article/31218.htm
最近由於項目需要,開始使用iText寫PDF文件,從網上搜索到一些信息,但都是零碎的一些,現在稍微整理一下,僅限於寫pdf文件部分。
首先創建一個pdfWriter的模板
- /*
- * Created on 2005-7-1
- *
- * TODO To change the template for this generated file go to
- * Window - Preferences - Java - Code Style - Code Templates
- */
- package javax.print.PDF;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import com.lowagie.text.Cell;
- import com.lowagie.text.Document;
- import com.lowagie.text.DocumentException;
- import com.lowagie.text.Paragraph;
- import com.lowagie.text.Rectangle;
- import com.lowagie.text.Table;
- import com.lowagie.text.pdf.PdfWriter;
- /**
- * @author jcoder
- *
- * TODO To change the template for this generated type comment go to Window -
- * Preferences - Java - Code Style - Code Templates
- */
- abstract public class PDFWriter {
- protected Document document = null;
- protected FileOutputStream out = null;
- protected Rectangle pageSize = null;
- protected String filePath = null;
- protected Cell cell = null;
- protected Paragraph header = null;
- protected Paragraph prg = null;
- protected Table table = null;
- public PDFWriter(String filePath) {
- try {
- this.filePath = filePath;
- document = new Document();
- out = new FileOutputStream(filePath);
- PdfWriter.getInstance(document, out);
- document.open();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (DocumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void close() {
- try {
- document.close();
- out.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
由於我需要在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,代碼如下:
- /*
- * Created on 2005-7-1
- *
- * TODO To change the template for this generated file go to
- * Window - Preferences - Java - Code Style - Code Templates
- */
- package javax.print.PDF;
- import com.lowagie.text.BadElementException;
- import com.lowagie.text.Cell;
- import com.lowagie.text.Chunk;
- import com.lowagie.text.Font;
- /**
- * @author jcoder
- *
- * TODO To change the template for this generated type comment go to Window -
- * Preferences - Java - Code Style - Code Templates
- */
- public class PDFCell extends Cell {
- public PDFCell(String content, int rowspan, int colspan)
- throws BadElementException {
- super(new Chunk(content, PDFChineseFont
- .createChineseFont(10, Font.NORMAL)));
- setRowspan(rowspan);
- setColspan(colspan);
- setHeader(false);
- }
- }
稍許解釋一下,rowspan和colspan爲Cell的兩個屬性,寫過網頁的朋友都知道,表格中的行和列有的時候有必要進行合併,這裏就實現了這個功能。
Paragraph類我也進行了封裝:
- /*
- * Created on 2005-7-5
- *
- * TODO To change the template for this generated file go to
- * Window - Preferences - Java - Code Style - Code Templates
- */
- package javax.print.PDF;
- import com.lowagie.text.Element;
- import com.lowagie.text.Font;
- import com.lowagie.text.Paragraph;
- /**
- * @author Administrator
- *
- * TODO To change the template for this generated type comment go to Window -
- * Preferences - Java - Code Style - Code Templates
- */
- public class PDFParagragh extends Paragraph {
- public PDFParagragh(String content, int alignment, int fontSize) {
- super(content, PDFChineseFont.createChineseFont(fontSize, Font.NORMAL));
- setAlignment(alignment);
- }
- public static final int CENTER = Element.ALIGN_CENTER;
- public static final int LEFT = Element.ALIGN_LEFT;
- public static final int RIGHT = Element.ALIGN_RIGHT;
- public static final int TOP = Element.ALIGN_TOP;
- public static final int MIDDLE = Element.ALIGN_MIDDLE;
- public static final int BOTTOM = Element.ALIGN_BOTTOM;
- }
從以上兩個代碼段中可以看到PDFChineseFont.createChineseFont(int fontSize,int fontStyle)函數 這個也進行了封裝,爲自定義函數:
- /*
- * Created on 2005-7-1
- *
- * TODO To change the template for this generated file go to
- * Window - Preferences - Java - Code Style - Code Templates
- */
- package javax.print.PDF;
- import java.io.IOException;
- import com.lowagie.text.DocumentException;
- import com.lowagie.text.Font;
- import com.lowagie.text.pdf.BaseFont;
- /**
- * @author jcoder
- *
- * TODO To change the template for this generated type comment go to Window -
- * Preferences - Java - Code Style - Code Templates
- */
- public class PDFChineseFont {
- private static Font chineseFont;
- public final static Font createChineseFont(int size, int style) {
- try {
- chineseFont = new Font(BaseFont.createFont("STSong-Light",
- "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), size, style);
- } catch (DocumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return chineseFont;
- }
- }
如果無此函數定義,生成的pdf文件中的中文字符將不顯示。
最後實現自己定製好的pdf文檔格式
- /*
- * Created on 2005-7-1
- *
- * TODO To change the template for this generated file go to
- * Window - Preferences - Java - Code Style - Code Templates
- */
- package javax.print.PDF;
- import com.lowagie.text.BadElementException;
- import com.lowagie.text.DocumentException;
- import com.lowagie.text.Table;
- /**
- * @author jcoder
- *
- * TODO To change the template for this generated type comment go to Window -
- * Preferences - Java - Code Style - Code Templates
- */
- public class MyWriter extends PDFWriter {
- public MyWriter(String path) {
- super(path);
- try {
- header = new PDFParagraph("儀器設備調撥單");
- document.add(header);
- table = new Table(14);
- table.setBorderWidth(0);
- table.addCell(new PDFCell("(單價:500元以上含500元)", 1, 5));
- table.addCell(new PDFCell("2005年7月1號", 1, 9));
- document.add(table);
- table = new Table(14);
- table.setBorderWidth(1);
- table.addCell(new PDFCell("設備編號", 1, 2));
- table.addCell(new PDFCell("設備名稱", 1, 3));
- table.addCell(new PDFCell("型號規格", 1, 2));
- table.addCell(new PDFCell("數量", 1, 1));
- table.addCell(new PDFCell("單價", 1, 1));
- table.addCell(new PDFCell("總價", 1, 1));
- table.addCell(new PDFCell("附件", 1, 2));
- table.addCell(new PDFCell("備註", 1, 2));
- table.endHeaders();//換行
- table.addCell(new PDFCell("0126242245", 1, 2));
- table.addCell(new PDFCell("IBM大型機", 1, 3));
- table.addCell(new PDFCell("5465-445GH", 1, 2));
- table.addCell(new PDFCell("3", 1, 1));
- table.addCell(new PDFCell("299,000", 1, 1));
- table.addCell(new PDFCell("2,230,200", 1, 1));
- table.addCell(new PDFCell("無", 1, 2));
- table.addCell(new PDFCell("軟件學院買入", 1, 2));
- table.endHeaders();
- table.addCell(new PDFCell("調出單位意見:", 1, 11));
- table.addCell(new PDFCell("院(系)簽章", 1, 3));
- table.endHeaders();
- table.addCell(new PDFCell("申請調入單位意見:", 1, 11));
- table.addCell(new PDFCell("院(系)簽章", 1, 3));
- table.endHeaders();
- table.addCell(new PDFCell("設備管理科審批:", 1, 5));
- table.addCell(new PDFCell("實驗室與設備管理處審批", 1, 4));
- table.addCell(new PDFCell("校部審批:", 1, 5));
- table.endHeaders();
- document.add(table);
- close();//別忘記關閉
- } catch (BadElementException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (DocumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
測試類:
- /*
- * Created on 2005-7-1
- *
- * TODO To change the template for this generated file go to
- * Window - Preferences - Java - Code Style - Code Templates
- */
- package javax.print.PDF;
- /**
- * @author jcoder
- *
- * TODO To change the template for this generated type comment go to Window -
- * Preferences - Java - Code Style - Code Templates
- */
- public class Test {
- public static void main(String[] args) {
- PDFWriter pdf = new MyWriter("mine.pdf");
- }
- }
寫iText需要iText包 下載地址爲http://prdownloads.sourceforge.net/itext/itext-1.3.jar
如果要加入中文,請再下載一個包,地址爲:http://itext.sourceforge.net/downloads/iTextAsian.jar