- package test;
- import com.ExtraTrading.admin.ConfPageUtil;
- import com.ExtraTrading.util.Common;
- import com.lowagie.text.*;
- import com.lowagie.text.Font;
- import com.lowagie.text.Image;
- import com.lowagie.text.List;
- import com.lowagie.text.Rectangle;
- import com.lowagie.text.pdf.BaseFont;
- import com.lowagie.text.pdf.PdfWriter;
- import fr.simatai.domain.FileLike;
- import org.apache.commons.io.FileUtils;
- import org.apache.commons.io.IOCase;
- import org.apache.commons.io.filefilter.SuffixFileFilter;
- import org.apache.commons.io.filefilter.TrueFileFilter;
- import org.apache.commons.lang.StringUtils;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.pdfbox.pdfparser.PDFParser;
- import org.pdfbox.pdmodel.PDDocument;
- import org.pdfbox.util.PDFTextStripper;
- import java.awt.*;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Date;
- import java.util.Iterator;
- /**
- * 很多应用程序要求动态生成 PDF 文档。这类应用程序包括银行生成用于电子邮件投递的客户报表,到读者购买特定图书章节并以 PDF 格式接收这些文档。例子罗列下去是很多的。在本文中,将使用 iText Java 库生成 PDF 文档,并引导您完成一个示例应用程序,以使您能够更好地理解和使用 iText。
- iText 是 Lowagie.com 站点(请参阅 参考资料)免费提供的 Java 库。iText 库的功能很强大,支持 HTML、RTF 和 XML 文档的生成,此外还能够生成 PDF 文档。可以从多种字体中选择文档中所使用的字体。同时,iText 的结构允许使用相同的代码生成以上任意类型的文档。
- * http://www.lowagie.com/iText/
- * iText API:近距离观察
- com.lowagie.text.Document 是生成 PDF 的主要的类。它是需要使用的第一个类。一旦开始创建文档,将需要一个写入器向文档中写入内容。com.lowagie.text.pdf.PdfWriter 就是一个 PDF 写入器。下面列出了通常需要使用的类:
- com.lowagie.text.Paragraph —— 这个类表示一个缩进的段落。
- com.lowagie.text.Chapter —— 这个类表示 PDF 文档中的章节。使用 Paragraph 作为题目并使用 int 作为章节号码来创建它。
- com.lowagie.text.Font —— 这个类包含了全部的字体规范,例如字体、大小、样式和颜色。各种字体都在这个类中声明为静态常数。
- com.lowagie.text.List —— 这个类表示一个列表,按顺序包含许多 ListItems。
- com.lowagie.text.Table —— 这个类表示包含单元格的表,单元格有序地排列在矩阵中。
- */
- public class JAVAtoPDF {
- Log log = LogFactory.getLog(JAVAtoPDF.class);
- private static final Font FONT = FontFactory.getFont(FontFactory.COURIER, 8, Font.NORMAL, Color.BLACK);
- /**
- * 写PDF文件,展示了PDF文档、章节、小节、字体、段落、表格、列表的使用
- * 最后展示如何使用写入中文。
- * @param fileName
- */
- public void writePDF(String fileName) {
- File file = new File(fileName);
- FileOutputStream out = null;
- try {
- //(1)实例化文档对象
- //第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
- Document document = new Document(PageSize.A4, 50, 50, 50, 50);
- //(2)创建写入器
- //第一个参数是对文档对象的引用,第二个参数是输出的文件,将out和document连接起来
- out = new FileOutputStream(file);
- PdfWriter writer = PdfWriter.getInstance(document, out);
- //打开文档准备写入内容
- document.addTitle("TEST的PDF");
- document.addSubject("这是一个关于发票的PDF");
- document.addKeywords("Invoice,new Green Continent Co.,Ltd");
- document.addAuthor("BOSS");
- document.addCreator("BEAUTY9235");
- document.addProducer();
- document.addCreationDate();
- document.open();
- HeaderFooter header = new HeaderFooter(new Phrase("This is a header."), false);
- HeaderFooter footer = new HeaderFooter(new Phrase("Copyright 2004 Little Ben, LLC. All rights reserved. /nNot to be reproduced or distributed without written consent from Little Ben, LLC./n Page", FONT), true);
- java.util.List numberList = new ArrayList();
- String webpath = ConfPageUtil.get("web.path");
- Image[] img = new Image[100];
- Collection c = FileUtils.listFiles(new File(webpath + "/customerlogo/"), new SuffixFileFilter(new String[]{"gif"}, IOCase.INSENSITIVE), TrueFileFilter.INSTANCE);
- int k = 0;
- for (
- Iterator it = c.iterator(); it.hasNext();) {
- File f = new File(it.next().toString());
- FileLike fileLike = new FileLike();
- fileLike.setName(f.getName());
- fileLike.setSizestr(Common.keep2DecimalFraction((f.length()) / 1024));
- fileLike.setPath(f.getPath());
- fileLike.setLastModified(f.lastModified());
- String p = StringUtils.replace(StringUtils.replace(f.getPath(), "F://data//wwwroot//", ""), "//", "/");
- log.debug(p);
- img[k++] = Image.getInstance(p);
- fileLike.setLastModifiedstr(Common.getPatternDateString("yyyy-MM-dd HH:mm:ss", new Date(f.lastModified())));
- numberList.add(fileLike);
- }
- int row = 7;
- int col = 5;
- int pageSize = 6;
- int pageCount = (numberList.size() + pageSize - 1) / pageSize;
- for (int j = 0; j < pageCount; j++) {
- Table table = new Table(col, row);
- table.setBorderWidth(0);
- table.setBorderColor(new Color(0, 0, 0));
- table.setPadding(2);
- table.setSpacing(0);
- table.setAlignment(Table.ALIGN_CENTER);
- float[] widths = {0.10f, 0.2f, 0.5f, 0.05f, 0.15f};
- table.setWidths(widths);
- // cell.setHeader(true);
- Cell c1 = new Cell(new Paragraph("name", FONT));
- c1.setHeader(true);
- table.addCell(c1);
- // table.getDefaultCell().setVerticalAlignment(Element.ALIGN_CENTER);
- Cell c2 = new Cell(new Paragraph("pic", FONT));
- c2.setHeader(true);
- table.addCell(c2);
- Cell c3 = new Cell(new Paragraph("path", FONT));
- c3.setHeader(true);
- table.addCell(c3);
- Cell c4 = new Cell(new Paragraph("size", FONT));
- c4.setHeader(true);
- table.addCell(c4);
- Cell c5 = new Cell(new Paragraph("modify", FONT));
- c5.setHeader(true);
- table.addCell(c5);
- table.endHeaders();
- for (int m = 0; m < 5; m++) {
- FileLike fileLike = (FileLike) numberList.get(pageCount + m);
- table.addCell(new Cell(new Paragraph(fileLike.getName(), FONT)));
- //table.getDefaultCell().setVerticalAlignment(Element.ALIGN_CENTER);
- table.addCell(new Cell(Image.getInstance(StringUtils.replace(StringUtils.replace(fileLike.getPath(), "F://data//wwwroot//simatai//", ""), "//", "/"))));
- table.addCell(new Cell(new Paragraph(fileLike.getPath(), FONT)));
- table.addCell(new Cell(new Paragraph(fileLike.getSizestr(), FONT)));
- table.addCell(new Cell(new Paragraph(fileLike.getLastModifiedstr(), FONT)));
- }
- table.setWidth(100);
- document.add(table);
- footer.setAlignment(HeaderFooter.ALIGN_CENTER);
- footer.setBorder(Rectangle.NO_BORDER);
- document.setFooter(footer);
- header.setAlignment(HeaderFooter.ALIGN_CENTER);
- header.setBorder(Rectangle.NO_BORDER);
- document.setHeader(header);
- document.newPage();
- }
- Image jpeg = Image.getInstance("WebRoot/icon/detourer-com.jpg");
- // log.debug(jpeg.getUrl());
- //图片居中
- jpeg.setAlignment(Image.LEFT | Image.TEXTWRAP);
- document.add(jpeg);
- //(3)下面创建章节对象
- //首先创建段落对象,作为章节的标题。FontFactory用于指定段落的字体。
- Font font = FontFactory.getFont(FontFactory.HELVETICA,
- 18, Font.BOLDITALIC, new Color(0, 0, 255));
- Paragraph chapter1_title = new Paragraph("Chapter 1",font);
- //创建了一个章节对象,标题为"Chapter 1"
- Chapter chapter1 = new Chapter(chapter1_title, 1);
- //将编号级别设为 0 就不会在页面上显示章节编号
- chapter1.setNumberDepth(0);
- //(4)创建小节对象
- //创建小节对象的标题
- font = FontFactory.getFont(FontFactory.HELVETICA, 16,
- Font.BOLD, new Color(255, 0, 0));
- Paragraph section1_title1 = new Paragraph("Section 1 of Chapter 1", font);
- //创建一个小节对象,标题为"This is Section 1 in Chapter 1",属于chapter1。
- Section section1 = chapter1.addSection(section1_title1);
- //(5)往小节中写文本内容
- Paragraph text = new Paragraph("This is the first text in section 1 of chapter 1.");
- section1.add(text);
- text = new Paragraph("Following is a 5×5 table:");
- section1.add(text);
- //(6)往小节中写表格
- //创建表格对象
- Table table = new Table(5, 5);
- //设置表格边框颜色
- table.setBorderColor(new Color(220, 255, 100));
- //设置单元格的边距间隔等
- table.setPadding(1);
- table.setSpacing(1);
- table.setBorderWidth(1);
- //单元格对象
- Cell cell = null;
- //添加表头信息
- for (int colNum=0; colNum<5; colNum++){
- cell = new Cell("header-" + colNum);
- cell.setHeader(true);
- table.addCell(cell);
- }
- table.endHeaders();
- //添加表的内容
- for (int rowNum=1; rowNum<5; rowNum++){
- for (int colNum=0; colNum<5; colNum++){
- cell= new Cell("value-" + rowNum + "-" + colNum);
- table.addCell(cell);
- }
- }
- //将表格对象添加到小节对象中
- section1.add(table);
- //(7)添加列表
- // 列表包含一定数量的 ListItem。可以对列表进行编号,也可以不编号。
- // 将第一个参数设置为 true 表明想创建一个进行编号的列表;
- // 第二个参数设置为true表示列表采用字母进行编号,为false则用数字进行编号;
- // 第三个参数为列表内容与编号之间的距离。
- List list = new List(true, false, 20);
- ListItem item = new ListItem("First item of list;");
- list.add(item);
- item = new ListItem("Second item of list;");
- list.add(item);
- item = new ListItem("Third item of list.");
- list.add(item);
- //将列表对象添加到小节对象中
- section1.add(list);
- //(8)添加中文
- //允许在PDF中写入中文,将字体文件放在classPath中。
- //simfang.ttf是仿宋的字体文件
- BaseFont bfChinese = BaseFont.createFont("simfang.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
- //中文大小为20,加粗
- font = new Font(bfChinese, 20, Font.BOLD);
- text = new Paragraph("PDF中文测试", font);
- section1.add(text);
- //(9)将章节对象加入到文档中
- document.add(chapter1);
- //(10)关闭文档
- document.close();
- System.out.println("PDF文件生成成功,PDF文件名:" + file.getAbsolutePath());
- } catch (DocumentException e) {
- System.out.println("PDF文件"+ file.getAbsolutePath() + "生成失败!" + e);
- e.printStackTrace();
- } catch (IOException ee) {
- System.out.println("PDF文件"+ file.getAbsolutePath() + "生成失败!" + ee);
- ee.printStackTrace();
- } finally {
- if (out != null){
- try {
- //关闭输出文件流
- out.close();
- } catch (IOException e1) {
- }
- }
- }
- }
- /**
- * 读PDF文件,使用了pdfbox开源项目,新的版本已经支持中文了。
- * 上www.pdfbox.org下载读PDF的jar包
- * @param fileName
- */
- public void readPDF(String fileName) {
- File file = new File(fileName);
- FileInputStream in = null;
- try {
- in = new FileInputStream(fileName);
- //新建一个PDF解析器对象
- PDFParser parser = new PDFParser(in);
- //对PDF文件进行解析
- parser.parse();
- //获取解析后得到的PDF文档对象
- PDDocument pdfdocument = parser.getPDDocument();
- //新建一个PDF文本剥离器
- PDFTextStripper stripper = new PDFTextStripper();
- //从PDF文档对象中剥离文本
- String result = stripper.getText(pdfdocument);
- System.out.println("PDF文件" + file.getAbsolutePath() + "的文本内容如下:");
- System.out.println(result);
- } catch (Exception e) {
- System.out.println("读取PDF文件"+ file.getAbsolutePath() + "生失败!" + e);
- e.printStackTrace();
- } finally {
- if (in != null){
- try {
- in.close();
- } catch (IOException e1) {
- }
- }
- }
- }
- public static void main(String[] args) {
- JAVAtoPDF pdf = new JAVAtoPDF();
- String fileName = "f:/temp/tempPDF.pdf";
- pdf.writePDF(fileName);
- pdf.readPDF(fileName);
- }
- }
iText生成PDF
最新推荐文章于 2022-11-09 10:12:07 发布