Java使用AsposePDF和AsposeWords进行表单填充

声明:本文为作者Huathy原创文章,禁止转载、爬取!否则,本人将保留追究法律责任的权力!

AsposePDF填充表单

adobe pdf表单准备

参考文档:Adobe创建和分发 PDF 表单
我们使用:adobe的Adobe Acrobat DC来编辑PDF准备表单,首先打开PDF,点击更多工具,点击准备表单,添加文本域。

在这里插入图片描述

在这里插入图片描述

引入依赖

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-pdf</artifactId>
    <version>21.11</version>
</dependency>

编写测试类

package com.hx.pdf;

import cn.hutool.core.date.DateUtil;
import com.aspose.pdf.facades.DocumentPrivilege;
import com.aspose.pdf.facades.PdfFileSecurity;

import java.util.Date;

/**
 * @author Huathy
 * @version 1.0
 * @description: TODO
 * @date 2024/7/21 10:55
 */
public class PdfTest1 {
    public static void main(String[] args) {
        fillPdfText();
    }

    private static void fillPdfText() {
        //导入必要的类
        //打开现有的 PDF 文档
        com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document("E:\\temp\\表单模板.pdf");
        //通过名称访问表单字段
        com.aspose.pdf.TextBoxField textBoxField = (com.aspose.pdf.TextBoxField) pdfDocument.getForm().get("name");
        //设置表单字段的值
        textBoxField.setValue("Huathy");
        //通过名称访问表单字段
        com.aspose.pdf.TextBoxField textBoxField1 = (com.aspose.pdf.TextBoxField) pdfDocument.getForm().get("date");
        //设置表单字段的值
        textBoxField1.setValue(DateUtil.formatDate(new Date()));
        //通过名称访问表单字段
        com.aspose.pdf.TextBoxField textBoxField2 = (com.aspose.pdf.TextBoxField) pdfDocument.getForm().get("time");
        //设置表单字段的值
        textBoxField2.setValue(DateUtil.formatTime(new Date()));
        // 填充后禁止编辑
        PdfFileSecurity fileSecurity = new PdfFileSecurity(pdfDocument);
        fileSecurity.setPrivilege(DocumentPrivilege.getForbidAll());
        //保存修改后的PDF
        pdfDocument.save("E:\\temp\\填充表单模板.pdf");
    }
}

AsposeWord表单填充

表单模板准备与生成效果

使用word邮件合并
在这里插入图片描述
效果
在这里插入图片描述

引入依赖

 <dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-words</artifactId>
    <version>20.3</version>
</dependency>

编码

package com.hx.word;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.License;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Huathy
 * @version 1.0
 * @description: TODO
 * @date 2024/7/21 10:55
 */
public class WordTest1 {
    /*
     * 加载 license
     * 由于 aspose是收费的,若没有 license,则会出现水印。
     */
    static {
        try {
            String license =
                    "<License>\n" +
                            "  <Data>\n" +
                            "    <Products>\n" +
                            "      <Product>Aspose.Cells for Java</Product>\n" +
                            "      <Product>Aspose.Words for Java</Product>\n" +
                            "      <Product>Aspose.Slides for Java</Product>\n" +
                            "    </Products>\n" +
                            "    <EditionType>Enterprise</EditionType>\n" +
                            "    <SubscriptionExpiry>yyyyMMdd</SubscriptionExpiry>\n" +
                            "    <LicenseExpiry>yyyyMMdd</LicenseExpiry>\n" +
                            "    <SerialNumber>UUID</SerialNumber>\n" +
                            "  </Data>\n" +
                            "  <Signature>Huathy</Signature>\n" +
                            "</License>";
            new License().setLicense(new ByteArrayInputStream(license.getBytes("UTF-8")));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("自动加载aspose证书文件失败!");
        }
    }

    public static void main(String[] args) throws Exception {
        fillWordText();
    }

    private static void fillWordText() throws Exception {
        // Create a new Word document
        Document doc = new Document("E:\\temp\\表单模板.docx");
        // Adding text to the document
        DocumentBuilder builder = new DocumentBuilder(doc);

        String text = "\t\t贵单位,由于XXX问题存在安全隐患,请贵单位及时整改!\n" +
                "\t 测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测测试测试测试试" +
                "测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试\n" +
                " \t并将整改报告汇报给我单位验收!";
        Map<String, String> toData = new HashMap<String, String>() {{
            put("org", "测试单位");
            put("text", text);
            put("date", DateUtil.formatDateTime(new Date()));
        }};
        String[] fieldNames = new String[toData.size()];
        String[] values = new String[toData.size()];
        int i = 0;
        for (Map.Entry<String, String> entry : toData.entrySet()) {
            fieldNames[i] = entry.getKey();
            values[i] = entry.getValue();
            i++;
        }
        doc.getMailMerge().execute(fieldNames, values);
        // Save the document
        doc.save("E:/temp/output202407.docx");
    }
}

参考文档

使用 Java 填充 PDF 文档中的表单字段
使用 Java 在 MS Word 文档中合并邮件

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Huathy-雨落江南,浮生若梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值