利用iText写PDF心得

本文介绍如何使用iText库创建包含中文内容及特定格式的PDF文件,包括自定义字体、段落及表格等组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近由于项目需要,开始使用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.  
  9. import java.io.FileNotFoundException;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.lowagie.text.Cell;
  14. import com.lowagie.text.Document;
  15. import com.lowagie.text.DocumentException;
  16. import com.lowagie.text.Paragraph;
  17. import com.lowagie.text.Rectangle;
  18. import com.lowagie.text.Table;
  19. import com.lowagie.text.pdf.PdfWriter;
  20.  
  21. /**
  22.  * @author jcoder
  23.  * 
  24.  * TODO To change the template for this generated type comment go to Window -
  25.  * Preferences - Java - Code Style - Code Templates
  26.  */
  27. abstract public class PDFWriter {
  28.     protected Document document = null;
  29.     protected FileOutputStream out = null;
  30.     protected Rectangle pageSize = null;
  31.     protected String filePath = null;
  32.     protected Cell cell = null;
  33.     protected Paragraph header = null;
  34.     protected Paragraph prg = null;
  35.     protected Table table = null;
  36.  
  37.     public PDFWriter(String filePath) {
  38.         try {
  39.             this.filePath = filePath;
  40.             document = new Document();
  41.             out = new FileOutputStream(filePath);
  42.             PdfWriter.getInstance(document, out);
  43.             document.open();
  44.         } catch (FileNotFoundException e) {
  45.             // TODO Auto-generated catch block
  46.             e.printStackTrace();
  47.         } catch (DocumentException e) {
  48.             // TODO Auto-generated catch block
  49.             e.printStackTrace();
  50.         }
  51.  
  52.     }
  53.  
  54.     public void close() {
  55.         try {
  56.             document.close();
  57.             out.close();
  58.         } catch (IOException e) {
  59.             // TODO Auto-generated catch block
  60.             e.printStackTrace();
  61.         }
  62.     }
  63.  
  64. }
由于我需要在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.  
  9. import com.lowagie.text.BadElementException;
  10. import com.lowagie.text.Cell;
  11. import com.lowagie.text.Chunk;
  12. import com.lowagie.text.Font;
  13.  
  14. /**
  15.  * @author jcoder
  16.  * 
  17.  * TODO To change the template for this generated type comment go to Window -
  18.  * Preferences - Java - Code Style - Code Templates
  19.  */
  20. public class PDFCell extends Cell {
  21.  
  22.     public PDFCell(String content, int rowspan, int colspan)
  23.             throws BadElementException {
  24.         super(new Chunk(content, PDFChineseFont
  25.                 .createChineseFont(10, Font.NORMAL)));
  26.         setRowspan(rowspan);
  27.         setColspan(colspan);
  28.         setHeader(false);
  29.     }
  30. }
稍许解释一下,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.  
  9. import com.lowagie.text.Element;
  10. import com.lowagie.text.Font;
  11. import com.lowagie.text.Paragraph;
  12.  
  13. /**
  14.  * @author Administrator
  15.  * 
  16.  * TODO To change the template for this generated type comment go to Window -
  17.  * Preferences - Java - Code Style - Code Templates
  18.  */
  19. public class PDFParagragh extends Paragraph {
  20.  
  21.     public PDFParagragh(String content, int alignment, int fontSize) {
  22.         super(content, PDFChineseFont.createChineseFont(fontSize, Font.NORMAL));
  23.         setAlignment(alignment);
  24.     }
  25.  
  26.     public static final int CENTER = Element.ALIGN_CENTER;
  27.     public static final int LEFT = Element.ALIGN_LEFT;
  28.     public static final int RIGHT = Element.ALIGN_RIGHT;
  29.     public static final int TOP = Element.ALIGN_TOP;
  30.     public static final int MIDDLE = Element.ALIGN_MIDDLE;
  31.     public static final int BOTTOM = Element.ALIGN_BOTTOM;
  32.  
  33. }
从以上两个代码段中可以看到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.  
  9. import java.io.IOException;
  10.  
  11. import com.lowagie.text.DocumentException;
  12. import com.lowagie.text.Font;
  13. import com.lowagie.text.pdf.BaseFont;
  14.  
  15. /**
  16.  * @author jcoder
  17.  * 
  18.  * TODO To change the template for this generated type comment go to Window -
  19.  * Preferences - Java - Code Style - Code Templates
  20.  */
  21. public class PDFChineseFont {
  22.     private static Font chineseFont;
  23.  
  24.     public final static Font createChineseFont(int size, int style) {
  25.         try {
  26.             chineseFont = new Font(BaseFont.createFont("STSong-Light",
  27.                     "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), size, style);
  28.         } catch (DocumentException e) {
  29.             // TODO Auto-generated catch block
  30.             e.printStackTrace();
  31.         } catch (IOException e) {
  32.             // TODO Auto-generated catch block
  33.             e.printStackTrace();
  34.         }
  35.         return chineseFont;
  36.     }
  37. }
如果无此函数定义,生成的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.  
  9. import com.lowagie.text.BadElementException;
  10. import com.lowagie.text.DocumentException;
  11. import com.lowagie.text.Table;
  12.  
  13. /**
  14.  * @author jcoder
  15.  * 
  16.  * TODO To change the template for this generated type comment go to Window -
  17.  * Preferences - Java - Code Style - Code Templates
  18.  */
  19. public class MyWriter extends PDFWriter {
  20.     public MyWriter(String path) {
  21.         super(path);
  22.         try {
  23.             header = new PDFParagraph("仪器设备调拨单");
  24.             document.add(header);
  25.             table = new Table(14);
  26. table.setBorderWidth(0);
  27.             table.addCell(new PDFCell("(单价:500元以上含500元)", 1, 5));
  28.             table.addCell(new PDFCell("2005年7月1号", 1, 9));
  29.             document.add(table);
  30.             table = new Table(14);
  31. table.setBorderWidth(1);
  32.             table.addCell(new PDFCell("设备编号", 1, 2));
  33.             table.addCell(new PDFCell("设备名称", 1, 3));
  34.             table.addCell(new PDFCell("型号规格", 1, 2));
  35.             table.addCell(new PDFCell("数量", 1, 1));
  36.             table.addCell(new PDFCell("单价", 1, 1));
  37.             table.addCell(new PDFCell("总价", 1, 1));
  38.             table.addCell(new PDFCell("附件", 1, 2));
  39.             table.addCell(new PDFCell("备注", 1, 2));
  40.             table.endHeaders();//换行
  41.             table.addCell(new PDFCell("0126242245", 1, 2));
  42.             table.addCell(new PDFCell("IBM大型机", 1, 3));
  43.             table.addCell(new PDFCell("5465-445GH", 1, 2));
  44.             table.addCell(new PDFCell("3", 1, 1));
  45.             table.addCell(new PDFCell("299,000", 1, 1));
  46.             table.addCell(new PDFCell("2,230,200", 1, 1));
  47.             table.addCell(new PDFCell("无", 1, 2));
  48.             table.addCell(new PDFCell("软件学院买入", 1, 2));
  49.             table.endHeaders();
  50.             table.addCell(new PDFCell("调出单位意见:", 1, 11));
  51.             table.addCell(new PDFCell("院(系)签章", 1, 3));
  52.             table.endHeaders();
  53.             table.addCell(new PDFCell("申请调入单位意见:", 1, 11));
  54.             table.addCell(new PDFCell("院(系)签章", 1, 3));
  55.             table.endHeaders();
  56.             table.addCell(new PDFCell("设备管理科审批:", 1, 5));
  57.             table.addCell(new PDFCell("实验室与设备管理处审批", 1, 4));
  58.             table.addCell(new PDFCell("校部审批:", 1, 5));
  59.             table.endHeaders();
  60.             document.add(table);
  61.             close();//别忘记关闭
  62.         } catch (BadElementException e) {
  63.             // TODO Auto-generated catch block
  64.             e.printStackTrace();
  65.         } catch (DocumentException e) {
  66.             // TODO Auto-generated catch block
  67.             e.printStackTrace();
  68.         }
  69.     }
  70. }
测试类:
  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. /**
  10.  * @author jcoder
  11.  * 
  12.  * TODO To change the template for this generated type comment go to Window -
  13.  * Preferences - Java - Code Style - Code Templates
  14.  */
  15. public class Test {
  16.  
  17.     public static void main(String[] args) {
  18.         PDFWriter pdf = new MyWriter("mine.pdf");
  19.     }
  20. }
写iText需要iText包 下载地址为 http://prdownloads.sourceforge.net/itext/itext-1.3.jar 如果要加入中文,请再下载一个包,地址为: http://itext.sourceforge.net/downloads/iTextAsian.jar 写的比较浅,希望对大家会有帮助
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值