PdfFormFlattener class

Real form flattening: draws the appearance and deletes the field.

It is the missing delivery of phase F6 of the roadmap. The flattenFields() of PdfAcroForm removes the /AcroForm without drawing anything — its _flattenField is an empty skeleton — so the filled-in value disappears from the page. Here the path is the PDFBox one (PDAcroForm.flatten):

  1. locate the widget in the /Fields tree, inheriting /FT and composing the fully qualified name;
  2. take the form XObject of the normal appearance (/AP /N), choosing the state by /AS when /N is a dictionary of states;
  3. draw that XObject on the page, scaled from the /BBox transformed by the /Matrix up to the /Rect of the widget;
  4. only then remove the widget from /Annots and the field from /AcroForm /Fields.

No appearance is synthesized. A field without a usable /AP /N raises a warning and is not removed: claiming it was flattened without drawing anything would lose the content silently (roadmap §9, "a flattened field disappearing").

final document = PdfDocument.parseFromBytes(bytes);
final result = PdfFormFlattener(document).flatten();
print('${result.flattened.length} campos achatados');
print(result.warnings);
final flat = await document.save();

What the flattening covers

Any widget with a normal appearance, whatever the /FT: text, checkbox, radio, button, combo, list and signature. What changes between them is only the appearance already written, and that is what goes to the page. Fields with /Kids are walked, and a widget with /AP /N in states picks the state by /AS — that is how the mark of a checked checkbox is preserved.

What it does not cover

  • Generating an appearance. /NeedAppearances true with a filled /V and no /AP does not become drawing; the field survives with a warning. That depends on the PdfAppearanceService planned for F5/F6.
  • /AP /N in states without /AS. Picking a state would mean deciding for the document; the field survives with a warning.
  • XFA. A dynamic XFA form stays in the catalog; removing it is an explicit option planned for F6.
  • Signatures. Flattening invalidates any existing signature, like any other content change.

Constructors

PdfFormFlattener(PdfDocument document, {PdfFormFlattenOptions options = const PdfFormFlattenOptions()})
Creates the flattener for document.

Properties

acroForm ↔ PdfAcroFormAccess
The single path for changing the /AcroForm.
latefinal
document → PdfDocument
The edited document.
final
hashCode → int
The hash code for this object.
no setterinherited
mutator ↔ PdfLoadedObjectMutator
The materializer shared by every change of this operation.
latefinal
options → PdfFormFlattenOptions
The flattening options.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
store → PdfObjectStore
The object resolution, with a fallback to the source parser.
final

Methods

flatten({Iterable<String>? fieldNames}) → PdfFormFlattenResult
Flattens the form fields.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited