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::prelude::*;

fn main() -> Result<()> {
    let mut doc = PdfDocument::open("application.pdf")?;

    {
        let mut form = doc.form_mut();
        form.set_text("first_name", "Jane")?
            .set_text("last_name", "Smith")?
            .set_checkbox("agree_terms", true)?
            .set_dropdown("country", "Netherlands")?;
    }

    doc.save("application_filled.pdf")?;
    Ok(())
}
Install:cargo add pdfluent@1.0.0-beta.5Download SDK →

Step by step

1

Open the form PDF

Open the PDF with a mutable binding. Use form_fields() to check whether the document has any form fields before trying to fill them.

rust
use pdfluent::prelude::*;

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

if doc.form_fields()?.is_empty() {
    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 write field values. The accessor is infallible — errors surface on the individual set_* calls when a field does not exist or has a different type.

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. The setters chain via Result<&mut Self>.

rust
form.set_text("first_name", "Jane")?
    .set_text("last_name", "Smith")?
    .set_text("email", "jane@example.com")?;
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. PDFluent writes the field value (/V) correctly. Note: in 1.0 the widget appearance state (/AS) on kid annotations is not yet synced — viewers that honour /AS may show stale visual state until they rebuild appearance from /V.

rust
form.set_checkbox("agree_terms", true)?
    .set_checkbox("newsletter", false)?
    // Radio button: pass the export value, not the display label
    .set_radio("payment_method", "credit_card")?;
5

Save the filled form

Drop the form handle scope and call save() on the document. flatten_forms() is on the 1.0 surface but currently returns Error::MissingDependency — the flatten runtime is tracked for a 1.x MINOR.

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

Notes and tips

  • Field names are the partial names of top-level AcroForm fields. In 1.0 only the top-level /Fields array is addressable; fully-qualified names like "Address.Street" that require walking /Kids are not yet supported.
  • Use doc.form_fields() to list every form field with its name, type, and current value.
  • Multiline text fields accept newline characters ("\n") in the value string.
  • set_text() and set_dropdown() encode values with lopdf::text_string — ASCII values use PDFDocEncoding, non-ASCII values switch to UTF-16BE with BOM so Unicode round-trips correctly.
  • Kid-widget checkbox and radio appearance sync (/AS on /Kids[*]) is tracked as a 1.1 follow-up; today /V is written correctly but the visual state may lag in viewers that don't rebuild appearances from /V.

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