Skip to content

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 폼

정부·기업용 폼 가운데는 표준 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로 복호화한 뒤 암호가 해제된 사본을 저장하고, 그 사본을 에디터로 열어 작업하세요.

관련 페이지