PDF-Formulare ausfüllen in Python, Rust, Node.js, Go und C#
Lesen Sie Formularfelder aus und befüllen Sie sie programmgesteuert — in jedem unterstützten Binding:
Python
from pdf_oxide import PdfDocument
doc = PdfDocument("application.pdf")
# Vorhandene Felder auslesen
for field in doc.page(0).form_fields():
print(f"{field.name}: {field.value}")
# Felder befüllen und speichern
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);
// Vorhandene Felder auslesen
const fields = doc.getFormFields();
for (const field of fields) {
console.log(`${field.name}: ${field.value}`);
}
// Formulardaten als XFDF exportieren
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")?;
// Vorhandene Felder auslesen
let fields = editor.get_form_fields()?;
for field in &fields {
println!("{}: {:?}", field.name(), field.value());
}
// Felder befüllen und speichern
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()
// Vorhandene Felder auslesen
fields, _ := doc.FormFields()
for _, f := range fields {
fmt.Printf("%s: %s\n", f.Name, f.Value)
}
// Felder im Editor befüllen und speichern
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 unterstützt AcroForm-Felder (Text, Checkbox, Radiobutton, Dropdown) sowie die Analyse von XFA-Formularen. MIT-lizenziert, ohne AGPL-Einschränkungen.
Installation
pip install pdf_oxide
Formularfelder lesen
Alle Felder auflisten
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" Typ: {field.field_type}")
print(f" Wert: {field.value}")
print(f" Position: {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(` Typ: ${field.fieldType}`);
console.log(` Wert: ${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!(" Typ: {:?}", field.field_type());
println!(" Wert: {:?}", field.value());
}
Go
doc, _ := pdfoxide.Open("form.pdf")
defer doc.Close()
fields, _ := doc.FormFields()
for _, f := range fields {
fmt.Printf("Name: %s\n Typ: %s\n Wert: %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($" Typ: {f.Type}");
Console.WriteLine($" Wert: {f.Value}");
}
Feldtypen
| Typ | Beschreibung | Beispielwerte |
|---|---|---|
Text |
Ein- oder mehrzeiliger Text | "Jane Doe" |
Button |
Checkbox oder Radiobutton | True / False |
Choice |
Dropdown oder Listenfeld | "Option A" |
Signature |
Digitale Signatur | (Signaturdaten) |
Formularfelder befüllen
Textfelder
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")
Checkboxen
doc = PdfDocument("form.pdf")
doc.set_form_field("agree_terms", True)
doc.set_form_field("opt_in_newsletter", False)
doc.save("filled.pdf")
Dropdown- / Auswahlfelder
doc = PdfDocument("form.pdf")
doc.set_form_field("country", "United States")
doc.set_form_field("department", "Engineering")
doc.save("filled.pdf")
Batch-Befüllung
Aus einer CSV-Datei
Dieselbe Vorlage wird für jede Zeile einer CSV befüllt:
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")
Aus einem Dictionary
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")
Formulardaten exportieren
Export nach 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 ist je nach Format ein String oder Bytes
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)?;
Export nach 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-Formulare
Manche Formulare aus Behörden- oder Unternehmensumgebungen nutzen XFA (XML Forms Architecture) anstelle klassischer AcroForms. PDF Oxide kann XFA-Formulare erkennen und analysieren:
from pdf_oxide import PdfDocument
doc = PdfDocument("government-form.pdf")
xfa = doc.has_xfa()
if xfa:
print(f"XFA-Formular erkannt: {len(xfa.fields)} Felder")
for field in xfa.fields:
print(f" {field.name} ({field.field_type})")
Ausführliche Details zur XFA-Verarbeitung finden Sie im XFA-Formulare-Guide.
Verschlüsselte Formulare
Auch passwortgeschützte PDFs lassen sich befüllen:
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")?;
Bearbeitung passwortgeschützter PDFs. Der
DocumentEditorin Go und C# stellt derzeit keinen Einstiegspunkt zum Authentifizieren direkt am Editor bereit. Um Felder eines verschlüsselten PDFs aus Go oder C# heraus zu befüllen, entschlüsseln Sie zunächst über den schreibgeschützten PfadPdfDocument.OpenWithPassword, speichern eine unverschlüsselte Kopie und öffnen diese anschließend im Editor.
Verwandte Seiten
- Formulardatenextraktion — vollständige API zur Formularextraktion
- Formularfeld-Bearbeitung — fortgeschrittene Formularoperationen
- XFA-Formulare — Umgang mit XFA-Formularen
- Batch-Verarbeitung — Muster für parallele Verarbeitung