Python / Rust / Node.js / Go / C# 填写 PDF 表单
在每个受支持的绑定中,读取并以代码填写表单字段:
Python
from pdf_oxide import PdfDocument
doc = PdfDocument("application.pdf")
# 读取现有字段
for field in doc.page(0).form_fields():
print(f"{field.name}: {field.value}")
# 填写字段并保存
doc.set_form_field("full_name", "Jane Doe")
doc.set_form_field("email", "jane@example.com")
doc.set_form_field("agree_terms", True)
doc.save("filled-application.pdf")
WASM
import { WasmPdfDocument } from "pdf-oxide-wasm";
const doc = new WasmPdfDocument(bytes);
// 读取现有字段
const fields = doc.getFormFields();
for (const field of fields) {
console.log(`${field.name}: ${field.value}`);
}
// 以 XFDF 导出表单数据
const xfdf = doc.exportFormData("xfdf");
console.log(xfdf);
doc.free();
Rust
use pdf_oxide::editor::{DocumentEditor, EditableDocument, FormFieldValue};
let mut editor = DocumentEditor::open("application.pdf")?;
// 读取现有字段
let fields = editor.get_form_fields()?;
for field in &fields {
println!("{}: {:?}", field.name(), field.value());
}
// 填写字段并保存
editor.set_form_field_value("full_name", FormFieldValue::Text("Jane Doe".into()))?;
editor.set_form_field_value("email", FormFieldValue::Text("jane@example.com".into()))?;
editor.set_form_field_value("agree_terms", FormFieldValue::Boolean(true))?;
editor.save("filled-application.pdf")?;
Go
package main
import (
"fmt"
"log"
pdfoxide "github.com/yfedoseev/pdf_oxide/go"
)
func main() {
doc, err := pdfoxide.Open("application.pdf")
if err != nil { log.Fatal(err) }
defer doc.Close()
// 读取现有字段
fields, _ := doc.FormFields()
for _, f := range fields {
fmt.Printf("%s: %s\n", f.Name, f.Value)
}
// 通过编辑器填写字段并保存
editor, err := pdfoxide.OpenEditor("application.pdf")
if err != nil { log.Fatal(err) }
defer editor.Close()
_ = editor.SetFormFieldValue("full_name", "Jane Doe")
_ = editor.SetFormFieldValue("email", "jane@example.com")
_ = editor.SetFormFieldValue("agree_terms", "Yes")
_ = editor.Save("filled-application.pdf")
}
C#
using PdfOxide;
using (var doc = PdfDocument.Open("application.pdf"))
{
foreach (var f in doc.GetFormFields())
Console.WriteLine($"{f.Name}: {f.Value}");
}
using var editor = DocumentEditor.Open("application.pdf");
editor.SetFormFieldValue("full_name", "Jane Doe");
editor.SetFormFieldValue("email", "jane@example.com");
editor.SetFormFieldValue("agree_terms", "Yes");
editor.Save("filled-application.pdf");
PDF Oxide 支持 AcroForm 字段(文本、复选框、单选按钮、下拉列表)以及 XFA 表单分析。采用 MIT 许可证,不受 AGPL 限制。
安装
pip install pdf_oxide
读取表单字段
列出全部字段
Python
from pdf_oxide import PdfDocument
doc = PdfDocument("form.pdf")
for field in doc.page(0).form_fields():
print(f"名称: {field.name}")
print(f" 类型: {field.field_type}")
print(f" 值: {field.value}")
print(f" 区域: {field.bounds}")
print()
WASM
const doc = new WasmPdfDocument(bytes);
const fields = doc.getFormFields();
for (const field of fields) {
console.log(`名称: ${field.name}`);
console.log(` 类型: ${field.fieldType}`);
console.log(` 值: ${field.value}`);
}
doc.free();
Rust
let mut editor = DocumentEditor::open("form.pdf")?;
let fields = editor.get_form_fields()?;
for field in &fields {
println!("名称: {}", field.name());
println!(" 类型: {:?}", field.field_type());
println!(" 值: {:?}", field.value());
}
Go
doc, _ := pdfoxide.Open("form.pdf")
defer doc.Close()
fields, _ := doc.FormFields()
for _, f := range fields {
fmt.Printf("名称: %s\n 类型: %s\n 值: %s\n", f.Name, f.Type, f.Value)
}
C#
using var doc = PdfDocument.Open("form.pdf");
foreach (var f in doc.GetFormFields())
{
Console.WriteLine($"名称: {f.Name}");
Console.WriteLine($" 类型: {f.Type}");
Console.WriteLine($" 值: {f.Value}");
}
字段类型
| 类型 | 描述 | 示例值 |
|---|---|---|
Text |
单行或多行文本 | "Jane Doe" |
Button |
复选框或单选按钮 | True / False |
Choice |
下拉列表或列表框 | "Option A" |
Signature |
数字签名 | (签名数据) |
填写表单字段
文本字段
doc = PdfDocument("form.pdf")
doc.set_form_field("first_name", "Jane")
doc.set_form_field("last_name", "Doe")
doc.set_form_field("address", "123 Main St\nApt 4B\nNew York, NY 10001")
doc.save("filled.pdf")
复选框
doc = PdfDocument("form.pdf")
doc.set_form_field("agree_terms", True)
doc.set_form_field("opt_in_newsletter", False)
doc.save("filled.pdf")
下拉 / 选择字段
doc = PdfDocument("form.pdf")
doc.set_form_field("country", "United States")
doc.set_form_field("department", "Engineering")
doc.save("filled.pdf")
批量填写
来自 CSV 文件
为 CSV 的每一行填写同一份模板:
import csv
from pdf_oxide import PdfDocument
with open("applicants.csv") as f:
reader = csv.DictReader(f)
for i, row in enumerate(reader):
doc = PdfDocument("template.pdf")
for field_name, value in row.items():
doc.set_form_field(field_name, value)
doc.save(f"filled_{i + 1}.pdf")
来自字典
from pdf_oxide import PdfDocument
data = {
"full_name": "Jane Doe",
"email": "jane@example.com",
"phone": "555-0123",
"department": "Engineering",
"start_date": "2025-03-01",
}
doc = PdfDocument("onboarding.pdf")
for name, value in data.items():
doc.set_form_field(name, value)
doc.save("onboarding-filled.pdf")
导出表单数据
导出为 FDF
Python
from pdf_oxide import PdfDocument
doc = PdfDocument("filled-form.pdf")
fdf_data = doc.export_fdf()
with open("form-data.fdf", "wb") as f:
f.write(fdf_data)
WASM
const doc = new WasmPdfDocument(bytes);
const fdfData = doc.exportFormData("fdf");
// fdfData 按格式返回字符串或字节
doc.free();
Rust
use pdf_oxide::extractors::FormExtractor;
let mut doc = PdfDocument::open("filled-form.pdf")?;
let fields = FormExtractor::extract_fields(&mut doc)?;
let fdf_bytes = FormExtractor::export_fdf(&mut doc, fields)?;
std::fs::write("form-data.fdf", &fdf_bytes)?;
导出为 XFDF(XML)
Python
from pdf_oxide import PdfDocument
doc = PdfDocument("filled-form.pdf")
xfdf_data = doc.export_xfdf()
with open("form-data.xfdf", "w") as f:
f.write(xfdf_data)
WASM
const doc = new WasmPdfDocument(bytes);
const xfdfData = doc.exportFormData("xfdf");
console.log(xfdfData);
doc.free();
Rust
let mut doc = PdfDocument::open("filled-form.pdf")?;
let fields = FormExtractor::extract_fields(&mut doc)?;
let xfdf = FormExtractor::export_xfdf(&mut doc, fields)?;
std::fs::write("form-data.xfdf", &xfdf)?;
XFA 表单
部分政府和企业表单使用 XFA(XML Forms Architecture),而不是标准的 AcroForm。PDF Oxide 能检测并分析 XFA 表单:
from pdf_oxide import PdfDocument
doc = PdfDocument("government-form.pdf")
xfa = doc.has_xfa()
if xfa:
print(f"检测到 XFA 表单:{len(xfa.fields)} 个字段")
for field in xfa.fields:
print(f" {field.name} ({field.field_type})")
关于 XFA 的完整处理方式,请参见 XFA 表单指南。
加密表单
为密码保护的 PDF 填写表单:
Python
from pdf_oxide import PdfDocument
doc = PdfDocument("protected-form.pdf", password="secret")
doc.set_form_field("signature_date", "2025-01-15")
doc.save("signed.pdf")
WASM
const doc = new WasmPdfDocument(bytes);
doc.authenticate("secret");
const fields = doc.getFormFields();
console.log(fields);
doc.free();
Rust
let mut editor = DocumentEditor::open_with_password("protected-form.pdf", "secret")?;
editor.set_form_field_value("signature_date", FormFieldValue::Text("2025-01-15".into()))?;
editor.save("signed.pdf")?;
密码保护 PDF 的编辑。 Go 与 C# 的
DocumentEditor目前尚未提供在编辑器上直接认证的入口。若要在 Go 或 C# 中填写加密 PDF 的字段,请先通过只读路径PdfDocument.OpenWithPassword解密,另存一份未加密副本,再用编辑器打开该副本处理。