Adobe Acrobat pro生成PDF模版 java生成PDF

最近作了一个关于动态生成PDF合同的需求  java生成PDF 网络上随便一搜遍有了java

不要用手动在代码里面输入合同中的文字这种方式 如这样的方式 http://blog.csdn.net/justinytsoft/article/details/53320225网络

需求的多变,那天须要修改一下字体的颜色,或者字体, 增长一行字,等等其余奇葩要求,上面这个博客的方式都得修改代码maven

使用itext使用pdf模版的方式,就不须要修改代码了.测试

 

这里用的是itext生成PDF字体

maven依赖spa

<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.6</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>

参考如下博客 ,可是该中仍是有一些问题,PDF模版
http://blog.csdn.net/top__one/article/details/65442390

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
如下都是踩过的坑,但愿能帮助后来的人
原始pdf



word模版转PDF后 导入Adobe Acrobat pro 若是pdf中 甲方:_______ 姓名:_____ 之类的下划线,,他会自动生成表单
导入以后

上图中的黑框  就是表单了  双击.net

重点关注表单中的名称  这个就是之后再代码分钟动态填充的3d

坑来了

这里能够看到表单的字体和大小 code

实际上自动生成的表单,  咱们在往里面填充内容的时候会有问题的.orm

若是须要填充的内容包含中文 一点要删掉默认生成的表单域  而后从新添加  注意字体

小塚明朝Pr6N字体(Kozuka Mincho Pr6N R)

填充汉字的时候有些汉字会显示不出来   好比  军 ,资  ,确定还有不少字不能显示出来

第一感受就是更换字体  而后换成微软雅黑

好吧,.如今汉字都显示不出来了,只显示数字

 

试了不少字体.都不行

最后,删除了自动生成的表单.手动往须要  动态添加内容的地方 添加表单域

实际测试  这种方法可行

 

 

 itext 代码案例    

//PdfTemplate类里面定义了全部表单域的名称 这里只举例子了两个表单域//上面提到了表单域的名称

public static void fillTemplate(PdfTemplate pdfTemplate)  throws BusinessException{
        // 模板路径
        String templatePath = "/pdf/xxxx.pdf";
        // 填充模版文件后生成的待签署的合同路径

        //TODO 地址
        // 文件名BORROWPROTOCOL+协议编号  
        String newPDFPath = TEMP_PDF_LOCATION+pdfTemplate.getContract_no()+EXPANDEDNAME;
        PdfReader reader;
        FileOutputStream out;
        ByteArrayOutputStream bos;
        PdfStamper stamper;

        try {
            out = new FileOutputStream(newPDFPath);
            // 读取pdf模板
            reader = new PdfReader(templatePath);
            bos = new ByteArrayOutputStream();
            stamper = new PdfStamper(reader, bos);
            AcroFields form = stamper.getAcroFields();

            //往pdf合同模版里面设置值  //mock
            //合同编号
            form.setField("contract_no", pdfTemplate.getContract_no());
            //甲方
            form.setField("first_user_name", pdfTemplate.getFirst_user_name());
            //...须要填充的其余表单域
            

            // 若是为false那么生成的PDF文件还能编辑,必定要设为true
            stamper.setFormFlattening(true);
            stamper.close();
            Document doc = new Document();
            PdfCopy copy = new PdfCopy(doc, out);

            doc.open();
            //pdf模版的页数
            int pagecount= reader.getNumberOfPages();
            for(int i=1 ;i<pagecount+1;i++){
                PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), i);
                copy.addPage(importPage);
            }
            doc.close();

        } catch (IOException e) {
            throw new BusinessException("生成合同模版失败,合同编号:"+pdfTemplate.getContract_no());
        } catch (DocumentException e) {
            throw new BusinessException("生成合同模版失败,合同编号:"+pdfTemplate.getContract_no());
        }

    }
相关文章
相关标签/搜索