<!--start of article content -->
最近由于项目需要,开始使用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读取PDF后使用pdfbox将PDF转为图片。压缩包内附有相应JAR文件
itext 生成pdf 目录,itext 生成pdf 目录,itext 生成pdf 目录
利用itext操作pdf从数据库导出大量数据 压缩包含需要的jar包、案例源码
java 利用利用poi+itextpdf进行word转pdf,里面有源码和依赖还有效果图,无需安装插件,感觉是一种比较好的方法
Android使用iText生成pdf并读取pdf内容,iText是著名的开放源码的站点sourceforge的一个项目,可用来生成Pdf文件和读取Pdf文件内容.
iText生成pdf解决中文不显示字库,pdf凉字不显示,由于生成iText插件生成pdf的时候中文会显示不出来,遇到过的是"凉"字,查到是字体库的原因,网上下载字体库msyh.ttc,生成的时候指定字体库,就可以解决了,小bug一...
使用IText生成PDF和WORD文档 包括源码和IText所需jar包
adobe pdf编辑器,解压安装即可使用,配合java利用itextpdf根据模板导出pdf文章中只做模板的方法;
将word转换成pdf确实有很多种方案!最近正好需要做一个这样的功能,需求是将word模板进行签名后转换为pdf。...这里记录一下最终的方案:利用poi+itextpdf进行word转pdf。此资源按包含源码和maven依赖。
itext对pdf进行编辑.使用Adobe对pdf创建表单,itext对表单填充,下拉选,单选,文本框.复选框.itext对pdf进行编辑.使用Adobe对pdf创建表单,itext对表单填充,下拉选,单选,文本框.复选框
在Java代码中使用itext生成PDF文档,实现PDF文档的导出。
此文档主要讲述了使用Java组件iText来生成PDF,操作PDF
com.itextpdf.itextpdf.5.5.13.3 相关jar包和包含源码jar包 <groupId>com.itextpdf</groupId> <artifactId>itextpdf <version>5.5.13.3 下载地址为: ...
iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。
iText是著名的开放源码的站点sourceforge的一个项目,它是一个用于生成PDF文档的一个java开源库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。如果PDF是标记的且包含一个结构树,...
iText7 用于html转换pdf核心包-html2pdf-1.0.1.jar。。。。
java解决Itext生成PDF中文不换行的问题
java利用iText转换jpg为PDF格式文件,详细代码100%可用
itext生成PDF图片文档