Read PDF form field values in Rust

List all form fields in a PDF and read their current values. Covers text fields, checkboxes, radio buttons, dropdowns, and list boxes.

rust
use pdfluent::PdfDocument;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let doc = PdfDocument::open("filled_form.pdf")?;
    let form = doc.form()?;

    for field in form.fields() {
        println!("{:?}  name={} value={:?}",
            field.field_type, field.name, field.value);
    }
    Ok(())
}
Install:cargo add pdfluentDownload SDK →

Step by step

1

Open the PDF

Open the document and check that it contains an AcroForm.

rust
use pdfluent::PdfDocument;

let doc = PdfDocument::open("application.pdf")?;
if !doc.has_form() {
    println!("No form fields.");
    return Ok(());
}
2

Get the form handle

Call doc.form() to get an immutable PdfForm handle.

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

List all fields

form.fields() returns a slice of FormField values. Each entry has a name, field_type, and value.

rust
for field in form.fields() {
    println!("name: {}", field.name);
    println!("type: {:?}", field.field_type);
    println!("value: {:?}", field.value);
    println!();
}
4

Read a specific field by name

Use form.field("name") to get a single field. Returns None if the field is not found.

rust
if let Some(f) = form.field("email") {
    println!("Email: {}", f.value.as_text().unwrap_or(""));
}
5

Export all field values to a HashMap

Use form.values_as_map() to get all field names and their string representations in one call. Useful for logging or serialising form data.

rust
use std::collections::HashMap;

let values: HashMap<String, String> = form.values_as_map();
for (name, val) in &values {
    println!("{} = {}", name, val);
}

Notes and tips

  • FormField.value is a FieldValue enum. Match on its variants: Text, Checked, Selected, and None.
  • Hierarchical field names use a dot separator, e.g. "address.city". form.fields() returns the full dotted name.
  • Read-only and hidden fields are included in the field list. Check field.is_read_only and field.is_hidden.

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