フォームフィールド編集
PDF Oxide は包括的なフォームフィールドサポートを提供します: 既存の値の読み取り、プログラムによるフィールド入力、新しいフィールドの追加、プロパティの設定、フォームの静的コンテンツへのフラット化、FDF/XFDF形式でのフォームデータのエクスポート。XFAフォームは分析してAcroFormに変換できます。
フォームフィールドの読み取り
全フォームフィールドの取得
Rust
use pdf_oxide::editor::DocumentEditor;
let mut editor = DocumentEditor::open("form.pdf")?;
let fields = editor.get_form_fields()?;
for field in &fields {
println!("Field: {} = {:?}", field.name(), field.value());
if let Some(ft) = field.field_type() {
println!(" Type: {:?}", ft);
}
if let Some(tooltip) = field.tooltip() {
println!(" Tooltip: {}", tooltip);
}
}
WASM
const doc = new WasmPdfDocument(bytes);
const fields = doc.getFormFields();
for (const f of fields) {
console.log(`${f.name} (${f.field_type}) = ${f.value}`);
}
Python
from pdf_oxide import PdfDocument
doc = PdfDocument("form.pdf")
fields = doc.get_form_fields()
for field in fields:
print(f"{field.name} ({field.field_type}) = {field.value}")
特定フィールドの値を取得
Rust
let mut editor = DocumentEditor::open("form.pdf")?;
let value = editor.get_form_field_value("first_name")?;
println!("First name: {:?}", value);
WASM
const value = doc.getFormFieldValue("first_name");
console.log(`First name: ${value}`);
Python
value = doc.get_form_field_value("first_name")
print(f"First name: {value}")
フィールドの存在確認
let mut editor = DocumentEditor::open("form.pdf")?;
if editor.has_form_field("email")? {
println!("Email field exists");
}
フォームフィールドの値を設定
フィールドの値を設定
Rust
use pdf_oxide::editor::DocumentEditor;
use pdf_oxide::editor::form_fields::FormFieldValue;
let mut editor = DocumentEditor::open("form.pdf")?;
// Set a text field
editor.set_form_field_value("first_name", FormFieldValue::Text("Jane".to_string()))?;
// Set a checkbox
editor.set_form_field_value("agree_terms", FormFieldValue::Boolean(true))?;
// Set a choice field
editor.set_form_field_value("country", FormFieldValue::Choice("United States".to_string()))?;
editor.save("filled.pdf")?;
WASM
const doc = new WasmPdfDocument(bytes);
doc.setFormFieldValue("first_name", "Jane");
doc.setFormFieldValue("agree_terms", true);
doc.setFormFieldValue("country", "United States");
writeFileSync("filled.pdf", doc.save());
doc.free();
Python
doc = PdfDocument("form.pdf")
doc.set_form_field_value("first_name", "Jane")
doc.set_form_field_value("agree_terms", True)
doc.set_form_field_value("country", "United States")
doc.save("filled.pdf")
FormFieldValue Variants
| Variant | 説明 | Example |
|---|---|---|
Text(String) |
テキストフィールド値 | FormFieldValue::Text("Hello".into()) |
Boolean(bool) |
チェックボックス/ラジオの状態 | FormFieldValue::Boolean(true) |
Choice(String) |
単一選択 | FormFieldValue::Choice("Option A".into()) |
MultiChoice(Vec<String>) |
複数選択 | FormFieldValue::MultiChoice(vec!["A".into(), "B".into()]) |
None |
値なし/フィールドをクリア | FormFieldValue::None |
フォームフィールドの追加
新しいフォームフィールドを追加
use pdf_oxide::editor::DocumentEditor;
use pdf_oxide::writer::form_fields::TextFieldWidget;
let mut editor = DocumentEditor::open("document.pdf")?;
// Create a text input field on page 0
let widget = TextFieldWidget::new("user_name")
.with_rect(100.0, 700.0, 200.0, 20.0)
.with_default_value("Enter name");
editor.add_form_field(widget, 0)?;
editor.save("with-form.pdf")?;
階層フィールドの追加
構造化フォームの親子フィールド関係を作成します。
use pdf_oxide::editor::form_fields::ParentFieldConfig;
let mut editor = DocumentEditor::open("document.pdf")?;
// Create a parent field
let parent = ParentFieldConfig::new("address");
editor.add_parent_field(parent)?;
// Add child fields under the parent
let street = TextFieldWidget::new("street")
.with_rect(100.0, 600.0, 300.0, 20.0);
editor.add_child_field(street, 0, "address")?;
let city = TextFieldWidget::new("city")
.with_rect(100.0, 570.0, 150.0, 20.0);
editor.add_child_field(city, 0, "address")?;
editor.save("hierarchical-form.pdf")?;
フォームフィールドの削除
let mut editor = DocumentEditor::open("form.pdf")?;
editor.remove_form_field("obsolete_field")?;
editor.save("cleaned.pdf")?;
フォームフィールドのプロパティ
プロパティの設定
名前で個別フィールドプロパティを設定します。
let mut editor = DocumentEditor::open("form.pdf")?;
// Access control
editor.set_form_field_readonly("signature_date", true)?;
editor.set_form_field_required("email", true)?;
// Tooltip
editor.set_form_field_tooltip("phone", "Enter phone number with area code")?;
// 位置とサイズ
editor.set_form_field_rect("name", pdf_oxide::geometry::Rect::new(100.0, 700.0, 200.0, 20.0))?;
// Text constraints
editor.set_form_field_max_length("zip_code", 10)?;
editor.set_form_field_alignment("amount", 2)?; // 0=left, 1=center, 2=right
// Appearance
editor.set_form_field_background_color("highlight_field", [1.0, 1.0, 0.8])?;
editor.set_form_field_border_color("name", [0.0, 0.0, 0.0])?;
editor.set_form_field_border_width("name", 1.0)?;
editor.set_form_field_default_appearance("name", "/Helv 12 Tf 0 g")?;
// Raw flags
editor.set_form_field_flags("options", 0x100000)?;
editor.save("styled-form.pdf")?;
FormFieldWrapper Properties
get_form_fields() で返される FormFieldWrapper オブジェクトの操作時:
| Method | Returns | 説明 |
|---|---|---|
name() |
&str |
完全フィールド名 |
partial_name() |
&str |
部分名(親プレフィックスなし) |
value() |
FormFieldValue |
現在のフィールド値 |
set_value(value) |
() |
フィールド値を設定 |
field_type() |
Option<&FieldType> |
フィールドタイプ |
page_index() |
usize |
フィールドを含むページ |
bounds() |
Option<Rect> |
フィールド位置とサイズ |
tooltip() |
Option<&str> |
ツールチップテキスト |
is_modified() |
bool |
値が変更されたか |
is_new() |
bool |
フィールドが追加されたか(ソースからではない) |
is_readonly() |
bool |
読み取り専用フラグ |
set_readonly(bool) |
() |
読み取り専用を設定 flag |
is_required() |
bool |
必須フラグ |
set_required(bool) |
() |
必須を設定 flag |
is_no_export() |
bool |
エクスポート不可フラグ |
set_no_export(bool) |
() |
エクスポート不可フラグ設定 |
set_tooltip(text) |
() |
ツールチップを設定 text |
set_rect(rect) |
() |
位置とサイズを設定 |
set_max_length(len) |
() |
Set maximum text length |
get_max_length() |
Option<u32> |
テキスト最大長取得 |
set_alignment(align) |
() |
テキストの配置を設定 |
get_alignment() |
Option<u32> |
テキスト配置取得 |
set_background_color(rgb) |
() |
背景色を設定 |
get_background_color() |
Option<[f32; 3]> |
背景色取得 |
set_border_color(rgb) |
() |
ボーダーの色を設定 |
get_border_color() |
Option<[f32; 3]> |
ボーダー色取得 |
set_border_width(width) |
() |
ボーダーの幅を設定 |
get_border_width() |
Option<f32> |
ボーダー幅取得 |
set_default_appearance(da) |
() |
デフォルトの外観を設定 string |
get_default_appearance() |
Option<&str> |
デフォルト外観文字列取得 |
set_default_value(value) |
() |
デフォルト値設定 |
get_default_value() |
Option<&FormFieldValue> |
デフォルト値取得 |
has_parent() |
bool |
親フィールド確認 |
parent_name() |
Option<&str> |
親フィールド名 |
フォームのフラット化
Flattening converts インタラクティブフォームフィールド into static page content. The field values become part of the page drawing and can no longer be edited.
単一ページのフラット化
Rust
let mut editor = DocumentEditor::open("form.pdf")?;
editor.flatten_forms_on_page(0)?;
editor.save("flat-page0.pdf")?;
WASM
const doc = new WasmPdfDocument(bytes);
doc.flattenFormsOnPage(0);
writeFileSync("flat-page0.pdf", doc.save());
doc.free();
全フォームのフラット化
Rust
let mut editor = DocumentEditor::open("form.pdf")?;
editor.flatten_forms()?;
editor.save("flat.pdf")?;
WASM
const doc = new WasmPdfDocument(bytes);
doc.flattenForms();
writeFileSync("flat.pdf", doc.save());
doc.free();
Check Flatten Status
editor.flatten_forms_on_page(0)?;
assert!(editor.is_page_marked_for_form_flatten(0));
assert!(editor.will_remove_acroform());
フォームデータのエクスポート
Export form field values to FDF or XFDF format for external processing.
FDF へのエクスポート
Rust
let mut editor = DocumentEditor::open("filled-form.pdf")?;
editor.export_form_data_fdf("form-data.fdf")?;
WASM
const doc = new WasmPdfDocument(bytes);
const fdfData = doc.exportFormData("fdf");
writeFileSync("form-data.fdf", fdfData);
doc.free();
Python
doc = PdfDocument("filled-form.pdf")
doc.export_form_data("form-data.fdf", format="fdf")
XFDF へのエクスポート
Rust
let mut editor = DocumentEditor::open("filled-form.pdf")?;
editor.export_form_data_xfdf("form-data.xfdf")?;
WASM
const doc = new WasmPdfDocument(bytes);
const xfdfData = doc.exportFormData("xfdf");
writeFileSync("form-data.xfdf", xfdfData);
doc.free();
XFA フォームのサポート
PDF Oxide can detect, analyze, and convert XFA forms to standard AcroForm.
XFA の確認
Rust
let mut editor = DocumentEditor::open("xfa-form.pdf")?;
if editor.has_xfa()? {
println!("Document contains XFA form data");
}
WASM
const doc = new WasmPdfDocument(bytes);
if (doc.hasXfa()) {
console.log("Document contains XFA form data");
}
Python
doc = PdfDocument("xfa-form.pdf")
if doc.has_xfa():
print("Document contains XFA form data")
XFA 構造の分析
let mut editor = DocumentEditor::open("xfa-form.pdf")?;
let analysis = editor.analyze_xfa()?;
println!("XFA analysis: {:?}", analysis);
XFA から AcroForm への変換
Convert XFA forms to standard AcroForm fields for broader compatibility.
let mut editor = DocumentEditor::open("xfa-form.pdf")?;
editor.convert_xfa_to_acroform(&Default::default())?;
editor.save("acroform.pdf")?;
完全な API リファレンス
フィールド操作
| Method | Returns | 説明 |
|---|---|---|
get_form_fields() |
Result<Vec<FormFieldWrapper>> |
すべてのフォームフィールドを一覧 |
get_form_field_value(name) |
Result<FormFieldValue> |
Get a field’s value |
has_form_field(name) |
Result<bool> |
フィールドが存在するか確認 |
set_form_field_value(name, value) |
Result<()> |
Set a field’s value |
add_form_field(widget, page) |
Result<()> |
新しいフィールドを追加 |
add_parent_field(config) |
Result<()> |
親フィールドを追加 |
add_child_field(widget, page, parent) |
Result<()> |
子フィールドを追加 |
remove_form_field(name) |
Result<()> |
フィールドを削除 |
フィールドプロパティ(名前別)
| Method | Returns | 説明 |
|---|---|---|
set_form_field_readonly(name, bool) |
Result<()> |
読み取り専用を設定 |
set_form_field_required(name, bool) |
Result<()> |
必須を設定 |
set_form_field_tooltip(name, text) |
Result<()> |
ツールチップを設定 |
set_form_field_rect(name, rect) |
Result<()> |
位置/サイズを設定 |
set_form_field_max_length(name, len) |
Result<()> |
テキストの最大長を設定 |
set_form_field_alignment(name, align) |
Result<()> |
テキストの配置を設定 |
set_form_field_background_color(name, rgb) |
Result<()> |
背景色を設定 |
set_form_field_border_color(name, rgb) |
Result<()> |
ボーダーの色を設定 |
set_form_field_border_width(name, width) |
Result<()> |
ボーダーの幅を設定 |
set_form_field_default_appearance(name, da) |
Result<()> |
デフォルトの外観を設定 |
set_form_field_flags(name, flags) |
Result<()> |
生のフィールドフラグを設定 |
フラット化
| Method | Returns | 説明 |
|---|---|---|
flatten_forms_on_page(page) |
Result<()> |
1ページのフォームをフラット化 |
flatten_forms() |
Result<()> |
全フォームをフラット化 |
is_page_marked_for_form_flatten(page) |
bool |
フラット化ステータスを確認 |
will_remove_acroform() |
bool |
AcroFormが削除されるか確認 |
エクスポート
| Method | Returns | 説明 |
|---|---|---|
export_form_data_fdf(path) |
Result<()> |
FDFファイルにエクスポート |
export_form_data_xfdf(path) |
Result<()> |
XFDFファイルにエクスポート |
XFA
| Method | Returns | 説明 |
|---|---|---|
has_xfa() |
Result<bool> |
XFAフォームデータを確認 |
analyze_xfa() |
Result<XfaAnalysis> |
XFA構造を分析 |
convert_xfa_to_acroform(options) |
Result<()> |
XFAをAcroFormに変換 |
応用例: Batch Fill and Flatten
use pdf_oxide::editor::DocumentEditor;
use pdf_oxide::editor::form_fields::FormFieldValue;
let mut editor = DocumentEditor::open("template.pdf")?;
// Fill form fields
editor.set_form_field_value("name", FormFieldValue::Text("John Doe".to_string()))?;
editor.set_form_field_value("date", FormFieldValue::Text("2025-12-01".to_string()))?;
editor.set_form_field_value("approved", FormFieldValue::Boolean(true))?;
// Make the completed form non-editable
editor.flatten_forms()?;
// Export data before saving
editor.export_form_data_xfdf("submission.xfdf")?;
editor.save("completed.pdf")?;
関連ページ
- Editing Overview – 開く、メタデータ、保存のワークフロー
- Text Editing – テキストコンテンツを直接変更
- Annotation Editing – アノテーションの追加と管理
- Encryption & Security – 権限でフォーム編集を制限