欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java实现PDF的生成(使用IText)

程序员文章站 2022-04-19 20:24:34
...
  • 基本步骤

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

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

//合并PDF
public 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

Java实现PDF的生成(使用IText)

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

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