Set text field values, check checkboxes, select radio buttons, and choose dropdown options programmatically in any AcroForm PDF.
use pdfluent::PdfDocument;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut doc = PdfDocument::open("application.pdf")?;
let mut form = doc.form_mut()?;
form.set_text("first_name", "Jane")?;
form.set_text("last_name", "Smith")?;
form.set_checkbox("agree_terms", true)?;
form.set_dropdown("country", "Netherlands")?;
doc.save("application_filled.pdf")?;
Ok(())
}Open the PDF with a mutable binding. Check that the document has an AcroForm before trying to fill fields.
use pdfluent::PdfDocument;
let mut doc = PdfDocument::open("application.pdf")?;
if !doc.has_form() {
eprintln!("This PDF has no form fields.");
return Ok(());
}Call form_mut() to get a PdfFormMut value that lets you read and write field values.
let mut form = doc.form_mut()?;Use set_text() with the field name as it appears in the PDF. Field names are case-sensitive.
form.set_text("first_name", "Jane")?;
form.set_text("last_name", "Smith")?;
form.set_text("email", "[email protected]")?;Pass true or false to set_checkbox(). For radio buttons, pass the export value of the option you want selected.
form.set_checkbox("agree_terms", true)?;
form.set_checkbox("newsletter", false)?;
// Radio button: pass the export value, not the display label
form.set_radio("payment_method", "credit_card")?;Drop the form handle and call save() on the document. If you want to prevent further editing, flatten the form before saving.
drop(form);
doc.save("application_filled.pdf")?;No JVM, no runtime, no DLL dependencies. Ships as a single native binary or WASM module.
Rust's ownership model prevents buffer overflows and use-after-free. No segfaults in PDF parsing.
Same code runs server-side, in Docker, on AWS Lambda, on Cloudflare Workers, or in the browser via WASM.