List all form fields in a PDF and read their current values. Covers text fields, checkboxes, radio buttons, dropdowns, and list boxes.
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(())
}Open the document and check that it contains an AcroForm.
use pdfluent::PdfDocument;
let doc = PdfDocument::open("application.pdf")?;
if !doc.has_form() {
println!("No form fields.");
return Ok(());
}Call doc.form() to get an immutable PdfForm handle.
let form = doc.form()?;form.fields() returns a slice of FormField values. Each entry has a name, field_type, and value.
for field in form.fields() {
println!("name: {}", field.name);
println!("type: {:?}", field.field_type);
println!("value: {:?}", field.value);
println!();
}Use form.field("name") to get a single field. Returns None if the field is not found.
if let Some(f) = form.field("email") {
println!("Email: {}", f.value.as_text().unwrap_or(""));
}Use form.values_as_map() to get all field names and their string representations in one call. Useful for logging or serialising form data.
use std::collections::HashMap;
let values: HashMap<String, String> = form.values_as_map();
for (name, val) in &values {
println!("{} = {}", name, val);
}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.