양식 필드 생성
PDF 문서에 대화형 양식 필드를 추가합니다. PDF Oxide는 텍스트 필드, 체크박스, 라디오 버튼 그룹, 콤보 박스(드롭다운), 목록 박스, 푸시 버튼 등 모든 표준 AcroForm 위젯 타입을 지원합니다.
바인딩 커버리지 (v0.3.38).
FluentPageBuilder는 Rust, Python, Node/TypeScript, C#, Go, WASM에서text_field,checkbox,combo_box,radio_group,push_button을 제공합니다 (DocumentBuilder → AcroForm 위젯 참고). 이 페이지에서 다루는 Rust 전용PdfWriter::text_field/checkbox등의 하위 API는 위젯별 세부 옵션 (여러 줄 텍스트, 비밀번호 필드, 리스트 박스, 테두리 색상)을 제공합니다. 세밀한 AcroForm 제어가 필요할 때 사용하세요. 기존 폼의 값 채우기와 평탄화(flatten) 는 모든 바인딩에서 동작합니다 — 폼 필드 편집과 PDF 폼 채우기를 참고하세요.
빠른 예제
Rust
use pdf_oxide::writer::{PdfWriter, TextFieldWidget, CheckboxWidget};
use pdf_oxide::geometry::Rect;
let mut writer = PdfWriter::new();
{
let mut page = writer.add_letter_page();
page.add_text("Name:", 72.0, 720.0, "Helvetica", 12.0);
page.text_field("name", Rect::new(130.0, 716.0, 200.0, 20.0));
page.add_text("I agree to terms:", 72.0, 690.0, "Helvetica", 12.0);
page.checkbox("agree", Rect::new(200.0, 686.0, 15.0, 15.0));
page.finish();
}
writer.save("form.pdf")?;
Python
from pdf_oxide import DocumentBuilder
(DocumentBuilder()
.letter_page()
.at(72, 720).text("Name:")
.text_field("name", 130, 716, 200, 20)
.at(72, 690).text("I agree to terms:")
.checkbox("agree", 200, 686, 15, 15, False)
.done()
.save("form.pdf"))
Node / TypeScript
import { DocumentBuilder } from "pdf-oxide";
await new DocumentBuilder()
.letterPage()
.at(72, 720).text("Name:")
.textField("name", 130, 716, 200, 20)
.at(72, 690).text("I agree to terms:")
.checkbox("agree", 200, 686, 15, 15, false)
.done()
.save("form.pdf");
C#
DocumentBuilder.Create()
.LetterPage()
.At(72, 720).Text("Name:")
.TextField("name", 130, 716, 200, 20)
.At(72, 690).Text("I agree to terms:")
.Checkbox("agree", 200, 686, 15, 15, false)
.Done()
.Save("form.pdf");
Go
builder := pdfoxide.NewDocumentBuilder()
builder.LetterPage().
At(72, 720).Text("Name:").
TextField("name", 130, 716, 200, 20, "").
At(72, 690).Text("I agree to terms:").
Checkbox("agree", 200, 686, 15, 15, false).
Done()
_ = builder.Save("form.pdf")
위젯 타입
TextFieldWidget – Text Input
Single-line or multi-line text input fields.
use pdf_oxide::writer::TextFieldWidget;
use pdf_oxide::geometry::Rect;
// Basic text field
let field = TextFieldWidget::new("username", Rect::new(72.0, 700.0, 200.0, 20.0));
// Fully configured text field
let field = TextFieldWidget::new("email", Rect::new(72.0, 670.0, 250.0, 20.0))
.with_value("user@example.com")
.with_default_value("")
.with_max_length(100)
.required()
.with_tooltip("Enter your email address")
.with_font("Helv", 12.0)
.with_text_color(0.0, 0.0, 0.0)
.with_border_color(0.5, 0.5, 0.5)
.with_background_color(1.0, 1.0, 0.95);
// Password field (characters are masked)
let password = TextFieldWidget::new("password", Rect::new(72.0, 640.0, 200.0, 20.0))
.password();
// Multi-line text area
let notes = TextFieldWidget::new("notes", Rect::new(72.0, 550.0, 300.0, 80.0))
.multiline();
주요 메서드:
| 메서드 | 설명 |
|---|---|
.with_value(s) |
현재 텍스트 값 설정 |
.with_default_value(s) |
재설정 값 설정 |
.with_max_length(n) |
최대 문자 수 |
.required() |
Mark as required field |
.read_only() |
편집 방지 |
.password() |
입력 문자 마스크 |
.multiline() |
여러 줄 허용 |
.with_tooltip(s) |
Hover tooltip text |
.with_font(name, size) |
Set font and size |
.with_text_color(r, g, b) |
Set text color (0.0-1.0 RGB) |
.with_border_color(r, g, b) |
Set border color |
.with_background_color(r, g, b) |
Set background color |
CheckboxWidget – Checkbox
Toggle fields with on/off states.
use pdf_oxide::writer::CheckboxWidget;
use pdf_oxide::geometry::Rect;
// Basic checkbox
let cb = CheckboxWidget::new("agree", Rect::new(72.0, 700.0, 15.0, 15.0));
// Pre-checked checkbox with custom export value
let cb = CheckboxWidget::new("newsletter", Rect::new(72.0, 680.0, 15.0, 15.0))
.checked()
.with_export_value("subscribed")
.with_tooltip("Subscribe to newsletter");
주요 메서드:
| 메서드 | 설명 |
|---|---|
.checked() |
Set initial state to checked |
.with_export_value(s) |
Value sent on form submission |
.with_tooltip(s) |
Hover tooltip text |
.read_only() |
Prevent toggling |
RadioButtonGroup – Radio Buttons
Mutually exclusive option groups. All buttons in a group share the same field name; selecting one deselects the others.
use pdf_oxide::writer::RadioButtonGroup;
use pdf_oxide::geometry::Rect;
let group = RadioButtonGroup::new("color")
.add_button("Red", Rect::new(72.0, 700.0, 15.0, 15.0))
.add_button("Green", Rect::new(72.0, 680.0, 15.0, 15.0))
.add_button("Blue", Rect::new(72.0, 660.0, 15.0, 15.0))
.with_selected("Green"); // Pre-select "Green"
주요 메서드:
| 메서드 | 설명 |
|---|---|
.add_button(value, rect) |
Add a radio option |
.with_selected(value) |
Pre-select an option |
.no_toggle_off() |
Prevent deselecting all options |
ComboBoxWidget – Dropdown
Drop-down selection fields with optional editable text.
use pdf_oxide::writer::ComboBoxWidget;
use pdf_oxide::writer::form_fields::ChoiceOption;
use pdf_oxide::geometry::Rect;
let combo = ComboBoxWidget::new("country", Rect::new(72.0, 700.0, 200.0, 20.0))
.add_option("US", "United States")
.add_option("GB", "United Kingdom")
.add_option("DE", "Germany")
.add_option("JP", "Japan")
.with_selected("US");
주요 메서드:
| 메서드 | 설명 |
|---|---|
.add_option(value, label) |
Add a selectable option |
.with_selected(value) |
Pre-select an option |
.editable() |
Allow typing custom values |
.sorted() |
Sort options alphabetically |
ListBoxWidget – Multi-Select List
Scrollable list fields with single or multiple selection.
use pdf_oxide::writer::ListBoxWidget;
use pdf_oxide::geometry::Rect;
let list = ListBoxWidget::new("languages", Rect::new(72.0, 600.0, 200.0, 80.0))
.add_option("rust", "Rust")
.add_option("python", "Python")
.add_option("go", "Go")
.add_option("typescript", "TypeScript")
.multi_select()
.with_selected("rust");
주요 메서드:
| 메서드 | 설명 |
|---|---|
.add_option(value, label) |
Add a selectable option |
.with_selected(value) |
Pre-select an option |
.multi_select() |
Allow selecting multiple items |
.sorted() |
Sort options alphabetically |
PushButtonWidget – Button
Clickable buttons that trigger actions (submit, reset, or JavaScript).
use pdf_oxide::writer::PushButtonWidget;
use pdf_oxide::geometry::Rect;
let submit = PushButtonWidget::new("submit", Rect::new(72.0, 500.0, 100.0, 30.0))
.with_label("Submit")
.submit_form("https://example.com/submit");
let reset = PushButtonWidget::new("reset", Rect::new(180.0, 500.0, 100.0, 30.0))
.with_label("Reset")
.reset_form();
고급 예제
Complete Registration Form
use pdf_oxide::writer::{
PdfWriter, PdfWriterConfig,
TextFieldWidget, CheckboxWidget, RadioButtonGroup,
ComboBoxWidget, PushButtonWidget,
};
use pdf_oxide::geometry::Rect;
let config = PdfWriterConfig::default()
.with_title("Registration Form")
.with_author("HR Department");
let mut writer = PdfWriter::with_config(config);
{
let mut page = writer.add_letter_page();
// Title
page.add_text("Employee Registration Form", 72.0, 740.0, "Helvetica-Bold", 18.0);
// Personal Information
page.add_text("First Name:", 72.0, 700.0, "Helvetica", 12.0);
page.add_text_field(
TextFieldWidget::new("first_name", Rect::new(170.0, 696.0, 200.0, 20.0))
.required()
);
page.add_text("Last Name:", 72.0, 670.0, "Helvetica", 12.0);
page.add_text_field(
TextFieldWidget::new("last_name", Rect::new(170.0, 666.0, 200.0, 20.0))
.required()
);
page.add_text("Email:", 72.0, 640.0, "Helvetica", 12.0);
page.add_text_field(
TextFieldWidget::new("email", Rect::new(170.0, 636.0, 250.0, 20.0))
.required()
.with_tooltip("Work email address")
);
// Department dropdown
page.add_text("Department:", 72.0, 610.0, "Helvetica", 12.0);
page.add_combo_box(
ComboBoxWidget::new("department", Rect::new(170.0, 606.0, 200.0, 20.0))
.add_option("eng", "Engineering")
.add_option("sales", "Sales")
.add_option("hr", "Human Resources")
.add_option("ops", "Operations")
);
// Employment type radio buttons
page.add_text("Employment Type:", 72.0, 570.0, "Helvetica", 12.0);
page.add_text("Full-time", 95.0, 550.0, "Helvetica", 10.0);
page.add_text("Part-time", 95.0, 530.0, "Helvetica", 10.0);
page.add_text("Contract", 95.0, 510.0, "Helvetica", 10.0);
page.add_radio_group(
RadioButtonGroup::new("employment_type")
.add_button("fulltime", Rect::new(72.0, 548.0, 15.0, 15.0))
.add_button("parttime", Rect::new(72.0, 528.0, 15.0, 15.0))
.add_button("contract", Rect::new(72.0, 508.0, 15.0, 15.0))
.with_selected("fulltime")
);
// Agreement checkbox
page.add_text("I agree to the terms and conditions", 95.0, 470.0, "Helvetica", 10.0);
page.add_checkbox(
CheckboxWidget::new("agree_terms", Rect::new(72.0, 468.0, 15.0, 15.0))
.with_export_value("agreed")
);
// Submit button
page.add_push_button(
PushButtonWidget::new("submit", Rect::new(72.0, 420.0, 120.0, 30.0))
.with_label("Submit Form")
);
page.finish();
}
writer.save("registration_form.pdf")?;
기존 PDF에 양식 필드 추가
DocumentEditor를 사용하여 기존 PDF 문서에 필드를 추가합니다:
use pdf_oxide::editor::DocumentEditor;
use pdf_oxide::writer::TextFieldWidget;
use pdf_oxide::geometry::Rect;
let mut editor = DocumentEditor::open("template.pdf")?;
let field = TextFieldWidget::new("signature", Rect::new(72.0, 100.0, 250.0, 25.0))
.with_tooltip("Sign here");
editor.add_form_field(0, field)?; // Add to page 0
editor.save("signed_template.pdf")?;
관련 페이지
- DocumentBuilder Low-Level API – Building pages with fluent API
- Annotation Creation – Non-interactive annotation types
- PdfBuilder Fluent API – High-level PDF creation