Python、Rust、Node.js、Go、C# でPDFフォームに入力する
すべてのサポート済みバインディングで、フォームフィールドを読み取ってプログラムから入力できます:
Python
from pdf_oxide import PdfDocument
doc = PdfDocument("application.pdf")
# Read existing fields
for field in doc.page(0).form_fields():
print(f"{field.name}: {field.value}")
# Fill fields and save
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);
// Read existing fields
const fields = doc.getFormFields();
for (const field of fields) {
console.log(`${field.name}: ${field.value}`);
}
// Export form data as 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")?;
// Read existing fields
let fields = editor.get_form_fields()?;
for field in &fields {
println!("{}: {:?}", field.name(), field.value());
}
// Fill fields and save
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()
// Read existing fields
fields, _ := doc.FormFields()
for _, f := range fields {
fmt.Printf("%s: %s\n", f.Name, f.Value)
}
// Fill fields on the editor and save
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");
Java
import fyi.oxide.pdf.PdfDocument;
import fyi.oxide.pdf.DocumentEditor;
import fyi.oxide.pdf.form.FormField;
import java.nio.file.Path;
// Read existing fields
try (PdfDocument doc = PdfDocument.open(Path.of("application.pdf"))) {
for (FormField field : doc.formFields()) {
System.out.println(field.name() + ": " + field.value().orElse(""));
}
}
// Fill fields and save
try (DocumentEditor editor = DocumentEditor.open("application.pdf")) {
editor.setFormField("full_name", "Jane Doe");
editor.setFormField("email", "jane@example.com");
editor.setFormField("agree_terms", true);
editor.saveTo(Path.of("filled-application.pdf"));
}
Kotlin
import fyi.oxide.pdf.PdfDocument
import fyi.oxide.pdf.DocumentEditor
// Read existing fields
PdfDocument.open("application.pdf").use { doc ->
for (field in doc.formFields()) {
println("${field.name()}: ${field.value().orElse("")}")
}
}
// Fill fields and save
DocumentEditor.open("application.pdf").use { editor ->
editor.setFormField("full_name", "Jane Doe")
editor.setFormField("email", "jane@example.com")
editor.setFormField("agree_terms", true)
editor.saveTo(java.nio.file.Path.of("filled-application.pdf"))
}
Scala
import fyi.oxide.pdf.{PdfDocument, DocumentEditor, formFieldsSeq}
import scala.util.Using
// Read existing fields
Using.resource(PdfDocument.open("application.pdf")) { doc =>
doc.formFieldsSeq.foreach { f =>
println(s"${f.name()}: ${f.value().orElse("")}")
}
}
// Fill fields and save
Using.resource(DocumentEditor.open("application.pdf")) { editor =>
editor.setFormField("full_name", "Jane Doe")
editor.setFormField("email", "jane@example.com")
editor.setFormField("agree_terms", true)
editor.saveTo(java.nio.file.Path.of("filled-application.pdf"))
}
Clojure
(require '[pdf-oxide.core :as pdf])
;; Read existing fields
(with-open [doc (pdf/open "application.pdf")]
(doseq [f (pdf/form-fields doc)]
(println (str (.name f) ": " (.orElse (.value f) "")))))
;; Fill fields and save
(with-open [ed (pdf/editor "application.pdf")]
(.setFormField ed "full_name" "Jane Doe")
(.setFormField ed "email" "jane@example.com")
(.setFormField ed "agree_terms" true)
(clojure.java.io/copy (.save ed) (clojure.java.io/file "filled-application.pdf")))
Ruby
require 'pdf_oxide'
# Read existing fields
PdfOxide::PdfDocument.open('application.pdf') do |doc|
doc.form_fields.each do |f|
puts "#{f[:name]}: #{f[:value]}"
end
end
# Fill fields and save
PdfOxide::DocumentEditor.open('application.pdf') do |editor|
editor.set_form_field('full_name', 'Jane Doe')
editor.set_form_field('email', 'jane@example.com')
editor.set_form_field('agree_terms', 'Yes')
editor.save_to('filled-application.pdf')
end
C++
#include <pdf_oxide/pdf_oxide.hpp>
// Read existing fields
auto doc = pdf_oxide::Document::open("application.pdf");
for (const auto& field : doc.get_form_fields()) {
std::cout << field.name << ": " << field.value << "\n";
}
// Fill fields and save
auto editor = pdf_oxide::DocumentEditor::open("application.pdf");
editor.set_form_field_value("full_name", "Jane Doe");
editor.set_form_field_value("email", "jane@example.com");
editor.set_form_field_value("agree_terms", "Yes");
editor.save("filled-application.pdf");
Swift
import PdfOxide
// Read existing fields
let doc = try Document.open("application.pdf")
for field in try doc.formFields() {
print("\(field.name): \(field.value)")
}
// Fill fields and save
let editor = try DocumentEditor.open("application.pdf")
try editor.setFormFieldValue("full_name", "Jane Doe")
try editor.setFormFieldValue("email", "jane@example.com")
try editor.setFormFieldValue("agree_terms", "Yes")
try editor.save("filled-application.pdf")
Dart
import 'package:pdf_oxide/pdf_oxide.dart';
// Read existing fields
final doc = PdfDocument.open('application.pdf');
for (final field in doc.getFormFields()) {
print('${field.name}: ${field.value}');
}
// Fill fields and save
final 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');
R
library(pdfoxide)
# Read existing fields
doc <- pdf_open("application.pdf")
for (field in pdf_get_form_fields(doc)) {
cat(sprintf("%s: %s\n", field$name, field$value))
}
# Fill fields and save
editor <- pdf_editor_open("application.pdf")
pdf_editor_set_form_field_value(editor, "full_name", "Jane Doe")
pdf_editor_set_form_field_value(editor, "email", "jane@example.com")
pdf_editor_set_form_field_value(editor, "agree_terms", "Yes")
pdf_editor_save(editor, "filled-application.pdf")
Julia
using PdfOxide
# Read existing fields
doc = open_document("application.pdf")
for field in get_form_fields(doc)
println("$(field.name): $(field.value)")
end
# Fill fields and save
editor = open_editor("application.pdf")
set_form_field_value(editor, "full_name", "Jane Doe")
set_form_field_value(editor, "email", "jane@example.com")
set_form_field_value(editor, "agree_terms", "Yes")
save(editor, "filled-application.pdf")
Zig
const pdf_oxide = @import("pdf_oxide");
// Read existing fields
var doc = try pdf_oxide.Document.open("application.pdf");
const fields = try doc.formFields();
// iterate the FormFieldList for name/value
// Fill fields and save
var editor = try pdf_oxide.DocumentEditor.openEditor("application.pdf");
try editor.setFormFieldValue("full_name", "Jane Doe");
try editor.setFormFieldValue("email", "jane@example.com");
try editor.setFormFieldValue("agree_terms", "Yes");
try editor.save("filled-application.pdf");
Objective-C
#import "POXPdfOxide.h"
NSError *err = nil;
// Read existing fields
POXDocument *doc = [POXDocument openPath:@"application.pdf" error:&err];
for (POXFormField *field in [doc formFieldsWithError:&err]) {
NSLog(@"%@: %@", field.name, field.value);
}
// Fill fields and save
POXDocumentEditor *editor = [POXDocumentEditor openEditor:@"application.pdf" error:&err];
[editor setFormField:@"full_name" value:@"Jane Doe" error:&err];
[editor setFormField:@"email" value:@"jane@example.com" error:&err];
[editor setFormField:@"agree_terms" value:@"Yes" error:&err];
[editor saveToPath:@"filled-application.pdf" error:&err];
Elixir
# Read existing fields
{:ok, doc} = PdfOxide.open("application.pdf")
{:ok, fields} = PdfOxide.form_fields(doc)
Enum.each(fields, fn f -> IO.puts("#{f.name}: #{f.value}") end)
# Fill fields and save
{:ok, editor} = PdfOxide.open_editor("application.pdf")
PdfOxide.set_form_field_value(editor, "full_name", "Jane Doe")
PdfOxide.set_form_field_value(editor, "email", "jane@example.com")
PdfOxide.set_form_field_value(editor, "agree_terms", "Yes")
PdfOxide.editor_save(editor, "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"Name: {field.name}")
print(f" Type: {field.field_type}")
print(f" Value: {field.value}")
print(f" Bounds: {field.bounds}")
print()
WASM
const doc = new WasmPdfDocument(bytes);
const fields = doc.getFormFields();
for (const field of fields) {
console.log(`Name: ${field.name}`);
console.log(` Type: ${field.fieldType}`);
console.log(` Value: ${field.value}`);
}
doc.free();
Rust
let mut editor = DocumentEditor::open("form.pdf")?;
let fields = editor.get_form_fields()?;
for field in &fields {
println!("Name: {}", field.name());
println!(" Type: {:?}", field.field_type());
println!(" Value: {:?}", field.value());
}
Go
doc, _ := pdfoxide.Open("form.pdf")
defer doc.Close()
fields, _ := doc.FormFields()
for _, f := range fields {
fmt.Printf("Name: %s\n Type: %s\n Value: %s\n", f.Name, f.Type, f.Value)
}
C#
using var doc = PdfDocument.Open("form.pdf");
foreach (var f in doc.GetFormFields())
{
Console.WriteLine($"Name: {f.Name}");
Console.WriteLine($" Type: {f.Type}");
Console.WriteLine($" Value: {f.Value}");
}
Java
try (PdfDocument doc = PdfDocument.open("form.pdf")) {
for (FormField f : doc.formFields()) {
System.out.println("Name: " + f.name());
System.out.println(" Type: " + f.type());
System.out.println(" Value: " + f.value().orElse(""));
}
}
Kotlin
PdfDocument.open("form.pdf").use { doc ->
for (f in doc.formFields()) {
println("Name: ${f.name()}")
println(" Type: ${f.type()}")
println(" Value: ${f.value().orElse("")}")
}
}
Scala
Using.resource(PdfDocument.open("form.pdf")) { doc =>
doc.formFieldsSeq.foreach { f =>
println(s"Name: ${f.name()}")
println(s" Type: ${f.type()}")
println(s" Value: ${f.value().orElse("")}")
}
}
Clojure
(with-open [doc (pdf/open "form.pdf")]
(doseq [f (pdf/form-fields doc)]
(println "Name:" (.name f))
(println " Type:" (.type f))
(println " Value:" (.orElse (.value f) ""))))
Ruby
PdfOxide::PdfDocument.open('form.pdf') do |doc|
doc.form_fields.each do |f|
puts "Name: #{f[:name]}"
puts " Type: #{f[:type]}"
puts " Value: #{f[:value]}"
end
end
C++
auto doc = pdf_oxide::Document::open("form.pdf");
for (const auto& f : doc.get_form_fields()) {
std::cout << "Name: " << f.name << "\n";
std::cout << " Type: " << f.type << "\n";
std::cout << " Value: " << f.value << "\n";
}
Swift
let doc = try Document.open("form.pdf")
for f in try doc.formFields() {
print("Name: \(f.name)")
print(" Type: \(f.type)")
print(" Value: \(f.value)")
}
Dart
final doc = PdfDocument.open('form.pdf');
for (final f in doc.getFormFields()) {
print('Name: ${f.name}');
print(' Type: ${f.type}');
print(' Value: ${f.value}');
}
R
doc <- pdf_open("form.pdf")
for (f in pdf_get_form_fields(doc)) {
cat(sprintf("Name: %s\n Type: %s\n Value: %s\n", f$name, f$type, f$value))
}
Julia
doc = open_document("form.pdf")
for f in get_form_fields(doc)
println("Name: $(f.name)")
println(" Type: $(f.type)")
println(" Value: $(f.value)")
end
Zig
var doc = try pdf_oxide.Document.open("form.pdf");
const fields = try doc.formFields();
// iterate the FormFieldList for each field's name, type, and value
Objective-C
POXDocument *doc = [POXDocument openPath:@"form.pdf" error:&err];
for (POXFormField *f in [doc formFieldsWithError:&err]) {
NSLog(@"Name: %@", f.name);
NSLog(@" Type: %@", f.type);
NSLog(@" Value: %@", f.value);
}
Elixir
{:ok, doc} = PdfOxide.open("form.pdf")
{:ok, fields} = PdfOxide.form_fields(doc)
Enum.each(fields, fn f ->
IO.puts("Name: #{f.name}")
IO.puts(" Type: #{f.type}")
IO.puts(" Value: #{f.value}")
end)
フィールドの種類
| 種類 | 説明 | 値の例 |
|---|---|---|
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 is a string or bytes depending on format
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)?;
C++
auto doc = pdf_oxide::Document::open("filled-form.pdf");
// format_type 0 = FDF
auto fdf = doc.export_form_data_to_bytes(0);
std::ofstream("form-data.fdf", std::ios::binary)
.write(reinterpret_cast<const char*>(fdf.data()), fdf.size());
Swift
let doc = try Document.open("filled-form.pdf")
// format_type 0 = FDF
let fdf = try doc.exportFormData(formatType: 0)
try Data(fdf).write(to: URL(fileURLWithPath: "form-data.fdf"))
Dart
final doc = PdfDocument.open('filled-form.pdf');
// format type 0 = FDF
final fdf = doc.exportFormData(0);
File('form-data.fdf').writeAsBytesSync(fdf);
R
doc <- pdf_open("filled-form.pdf")
# format_type 0 = FDF
fdf <- pdf_export_form_data_to_bytes(doc, 0L)
writeBin(fdf, "form-data.fdf")
Julia
doc = open_document("filled-form.pdf")
# format_type 0 = FDF
fdf = export_form_data_to_bytes(doc, 0)
write("form-data.fdf", fdf)
Zig
var doc = try pdf_oxide.Document.open("filled-form.pdf");
const a = std.heap.page_allocator;
// format_type 0 = FDF
const fdf = try doc.exportFormDataToBytes(a, 0);
defer a.free(fdf);
try std.fs.cwd().writeFile(.{ .sub_path = "form-data.fdf", .data = fdf });
Objective-C
POXDocument *doc = [POXDocument openPath:@"filled-form.pdf" error:&err];
// format type 0 = FDF
NSData *fdf = [doc exportFormDataToBytes:0 error:&err];
[fdf writeToFile:@"form-data.fdf" atomically:YES];
Elixir
{:ok, doc} = PdfOxide.open("filled-form.pdf")
# format_type 0 = FDF
{:ok, fdf} = PdfOxide.export_form_data_to_bytes(doc, 0)
File.write!("form-data.fdf", fdf)
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)?;
C++
auto doc = pdf_oxide::Document::open("filled-form.pdf");
// format_type 1 = XFDF
auto xfdf = doc.export_form_data_to_bytes(1);
std::ofstream("form-data.xfdf", std::ios::binary)
.write(reinterpret_cast<const char*>(xfdf.data()), xfdf.size());
Swift
let doc = try Document.open("filled-form.pdf")
// format_type 1 = XFDF
let xfdf = try doc.exportFormData(formatType: 1)
try Data(xfdf).write(to: URL(fileURLWithPath: "form-data.xfdf"))
Dart
final doc = PdfDocument.open('filled-form.pdf');
// format type 1 = XFDF
final xfdf = doc.exportFormData(1);
File('form-data.xfdf').writeAsBytesSync(xfdf);
R
doc <- pdf_open("filled-form.pdf")
# format_type 1 = XFDF
xfdf <- pdf_export_form_data_to_bytes(doc, 1L)
writeBin(xfdf, "form-data.xfdf")
Julia
doc = open_document("filled-form.pdf")
# format_type 1 = XFDF
xfdf = export_form_data_to_bytes(doc, 1)
write("form-data.xfdf", xfdf)
Zig
var doc = try pdf_oxide.Document.open("filled-form.pdf");
const a = std.heap.page_allocator;
// format_type 1 = XFDF
const xfdf = try doc.exportFormDataToBytes(a, 1);
defer a.free(xfdf);
try std.fs.cwd().writeFile(.{ .sub_path = "form-data.xfdf", .data = xfdf });
Objective-C
POXDocument *doc = [POXDocument openPath:@"filled-form.pdf" error:&err];
// format type 1 = XFDF
NSData *xfdf = [doc exportFormDataToBytes:1 error:&err];
[xfdf writeToFile:@"form-data.xfdf" atomically:YES];
Elixir
{:ok, doc} = PdfOxide.open("filled-form.pdf")
# format_type 1 = XFDF
{:ok, xfdf} = PdfOxide.export_form_data_to_bytes(doc, 1)
File.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 form detected: {len(xfa.fields)} 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")?;
パスワード保護された編集機能について。 GoとC#の
DocumentEditorには、現時点でエディター起動時の認証エントリーポイントが存在しません。GoまたはC#で暗号化されたPDFのフォームに入力するには、まず読み取り専用のPdfDocument.OpenWithPasswordで復号し、暗号化されていないコピーを保存してから、そのコピーをエディターで開いてください。
関連ページ
- フォームデータの抽出 — フォーム抽出APIの完全ガイド
- フォームフィールドの編集 — 高度なフォーム操作
- XFAフォーム — XFAフォームの処理
- バッチ処理 — 並列処理パターン