Crear formularios
Añade campos de formulario interactivos a tus documentos PDF. PDF Oxide admite todos los tipos de widget AcroForm estándar: campos de texto, casillas de verificación, grupos de radio, combos (listas desplegables), listas y botones pulsables.
Cobertura por binding (v0.3.38).
FluentPageBuilderexponetext_field,checkbox,combo_box,radio_groupypush_buttonen Rust, Python, Node/TypeScript, C#, Go y WASM (consulta DocumentBuilder → Widgets AcroForm). La superficie de bajo nivelPdfWriter::text_field/checkbox/ etc. de esta página es solo Rust y ofrece controles adicionales por widget (texto multilínea, contraseñas, listas, colores de borde); úsala cuando necesites control fino sobre AcroForm. Rellenar y aplanar formularios existentes funciona en todos los bindings — consulta Edición de campos de formulario y Rellenar formularios PDF.
Ejemplo rápido
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")
Tipos de widgets
TextoFieldWidget – entrada de texto
Campos de entrada de texto de una o múltiples líneas.
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();
Métodos clave:
| Method | Description |
|---|---|
.with_value(s) |
Set current text value |
.with_default_value(s) |
Set reset value |
.with_max_length(n) |
Maximum character count |
.required() |
Mark as required field |
.read_only() |
Prevent editing |
.password() |
Mask input characters |
.multiline() |
Allow multiple lines |
.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) |
Establecer color de borde |
.with_background_color(r, g, b) |
Establecer color de fondo |
CheckboxWidget – casilla de verificación
Campos de alternancia con estados activado/desactivado.
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");
Key methods:
| Method | Description |
|---|---|
.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 – botones de radio
Grupos de opciones mutuamente excluyentes. Todos los botones en un grupo comparten el mismo nombre de campo; seleccionar uno deselecciona los otros.
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"
Key methods:
| Method | Description |
|---|---|
.add_button(value, rect) |
Add a radio option |
.with_selected(value) |
Pre-select an option |
.no_toggle_off() |
Prevent deselecting all options |
ComboBoxWidget – lista desplegable
Campos de selección desplegable con texto editable opcional.
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");
Key methods:
| Method | Description |
|---|---|
.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 – lista de selección múltiple
Campos de lista desplazable con selección simple o múltiple.
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");
Key methods:
| Method | Description |
|---|---|
.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 – botón
Botones que activan acciones (enviar, restablecer o 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();
Ejemplos avanzados
Formulario de registro completo
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")?;
Agregar campos de formulario a PDFs existentes
Use DocumentEditor para agregar campos a un documento PDF preexistente:
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")?;
Páginas relacionadas
- API de bajo nivel DocumentBuilder – Construcción de páginas con API fluida
- Creación de anotaciones – Tipos de anotación no interactivos
- API fluida PdfBuilder – Creación de PDF de alto nivel