<!--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");
- }
- }
|
|
相关推荐
这份文档会帮助你了解PDF文档的结构,以及如何利用IText的API来实现各种功能。 "iText中文教程.doc"和"学习itext笔记.docx"是两份中文学习资料,它们可能是对IText的详细解释和实践心得。这些文档可能会提供更贴近...
这本书是一部深入探讨Itext库的权威指南,通过实例讲解了如何利用Itext进行PDF操作。博客链接指向的是一个ITeye上的博客文章,作者田林ZX分享了他阅读此书的心得和实践案例,读者可以通过这个链接获取更多的实践经验...
IText是一个用于创建PDF文档的Java库。 ##### 16.4 Graphics2D `Graphics2D`是Java中的一个类,用于绘制2D图形。 ##### 16.5 开始导出 通过简单的步骤即可将图表导出为PDF。 ##### 16.6 实例应用 通过一个实际的...
嵌入式八股文面试题库资料知识宝典-华为的面试试题.zip
训练导控系统设计.pdf
嵌入式八股文面试题库资料知识宝典-网络编程.zip
人脸转正GAN模型的高效压缩.pdf
少儿编程scratch项目源代码文件案例素材-几何冲刺 转瞬即逝.zip
少儿编程scratch项目源代码文件案例素材-鸡蛋.zip
嵌入式系统_USB设备枚举与HID通信_CH559单片机USB主机键盘鼠标复合设备控制_基于CH559单片机的USB主机模式设备枚举与键盘鼠标数据收发系统支持复合设备识别与HID
嵌入式八股文面试题库资料知识宝典-linux常见面试题.zip
面向智慧工地的压力机在线数据的预警应用开发.pdf
基于Unity3D的鱼类运动行为可视化研究.pdf
少儿编程scratch项目源代码文件案例素材-霍格沃茨魔法学校.zip
少儿编程scratch项目源代码文件案例素材-金币冲刺.zip
内容概要:本文深入探讨了HarmonyOS编译构建子系统的作用及其技术细节。作为鸿蒙操作系统背后的关键技术之一,编译构建子系统通过GN和Ninja工具实现了高效的源代码到机器代码的转换,确保了系统的稳定性和性能优化。该系统不仅支持多系统版本构建、芯片厂商定制,还具备强大的调试与维护能力。其高效编译速度、灵活性和可扩展性使其在华为设备和其他智能终端中发挥了重要作用。文章还比较了HarmonyOS编译构建子系统与安卓和iOS编译系统的异同,并展望了其未来的发展趋势和技术演进方向。; 适合人群:对操作系统底层技术感兴趣的开发者、工程师和技术爱好者。; 使用场景及目标:①了解HarmonyOS编译构建子系统的基本概念和工作原理;②掌握其在不同设备上的应用和优化策略;③对比HarmonyOS与安卓、iOS编译系统的差异;④探索其未来发展方向和技术演进路径。; 其他说明:本文详细介绍了HarmonyOS编译构建子系统的架构设计、核心功能和实际应用案例,强调了其在万物互联时代的重要性和潜力。阅读时建议重点关注编译构建子系统的独特优势及其对鸿蒙生态系统的深远影响。
嵌入式八股文面试题库资料知识宝典-奇虎360 2015校园招聘C++研发工程师笔试题.zip
嵌入式八股文面试题库资料知识宝典-腾讯2014校园招聘C语言笔试题(附答案).zip
双种群变异策略改进RWCE算法优化换热网络.pdf
内容概要:本文详细介绍了基于瞬时无功功率理论的三电平有源电力滤波器(APF)仿真研究。主要内容涵盖并联型APF的工作原理、三相三电平NPC结构、谐波检测方法(ipiq)、双闭环控制策略(电压外环+电流内环PI控制)以及SVPWM矢量调制技术。仿真结果显示,在APF投入前后,电网电流THD从21.9%降至3.77%,显著提高了电能质量。 适用人群:从事电力系统研究、电力电子技术开发的专业人士,尤其是对有源电力滤波器及其仿真感兴趣的工程师和技术人员。 使用场景及目标:适用于需要解决电力系统中谐波污染和无功补偿问题的研究项目。目标是通过仿真验证APF的有效性和可行性,优化电力系统的电能质量。 其他说明:文中提到的仿真模型涉及多个关键模块,如三相交流电压模块、非线性负载、信号采集模块、LC滤波器模块等,这些模块的设计和协同工作对于实现良好的谐波抑制和无功补偿至关重要。