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 |
1 行または複数行のテキスト | "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 フォーム
行政や企業のフォームでは、標準の AcroForm の代わりに XFA(XML Forms Architecture)が使われていることがあります。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で復号し、暗号化されていないコピーを保存してから、そのコピーをエディタで開いてください。
関連ページ
- フォームデータ抽出 — フォーム抽出 API の全体像
- フォームフィールド編集 — 高度なフォーム操作
- XFA フォーム — XFA フォームの扱い方
- バッチ処理 — 並列処理のパターン