Adobe Acrobat Pro DC工具配合iText Java类库编辑操作PDF模版

624 阅读2分钟

一.工具

二.准备模版

1.用word或者excel将要制作的文档排版布局做好

2.用Adobe Acrobat Pro DC打开word或者excel文件,创建pdf文件

image.png

3.开始制作表单,右侧菜单中有准备表单菜单,点击准备菜单就可以进行表单设置

image.png

表单内容会自动进行填充

image.png

以上字段根据后台java iText 类的定义的字段名称要求修改,便于后台能够精准赋值


右键点击每个表单输入框,点击属性即可设置每个框的字段名称,文本框大小位置进行设置


如果遇到需要checkbox选择框的,直接在指定位置添加复选框,并沟通协调好checkbox的名称和导出值。

image.png 表单设置完毕,交付后台研发,进行数据文件自动生成。

三.代码示例

代码块

/** 添加的. */
public static final String COVER = "/Users/liulanlan/project/mall-ecms/mall-ecms-web/src/main/resources/pdfTemplate/financeAttach6.pdf";
/** 主pdf. */
public static final String SRC = "/Users/liulanlan/project/mall-ecms/mall-ecms-web/src/main/resources/pdfTemplate/selfContractTemplate.pdf";
/** 存在哪. */
public static final String DEST = "/Users/liulanlan/project/mall-ecms/mall-ecms-web/src/main/resources/pdfTemplate/selfAdd.pdf";
static final String url1="http://gitlab.itextsupport.com/itext/sandbox/raw/master/resources/pdfs/hero.pdf";
static final String url2="http://gitlab.itextsupport.com/itext/sandbox/raw/master/resources/pdfs/hero.pdf";
    /**
     * 填充pdf模版表单
     * @param templatePath
     * @param contractPdfModel
     * @return
     */
    private static void fillPdfModel(String templatePath,
            ContractPdfModel contractPdfModel) {
        //生成的新文件路径
        String newPDFPath = "/Users/liulanlan/project/mall-ecms/mall-ecms-web/src/main/resources/pdfTemplate/newPdf.pdf";
        PdfReader reader;
        FileOutputStream out;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        PdfStamper stamper;
        try {
            out = new FileOutputStream(newPDFPath);//输出流
            reader = new PdfReader(templatePath);//读取pdf模板
            bos = new ByteArrayOutputStream();
            stamper = new PdfStamper(reader, bos);
            AcroFields form = stamper.getAcroFields();
            //设置模版中的表单属性值
            form.setField("partyA.partyName","partyName1");
            form.setField("partyA.partyAddress","address1");
            //设置模版中多选框是否选中
            form.setField("accountDay0","Off");
            form.setField("accountDay15","On");
            form.setField("accountDay30","Off");
            stamper.setFormFlattening(false);
            stamper.close();
            //将bos导入新的文件中(实际程序中,返回bos即可)
            Document doc = new Document();
            PdfCopy copy = new PdfCopy(doc, out);
            doc.open();
            copy.addDocument(reader);
            doc.close();
        } catch (Exception e){
            //do nothing
        } finally {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
     /**
     * 本地pdf文件合并
     * @param src
     * @param dest
     * @throws IOException
     * @throws DocumentException
     */
    public static void manipulatePdf(String src, String dest)
            throws IOException, DocumentException {
        PdfReader cover = new PdfReader(COVER);
        PdfReader reader = new PdfReader(src);
        Document document = new Document();
        PdfCopy copy = new PdfCopy(document, new FileOutputStream(dest));
        document.open();
        copy.addDocument(reader);
        copy.addDocument(cover);
        document.close();
        cover.close();
        reader.close();
    }
    
    /**
     * url远程文件合并
     * @param url1
     * @param url2
     * @throws IOException
     * @throws DocumentException
     */
    private static void mergeUrlPdf(String url1, String url2) throws IOException, DocumentException{
        URL url=new URL(url1);
        URLConnection conn = url.openConnection();
        InputStream is=conn.getInputStream();
        PdfReader cover = new PdfReader(is);
        URL u=new URL(url2);
        URLConnection con = u.openConnection();
        InputStream is2=con.getInputStream();
        PdfReader reader = new PdfReader(is2);
        Document document = new Document();
        PdfCopy copy = new PdfCopy(document, new FileOutputStream(DEST));
        document.open();
        copy.addDocument(cover);
        copy.addDocument(reader);
        document.close();
        cover.close();
        reader.close();
    }
    
     /**
     * Main method.
     * @param
     * @throws DocumentException
     * @throws IOException
     */
    public static void main(String[] args)
            throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        //本地
        manipulatePdf(SRC, DEST);
        //url
        mergeUrlPdf(url1,url2);
        //表单填充
        ContractPdfModel contractPdfModel = new ContractPdfModel();
        fillPdfModel(SRC,contractPdfModel);
    }

\