Fill PDF form fields in Rust

Set text field values, check checkboxes, select radio buttons, and choose dropdown options programmatically in any AcroForm PDF.

rust
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(())
}
Install:cargo add pdfluentDownload SDK →

Step by step

1

Open the form PDF

Open the PDF with a mutable binding. Check that the document has an AcroForm before trying to fill fields.

rust
use pdfluent::PdfDocument;

let mut doc = PdfDocument::open("application.pdf")?;

if !doc.has_form() {
    eprintln!("This PDF has no form fields.");
    return Ok(());
}
2

Get a mutable form handle

Call form_mut() to get a PdfFormMut value that lets you read and write field values.

rust
let mut form = doc.form_mut()?;
3

Set text field values

Use set_text() with the field name as it appears in the PDF. Field names are case-sensitive.

rust
form.set_text("first_name", "Jane")?;
form.set_text("last_name", "Smith")?;
form.set_text("email", "[email protected]")?;
4

Set checkboxes and radio buttons

Pass true or false to set_checkbox(). For radio buttons, pass the export value of the option you want selected.

rust
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")?;
5

Save the filled form

Drop the form handle and call save() on the document. If you want to prevent further editing, flatten the form before saving.

rust
drop(form);
doc.save("application_filled.pdf")?;

Notes and tips

  • Field names are the partial names from the AcroForm hierarchy. Use form.fields() to list all available field names before filling.
  • Multiline text fields accept newline characters ("\n") in the value string.
  • If a text field has a maximum length, set_text() returns an Err if the value exceeds it.
  • To list all available dropdown options, call form.dropdown_options("field_name").

Why PDFluent for this

Pure Rust

No JVM, no runtime, no DLL dependencies. Ships as a single native binary or WASM module.

Memory safe

Rust's ownership model prevents buffer overflows and use-after-free. No segfaults in PDF parsing.

Runs anywhere

Same code runs server-side, in Docker, on AWS Lambda, on Cloudflare Workers, or in the browser via WASM.

Frequently asked questions