You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Java向PDF表单输入字段写入信息?

在Java中给PDF表单字段填充数据的实现方案

当然可以!你完全不用重新生成整个PDF文件,Java里有成熟的库能直接操作PDF的表单字段,和PHP的思路类似——直接定位到表单对象然后给字段赋值就行。下面我给你分享两个最常用的实现方案:

方案一:Apache PDFBox(开源免费,Apache协议)

这是Apache基金会的开源库,完全免费且功能强大,适合大多数场景。

1. 引入依赖(Maven为例)

<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.32</version> <!-- 建议使用最新稳定版 -->
</dependency>

2. 核心代码示例

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDField;

import java.io.File;
import java.io.IOException;

public class PdfFormFiller {
    public static void main(String[] args) {
        // 用try-with-resources自动释放文档资源,避免内存泄漏
        try (PDDocument document = PDDocument.load(new File("客户订单模板.pdf"))) {
            // 获取PDF的表单对象
            PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
            if (acroForm == null) {
                System.out.println("这个PDF没有可编辑的表单字段!");
                return;
            }

            // 给指定字段赋值(字段名要和PDF模板里的字段ID完全一致,大小写敏感)
            PDField customerName = acroForm.getField("customer_name");
            if (customerName != null) {
                customerName.setValue("张三");
            }

            PDField orderId = acroForm.getField("order_id");
            if (orderId != null) {
                orderId.setValue("ORD-2024-001");
            }

            // 保存填充后的PDF
            document.save("已填充的客户订单.pdf");
            System.out.println("PDF表单填充完成!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

方案二:iText 7(开源/商业双授权)

iText是老牌的PDF处理库,功能更丰富,但开源版本采用AGPL协议——如果你的项目是闭源商业项目,需要购买商业授权。

1. 引入依赖(Maven为例,开源版本)

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext7-core</artifactId>
    <version>7.2.5</version> <!-- 建议使用最新稳定版 -->
</dependency>

2. 核心代码示例

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormField;

import java.io.IOException;

public class ItextPdfFiller {
    public static void main(String[] args) {
        try (PdfReader reader = new PdfReader("客户订单模板.pdf");
             PdfWriter writer = new PdfWriter("已填充的客户订单.pdf");
             PdfDocument document = new PdfDocument(reader, writer)) {

            // 获取表单对象
            PdfAcroForm form = PdfAcroForm.getAcroForm(document, true);
            if (form == null) {
                System.out.println("这个PDF没有可编辑的表单字段!");
                return;
            }

            // 给字段赋值
            PdfFormField customerName = form.getField("customer_name");
            if (customerName != null) {
                customerName.setValue("张三");
            }

            PdfFormField orderId = form.getField("order_id");
            if (orderId != null) {
                orderId.setValue("ORD-2024-001");
            }

            // 如果需要锁定字段防止后续编辑,可以添加这行代码
            // form.flattenFields();

            System.out.println("PDF表单填充完成!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

一些关键注意事项

  • 字段名匹配:一定要确保你用的字段名和PDF模板里的字段ID完全一致,大小写敏感!可以用Acrobat Reader打开模板,右键字段→属性→查看“名称”,或者用库的getFields()方法列出所有字段名排查。
  • 特殊字段处理:比如单选框、复选框,设置值的时候要传入字段的选项值(不是显示的文本),比如单选框的选项值可能是"1"、"2",而不是"男"、"女"。
  • 资源释放:一定要用try-with-resources语法自动关闭文档、流,避免内存泄漏。

内容的提问来源于stack exchange,提问作者Jamal Williams

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:49:12