Check for an XFA form
You’ll check a PDF form for an XFA part. XFA is an older Adobe format, deprecated in PDF 2.0, that forms made in Adobe’s LiveCycle tools may carry. You need this before you fill a form, so you know what a person will see in each app.
xfaFacts says whether the form has an XFA part and what kind it is. xfa also gives each packet’s bytes. A dynamic form has placeholder pages, usually a message asking you to wait, and only Adobe’s apps can show or fill it. A static form has real pages and ordinary fields beside its XFA part. The example leaves a dynamic form alone and removes a static form’s XFA part with the removeXfa edit:
// Check whether a PDF form is an XFA form, and make a static one show the same answers in every// app. An XFA form is an older Adobe format that only Adobe's apps lay out. A dynamic one has// placeholder pages and nothing to fill here; a static one has real pages and ordinary fields,// but Adobe's apps show the XFA part's own answers, so the part is removed.import { Document } from "@laminapdf/sdk";
/** * The form as it saves: without its XFA part when it is a static XFA form, unchanged otherwise. * Needs `init({ extract: true })`. */export function checkXfaForm(form: Uint8Array): Uint8Array<ArrayBuffer> { using doc = Document.open(form); const xfa = doc.xfaFacts(); if (xfa === undefined) { console.warn("not an XFA form"); } else if (xfa.kind === "dynamic") { // Only Adobe's apps lay it out: leave it as it is. console.warn("a dynamic XFA form: open it in Adobe Acrobat"); } else { console.warn("a static XFA form: removing its XFA part"); doc.apply([{ op: "removeXfa", args: {} }]); } return doc.save({ mode: "fullRewrite" });}//! Check whether a PDF form is an XFA form, and make a static one show the same answers in//! every app.//!//! An XFA form is an older Adobe format that only Adobe's apps lay out. A dynamic one has//! placeholder pages and nothing to fill here; a static one has real pages and ordinary fields,//! but Adobe's apps show the XFA part's own answers, so the part is removed.//!//! ```sh//! cargo run -p laminapdf --example check_xfa_form -- form.pdf [out-dir]//! ```
use std::error::Error;use std::path::PathBuf;
use laminapdf::{Document, OpenOptions, RemoveXfa, SaveMode, SaveOptions, XfaKind};
fn main() -> Result<(), Box<dyn Error>> { let mut args = std::env::args().skip(1); let usage = "usage: check_xfa_form form.pdf [out-dir]"; let form = PathBuf::from(args.next().ok_or(usage)?); let out = PathBuf::from(args.next().unwrap_or_else(|| "target/examples-out".into())); std::fs::create_dir_all(&out)?; std::fs::write(out.join("check_xfa_form.pdf"), check(std::fs::read(form)?)?)?; Ok(())}
/// The form as it saves: without its XFA part when it is a static XFA form, unchanged otherwise.pub(crate) fn check(bytes: Vec<u8>) -> Result<Vec<u8>, Box<dyn Error>> { let mut doc = Document::open_bytes(bytes, OpenOptions::new())?; match doc.xfa()?.map(|xfa| xfa.kind) { None => eprintln!("not an XFA form"), // Only Adobe's apps lay it out: leave it as it is. Some(XfaKind::Dynamic) => eprintln!("a dynamic XFA form: open it in Adobe Acrobat"), Some(_) => { eprintln!("a static XFA form: removing its XFA part"); doc.apply(vec![RemoveXfa::new().into()])?; } } let options = SaveOptions::new().with_mode(SaveMode::FullRewrite); Ok(doc.save(&options)?.bytes)}
The saved form has the same pages and fields, and no XFA part.
How it works
Section titled “How it works”- A form is dynamic when the catalog’s
/NeedsRenderingis true, when itsconfigpacket requires dynamic rendering, or when its template has fields and its form has none. Any other form with an XFA part is static. - Adobe’s apps show a static form’s XFA answers, not the fields’, so answers filled into the fields alone don’t show there. Removing the XFA part makes every app show the fields.
- Some static forms keep their answers only in the XFA part, so their fields look empty once it is gone. Undo brings the part back.
- A certified file, and a file with usage rights, don’t allow the edit: saving it would change the file after the signature.
Next: fill a form from JSON.