您的位置:首页 > 其它

利用iText写PDF

2009-11-11 11:37 176 查看
(原贴地址)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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: