How-to guides/XFA Forms

Convert an XFA form to a standard AcroForm PDF in Rust

XFA forms do not open correctly in many PDF readers. Converting to AcroForm makes your PDFs compatible with Preview, browsers, and any standard PDF viewer.

rust
use pdfluent::{PdfDocument, XfaToAcroformOptions};

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

    let opts = XfaToAcroformOptions::default()
        .preserve_values(true)
        .flatten_static_content(false);

    doc.convert_xfa_to_acroform(opts)?;
    doc.save("acroform-output.pdf")?;
    Ok(())
}
Install:cargo add pdfluentDownload SDK →

Step by step

1

Add PDFluent with the xfa feature

XFA-to-AcroForm conversion requires both the xfa and acroform feature flags.

rust
# Cargo.toml
[dependencies]
pdfluent = { version = "0.9", features = ["xfa", "acroform"] }
2

Open the XFA document

Open the source PDF. Call has_xfa() to confirm it contains an XFA form before converting.

rust
use pdfluent::PdfDocument;

let mut doc = PdfDocument::open("xfa-form.pdf")?;

if !doc.has_xfa() {
    eprintln!("No XFA form found. Skipping conversion.");
    return Ok(());
}
3

Configure the conversion options

preserve_values carries over any data already entered into the XFA form. flatten_static_content burns non-interactive XFA graphics into the page as static content.

rust
use pdfluent::XfaToAcroformOptions;

let opts = XfaToAcroformOptions::default()
    .preserve_values(true)
    .flatten_static_content(false)
    .page_size_from_xfa(true);
4

Run the conversion

convert_xfa_to_acroform() rewrites the document in place. The XFA streams are removed and replaced with AcroForm fields at the correct positions.

rust
doc.convert_xfa_to_acroform(opts)?;

let field_count = doc.acroform()?.fields().count();
println!("Converted {} fields to AcroForm", field_count);
5

Save the AcroForm PDF

The output file is a standard PDF with no XFA dependency. It opens in any PDF viewer.

rust
doc.save("acroform-output.pdf")?;
println!("Saved acroform-output.pdf");

Notes and tips

  • Dynamic XFA forms with JavaScript business logic may not convert perfectly. Static layout forms convert with high fidelity.
  • Field types are mapped: XFA text fields become AcroForm text fields, XFA checkboxes become AcroForm checkboxes, and XFA dropdowns become AcroForm combo boxes.
  • The PDF version is set to 1.6 or higher on output. This is required for AcroForm fields with rich text content.
  • After conversion, verify the output with doc.acroform()?.fields() to confirm all expected fields are present.

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