博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java实现PDF的生成(使用IText)
阅读量:2386 次
发布时间:2019-05-10

本文共 3942 字,大约阅读时间需要 13 分钟。

  • 基本步骤

1、新建document对象

2、建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中

3、打开文档

4、向文档中添加内容

5、关闭文档

参考地址:

http://blog.csdn.net/yi2419808933/article/details/52469241

http://blog.csdn.net/justinytsoft/article/details/53320225?locationNum=4&fps=1

http://www.cnblogs.com/dengjiali/articles/2521301.html

http://blog.csdn.net/jixiangrurui/article/details/41042925

  • Java生成pdf方案总结

1. Jasper Report生成pdf:设计思路是先生成模板,然后得到数据,最后将两者整合得到结果。但是Jasper Report的问题在于,其生成模板的方式过于复杂,即使有IDE的帮助,我们还是需要对其中的众多规则有所了解才行,否则就会给调试带来极大的麻烦。

2. openoffice生成pdf:openoffice是开源软件且能在windows和linux平台下运行。

3. itext + flying saucer生成pdf:itext和flying saucer都是免费开源的,且与平台无关,结合css和velocity技术,可以很好的实现。

http://blog.csdn.net/lewee0215/article/details/44238889?locationNum=16

https://blog.csdn.net/jimmy609/article/details/12748053

一种思路:Freemarker+Flying sauser+Itext 整合生成PDF
  • PDF操作中的一些类

文本的对象:块(Chunk)、短句(Phrase)、段落(Paragraph)

https://my.oschina.net/smellok/blog/73727

  • (使用jacob)

http://blog.csdn.net/u013238430/article/details/52943075

http://blog.csdn.net/ilovejavas/article/details/17501899

注意:Word转PDF可以使用OpenOffice

  • 添加水印文字

http://blog.sina.com.cn/s/blog_d5af62a70102vhrv.html

http://www.cnblogs.com/tankqiu/p/4412898.html

/**     * 加水印(字符串)     * @param inputFile 需要加水印的PDF路径     * @param outputFile 输出生成PDF的路径     * @param waterMarkName 水印字符*/public static void stringWaterMark(String inputFile, String waterMarkName) {    try {        String[] spe = PDFStyle.separatePath(inputFile);        String outputFile = spe[0] + "_WM." + spe[1];           PdfReader reader = new PdfReader(inputFile);        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));        int total = reader.getNumberOfPages() + 1;        PdfContentByte under;        //给每一页加水印        for (int i = 1; i < total; i++) {            Rectangle rectangle = stamper.getReader().getPageSizeWithRotation(i);            float x = rectangle.getWidth()/2;            float y = rectangle.getHeight()/2-5;            under = stamper.getUnderContent(i);            under.beginText();            under.setFontAndSize(PDFStyle.getBaseFont(), 35);            under.setTextMatrix(200, 120);            under.setColorFill(new Color(238, 209, 212));            under.showTextAligned(Element.ALIGN_CENTER, waterMarkName, x, y, 50);            // 添加水印文字            under.endText();            under.stroke();        }        stamper.close();    } catch (Exception e) {        e.printStackTrace();    }}/*** 分割路径* @param path* @return 返回分割后的路径*/public static String[] separatePath(String path){    if(StringUtils.isBlank(path)){        return null;    }    String[] sep = path.split("\\.");    return new String[]{sep[0],sep[1]};}
  • 合并PDF

http://blog.csdn.net/qq_32566703/article/details/70112058

http://blog.csdn.net/MissEel/article/details/75220571

//合并PDFpublic static void pdfMerge(Document document , PdfWriter pdfWriter){    try {        PdfReader pdfReader;        //附加信息PDF        pdfReader = new PdfReader("E:\\附加信息.pdf");        PdfContentByte pdfContentByte = pdfWriter.getDirectContent();        int totalPages = pdfReader.getNumberOfPages();        int pageOfCurrentReaderPDF = 0;        while (pageOfCurrentReaderPDF < totalPages) {            //创建新的一页            document.newPage();            pageOfCurrentReaderPDF++;            PdfImportedPage page = pdfWriter.getImportedPage(pdfReader, pageOfCurrentReaderPDF);            pdfContentByte.addTemplate(page, 0, 0);        }    } catch (IOException e) {        e.printStackTrace();    }}
  • iText和flying saucer结合生成pdf的技术(这个还没去实现/测试)

https://blog.csdn.net/yelongshang1/article/details/51098242

http://gaojunwei.iteye.com/blog/1996749

https://blog.csdn.net/xxj_jing/article/details/70888607

https://blog.csdn.net/cuiyaonan2000/article/details/42459259

https://blog.csdn.net/mhouwei62/article/details/51394804

注意:Itext中的Table与PdfTable

http://www.open-open.com/doc/view/c4331fe8a1084315890ee17df8f96da1

https://blog.csdn.net/feng27156/article/details/16879069

你可能感兴趣的文章
安全工具集合
查看>>
Metasploit 3.3 Development Updates
查看>>
Windows Services for UNIX Version 3.5
查看>>
Linux 测试工具
查看>>
Modifying SSH to Capture Login Credentials from Attackers
查看>>
nikto 2.1 coming
查看>>
How to own a Windows Domain
查看>>
Longcat – multi-protocol stress testing tool
查看>>
数据流0day原理+实践
查看>>
淺談以STIX實現網路威脅情報標準化框架
查看>>
Top IT management trends - the next 5 years
查看>>
推荐 OWASP - Transport Layer Protection Cheat Sheet
查看>>
AutoNessus v1.3.2 released
查看>>
hack tools
查看>>
rhel5中管理swap空间
查看>>
/proc filesystem allows bypassing directory permissions on Linux
查看>>
nginx dos
查看>>
RASP解决方案包括开源方案
查看>>
Linux下共享文件系统文件传输的简单设计(转载)
查看>>
点评Ubuntu下的文件安全删除工具
查看>>