From 127536c42727da1387600972ece59ece7bcdf331 Mon Sep 17 00:00:00 2001 From: Eric Kaden <608341+EricKaden@users.noreply.github.com> Date: Sat, 24 Oct 2026 15:33:01 -0400 Subject: [PATCH 1/7] M12: a form saved with its pages is laid out from its template when it requires dynamic rendering, unless signed Co-authored-by: EULA <90616501+EULArule@users.noreply.github.com> --- crates/engine/src/tests.rs | 23 ++++++++++++++ crates/render/src/inspect.rs | 11 +++++-- crates/xfa/README.md | 4 +++ crates/xfa/src/fixtures.rs | 40 +++++++++++++++++++++++++ crates/xfa/src/lib.rs | 11 +++++-- crates/xfa/src/packets.rs | 58 ++++++++++++++++++++++++++++++++++++ crates/xfa/src/tests.rs | 35 ++++++++++++++++++++++ parity/acrobat-features.toml | 4 +-- 8 files changed, 180 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index dc76159d14..202f3a3190 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -5288,6 +5288,29 @@ fn dynamic_xfa_forms_open_with_the_values_and_rows_of_their_data() { assert!(!doc.dirty); } +/// A dynamic form a viewer saved with its pages drawn holds only its signature box, so it opened as +/// a picture with nothing to fill in. When its configuration says it must be rendered dynamically +/// it is laid out from its template on opening, and its text field can be filled; once signed, it +/// opens as saved, its pages and signature untouched. +#[test] +fn a_form_saved_with_its_pages_that_requires_dynamic_rendering_is_laid_out_unless_signed() { + let mut s = Session::new().with_clock(|| 1_700_000_000); + let unsigned = Arc::new(pdfcraft_xfa::fixtures::saved_with_pages_shell("required", false)); + let id = s.open("saved.pdf", None, unsigned, None).expect("opens"); + let doc = s.get(id).unwrap(); + assert_eq!(doc.info.xfa, Some(pdfcraft_render::Xfa::Dynamic)); + assert!(doc.xfa.is_some(), "laid out from the template"); + let name = doc.form.iter().find(|f| f.name.contains("name")).map(|f| f.name.clone()).expect("the template's text field"); + s.apply(id, Edit::SetFieldValue { name: name.clone(), value: FieldValue::Text("Ada".into()) }).expect("fills"); + assert_eq!(s.get(id).unwrap().form.iter().find(|f| f.name == name).map(|f| f.value.clone()), Some(vec!["Ada".to_string()])); + let signed = Arc::new(pdfcraft_xfa::fixtures::saved_with_pages_shell("required", true)); + let id = s.open("signed.pdf", None, signed.clone(), None).expect("opens"); + let doc = s.get(id).unwrap(); + assert_eq!(doc.info.xfa, Some(pdfcraft_render::Xfa::Static), "signed: as saved"); + assert!(doc.xfa.is_none(), "nothing was laid out"); + assert_eq!(doc.bytes[..], signed[..], "no revision was appended on open"); +} + /// #3180: an XFA form with no fields whose configuration says `dynamicRender` is `forbidden` is a /// static form: its pages are the form. Opening it used to lay the template out into new pages /// and write them as a revision, so even a save with no change replaced the pages. diff --git a/crates/render/src/inspect.rs b/crates/render/src/inspect.rs index 827e678567..4290c3c48a 100644 --- a/crates/render/src/inspect.rs +++ b/crates/render/src/inspect.rs @@ -809,8 +809,15 @@ impl<'a> Inspector<'a> { // A form with no fields is taken to be laid out from its template, unless its // configuration says it is a static form (#3180): Designer writes // `forbidden` for one, whose pages are the form. - let static_form = self.xfa_dynamic_render(xfa).as_deref() == Some("forbidden"); - info.xfa = Some(if needs_rendering || (info.fields.is_empty() && !static_form) { Xfa::Dynamic } else { Xfa::Static }); + let render = self.xfa_dynamic_render(xfa); + let static_form = render.as_deref() == Some("forbidden"); + // A form a viewer saved with its pages drawn, whose configuration says it must be + // rendered dynamically, is laid out from its template too: its fields live there. + // Not once it is signed: the signature covers the pages as saved (the xfa crate's + // `is_dynamic` keeps the same rule). + let signed = info.fields.iter().any(|f| f.kind == FieldKind::Signature && f.value.is_some()); + let required = render.as_deref() == Some("required") && !signed; + info.xfa = Some(if needs_rendering || required || (info.fields.is_empty() && !static_form) { Xfa::Dynamic } else { Xfa::Static }); } self.layers(catalog, &mut info.layers); info.layer_groups = self.layer_groups(catalog); diff --git a/crates/xfa/README.md b/crates/xfa/README.md index 23a6ef0d0e..a48b43c6ec 100644 --- a/crates/xfa/README.md +++ b/crates/xfa/README.md @@ -134,6 +134,10 @@ let form = pdfcraft_xfa::layout_xml(template_xml)?; // pages of items, for only their data is read and written. A form with no fields is also static when its `config` packet says `forbidden` (Designer's static PDF form): its pages are the form and are not laid out again (#3180). Without that, no fields means dynamic. + The other way round, a form with fields is dynamic when its `config` packet says + `required`: a viewer saved it with its pages drawn and only its + signature boxes as fields, so its other fields live in the template. Once one of those boxes + is signed it stays as saved, since laying it out would change the pages the signature covers. - **Flattening and converting.** `strip` takes the XFA out of a document: the packets, `/NeedsRendering`, the layout mark, the overrides and the private keys on the fields. The engine calls it after flattening an XFA form's fields (so Adobe's viewers show the baked diff --git a/crates/xfa/src/fixtures.rs b/crates/xfa/src/fixtures.rs index b76b3efa1a..f563ad7988 100644 --- a/crates/xfa/src/fixtures.rs +++ b/crates/xfa/src/fixtures.rs @@ -674,6 +674,46 @@ pub fn signed_shell() -> Vec { assemble(objs) } +/// A dynamic form as a viewer saves it with its pages drawn: no `/NeedsRendering`, a page with +/// the form's picture, and only its signature box `sign` among the AcroForm's fields (the text +/// field `name` lives in the template), signed when `signed`. Its configuration packet says +/// `{render}` (none when `render` is empty). +pub fn saved_with_pages_shell(render: &str, signed: bool) -> Vec { + let config = if render.is_empty() { + String::new() + } else { + format!( + r#"1.7{render}"# + ) + }; + let xdp = format!( + r##" + +{config} + + +"## + ); + let value = if signed { " /V 7 0 R" } else { "" }; + let picture = "BT /F1 12 Tf 72 700 Td (Name: ______) Tj ET"; + let objs: Vec> = vec![ + b"<< /Type /Catalog /Pages 2 0 R /AcroForm << /XFA 5 0 R /Fields [6 0 R] /SigFlags 3 >> >>".to_vec(), + b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_vec(), + b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 4 0 R /Annots [6 0 R] >>".to_vec(), + format!("<< /Length {} >>\nstream\n{picture}\nendstream", picture.len()).into_bytes(), + format!("<< /Length {} >>\nstream\n{xdp}\nendstream", xdp.len()).into_bytes(), + format!("<< /T (form[0].sign[0]) /FT /Sig{value} /Type /Annot /Subtype /Widget /Rect [72 500 288 572] /P 3 0 R >>").into_bytes(), + b"<< /Type /Sig /Name (A Signer) >>".to_vec(), + ]; + assemble(objs) +} + /// A check box `kind` whose change script, once it is checked, opens the protected check box /// `leave` and shows the invisible field `details`, then fails on an object the form doesn't /// have (as scripts written for an earlier version of a form do). diff --git a/crates/xfa/src/lib.rs b/crates/xfa/src/lib.rs index 7fe372d53a..a90c335231 100644 --- a/crates/xfa/src/lib.rs +++ b/crates/xfa/src/lib.rs @@ -130,9 +130,16 @@ pub struct Report { } /// Is this a dynamic XFA form still showing its placeholder pages (one PdfCraft has not laid -/// out yet)? +/// out yet)? That is a form that asks to be laid out (`/NeedsRendering`), one with no fields of +/// its own, or one a viewer saved with its pages drawn whose configuration says it must be +/// rendered dynamically (`required`): its fields live in the +/// template. A signature on such a form covers the pages as saved, so a signed one is left as it +/// is: laying it out would change what was signed. pub fn is_dynamic(doc: &Document) -> bool { - existing_layout(doc).is_none() && matches!(read_packets(doc), Ok(Some(p)) if p.needs_rendering || !p.has_fields) + existing_layout(doc).is_none() + && matches!(read_packets(doc), Ok(Some(p)) if p.needs_rendering + || !p.has_fields + || (packets::requires_dynamic_render(&p.xdp) && !packets::has_signature(doc))) } /// Lay a template packet (or whole XDP, whose datasets then fill the fields) out. diff --git a/crates/xfa/src/packets.rs b/crates/xfa/src/packets.rs index f79b071c0e..d1ac1831e3 100644 --- a/crates/xfa/src/packets.rs +++ b/crates/xfa/src/packets.rs @@ -97,6 +97,64 @@ fn to_text(bytes: &[u8]) -> String { } } +/// Whether the XDP's configuration packet asks for dynamic rendering: `required` at +/// `config/acrobat/acrobat7/dynamicRender` (Designer writes it for a dynamic form). The same +/// names in the template or the data are not the configuration. +pub(crate) fn requires_dynamic_render(xdp: &str) -> bool { + // The configuration packet: from `' || c.is_whitespace())) + else { + return false; + }; + let Some(packet) = xdp.get(start..).and_then(|r| r.find("").and_then(|end| r.get(..end + "".len()))) else { + return false; + }; + let Ok(config) = roxmltree::Document::parse(packet) else { return false }; + let mut node = config.root_element(); + for name in ["acrobat", "acrobat7", "dynamicRender"] { + match node.children().find(|c| c.is_element() && c.tag_name().name() == name) { + Some(c) => node = c, + None => return false, + } + } + node.text().is_some_and(|t| t.trim() == "required") +} + +/// Whether the AcroForm holds a signed signature field (`/FT /Sig` with a value), looked for at +/// most 32 levels deep and among at most 10 000 fields. +pub(crate) fn has_signature(doc: &Document) -> bool { + let Some(root) = doc.root() else { return false }; + let catalog = doc.get(root); + let Some(acro) = catalog.as_dict().and_then(|c| c.get(b"AcroForm")).map(|a| doc.resolve(a)) else { return false }; + let Some(fields) = acro.as_dict().and_then(|a| a.get(b"Fields")).map(|f| doc.resolve(f)) else { return false }; + let Some(fields) = fields.as_array() else { return false }; + // (field, inherited field type, depth) + let mut todo: Vec<(Object, bool, usize)> = fields.iter().map(|f| (f.clone(), false, 0)).collect(); + let mut seen = std::collections::HashSet::new(); + let mut budget = 10_000usize; + while let Some((f, sig_above, depth)) = todo.pop() { + if budget == 0 || depth > 32 || f.as_ref().is_some_and(|r| !seen.insert(r)) { + continue; + } + budget -= 1; + let f = doc.resolve(&f); + let Some(d) = f.as_dict() else { continue }; + let sig = d.name(b"FT").map_or(sig_above, |t| t == b"Sig"); + if sig && d.get(b"V").is_some_and(|v| !matches!(&*doc.resolve(v), Object::Null)) { + return true; + } + if let Some(kids) = d.get(b"Kids").map(|k| doc.resolve(k)) + && let Some(kids) = kids.as_array() + { + todo.extend(kids.iter().map(|k| (k.clone(), sig, depth + 1))); + } + } + false +} + /// Read the XFA packets, if the document has any. pub fn read_packets(doc: &Document) -> Result, XfaError> { let Some(root) = doc.root() else { return Ok(None) }; diff --git a/crates/xfa/src/tests.rs b/crates/xfa/src/tests.rs index 3175882f89..7bb0b7d4ca 100644 --- a/crates/xfa/src/tests.rs +++ b/crates/xfa/src/tests.rs @@ -299,6 +299,41 @@ fn tables_break_across_pages_and_repeat_their_header_row() { assert_eq!(all.len(), all.iter().collect::>().len(), "field names are unique"); } +/// A form a viewer saved with its pages drawn, holding only its signature box, is laid out from +/// its template when its configuration says it must be rendered dynamically: its other fields +/// live there. Signed, it is left as saved (laying it out would change what was signed), as it is +/// without that configuration or with `forbidden`. +#[test] +fn a_form_saved_with_its_pages_is_laid_out_when_it_requires_dynamic_rendering() { + let open = |render: &str, signed: bool| Document::open(Arc::new(crate::fixtures::saved_with_pages_shell(render, signed))).unwrap(); + let mut doc = open("required", false); + assert!(is_dynamic(&doc)); + let report = render_into(&mut doc).unwrap(); + assert_eq!(report.fields, 2, "the text field from the template, and the signature box"); + assert!(!is_dynamic(&open("required", true)), "signed: left as saved"); + assert!(!is_dynamic(&open("", false)), "no configuration: the fields it has are the form's"); + assert!(!is_dynamic(&open("forbidden", false))); +} + +/// Only the configuration packet's `acrobat/acrobat7/dynamicRender` asks for dynamic rendering: +/// not the same element elsewhere, another element that starts with `config`, or another value. +#[test] +fn dynamic_rendering_is_read_from_the_configuration_only() { + let required = crate::packets::requires_dynamic_render; + let config = |inner: &str| { + format!( + r#"{inner}