根据现有PDF模板填充信息(SpringBoot)

来源:互联网 发布:windows入门教程 编辑:程序博客网 时间:2024/06/06 02:22

根据现有PDF模板填充信息(SpringBoot+maven)

首先得有一个pdf模板,建立pdf模板需要下载工具
红色框为文本框,filename为域名。java需要根据域名赋值

这里写图片描述

pom 文件配置

   <!-- always needed -->        <dependency>            <groupId>com.itextpdf</groupId>            <artifactId>layout</artifactId>            <version>7.0.3</version>        </dependency>        <!-- only needed for forms -->        <dependency>            <groupId>com.itextpdf</groupId>            <artifactId>forms</artifactId>            <version>7.0.3</version>        </dependency>        <!-- only needed for Asian fonts -->        <dependency>            <groupId>com.itextpdf</groupId>            <artifactId>font-asian</artifactId>            <version>7.0.3</version>        </dependency>

java 代码

 // pdf文件    private Boolean tranPdf(CopyRightsVo copyRightsVo) {    // 模板地址        String filePath = UlegalZCUtil.rootPath() + File.separator + "pdf" + File.separator + "templateC.pdf";        // 填完信息后生成新的模板地址        String toPath = UlegalZCUtil.rootPath() + File.separator + "pdf" + File.separator + "templateOL" + ".pdf";        try {            System.out.println(filePath);            PdfDocument pdfDoc = new PdfDocument(new PdfReader(filePath), new PdfWriter(toPath));            PdfAcroForm pdfAcroForm = PdfAcroForm.getAcroForm(pdfDoc, true);            pdfAcroForm.getField("fileName").setValue("小编");            pdfAcroForm.getField("applicant").setValue("ol");            Date date = new Date();            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");            String cmmitTime = simpleDateFormat.format(date);            pdfAcroForm.getField("time").setValue(cmmitTime);//            pdfAcroForm.getField("hashValue").setValue(copyRightsVo.getHashValue());            pdfAcroForm.flattenFields();            pdfDoc.close();        } catch (Exception e) {            e.printStackTrace();            return false;        }        return true;    }

搞定
这里写图片描述

阅读全文
0 0
原创粉丝点击