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

  • 基本步骤

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

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

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

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java使用com.lowagie.text.pdf插件编写的PDF报表工具类,支持动态报表创建,使用简单,附件中包含了测试类和生成的报表文件。附件中的代码需要修改相关的保存路径后可以直接使用。创建一张报表例子: private JsFileExportResult createRowReport() { String condition = "开始时间:2018-02-02 14:00:30 结束时间:2018-02-06 16:00:30"; PDFGridReport pdfReport = new PDFGridReport("报表创建测试", GridReportTestModel.getModels()); pdfReport.addCondition(condition); pdfReport.header("字段名称1", "item1", 0, 0).width(160); pdfReport.header("字段名称3", "item3", 0, 2).getCell().backgroundColor(Color.ORANGE); pdfReport.header("字段名称4", "item4", 0, 3); pdfReport.header("字段名称5", "item5", 0, 4); pdfReport.header("字段名称2", "item2", 0, 1); pdfReport.header("值", "value", 0, 5).alignH(HAlign.ALIGN_CENTER).getCell().alignH(HAlign.ALIGN_RIGHT); pdfReport.header("时间", "time", 0, 6); pdfReport.header("图片", "image", 0, 7).width(60).alignH(HAlign.ALIGN_CENTER).getCell() .alignH(HAlign.ALIGN_CENTER).alignV(VAlign.ALIGN_MIDDLE); // 横向打印 pdfReport.getPageSetting().setPrintHorizontal(); pdfReport.group("item1").childGroup("item2"); pdfReport.setCellFormat(new PDFCellValueFormat() { @Override public String format(String fieldName, String oriValue) { if ("value".equals(fieldName)) { return String.format("%.2f", Double.parseDouble(oriValue)).toString(); } else { return oriValue; } } }); JsFileExportResult fileResult = pdfReport.createReport(); return fileResult; }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值