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


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

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

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

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

    这里用的是itext生成PDF

    maven依赖

    <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中 甲方:_______ 姓名:_____ 之类的下划线,,他会自动生成表单
    导入之后

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

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

    坑来了

    这里可以看到表单的字体和大小 

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

    如果需要填充的内容包含中文 一点要删掉默认生成的表单域  然后重新添加  注意字体

    小塚明朝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());
            }
    
        }






  • 相关阅读:
    牛客练习赛51 D题
    Educational Codeforces Round 72 (Rated for Div. 2) C题
    Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises) C题
    Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises) A题
    Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises) A题
    Educational Codeforces Round 72 (Rated for Div. 2) B题
    Educational Codeforces Round 72 (Rated for Div. 2) A题
    《DSP using MATLAB》Problem 7.2
    《DSP using MATLAB》Problem 7.1
    《DSP using MATLAB》Problem 6.24
  • 原文地址:https://www.cnblogs.com/xxj0316/p/8298075.html
Copyright © 2020-2023  润新知