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() -> pdfluent::Result<()> {
let doc = PdfDocument::open("form.pdf")?;
for field in doc.form_fields()? {
println!("{} = {} ({:?})", field.name, field.value, field.field_type);
}
Ok(())
}Open the document and check that it contains an AcroForm.
use pdfluent::PdfDocument;
let doc = PdfDocument::open("application.pdf")?;
if doc.form_fields()?.is_empty() {
println!("No form fields.");
return Ok(());
}Call doc.form() to get an immutable PdfForm handle.
let fields = doc.form_fields()?;
println!("{} form field(s)", fields.len());doc.form_fields() returns a slice of FormField values. Each entry has a name, field_type, and value.
for field in doc.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) = doc.form_fields()?.iter().find(|f| f.name == "email") {
println!("Email: {}", f.value);
}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> = doc
.form_fields()?
.into_iter()
.map(|f| (f.name, f.value))
.collect();
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.