Include FPdfFormMerge to use either MergePdfFilesWithForms or TPdfFormMergeHelper.MergeFilesWithForms
The helper stages imports of selected pages and field grafting, discards orphaned imported widgets, and independently reopens the saved output before replacing the caller's document; errors or cancellation leave the destination intact
TPdfFormMergeReport summarises the merge with success and cancellation flags, imported source and page counts, grafted, renamed, merged and filtered field and widget counters plus the resource rename count; ErrorMessage carries the failure text of an aborted merge, which the caller can raise directly
pagcpReject rejects a duplicate fully qualified field name, pagcpRename adds a deterministic collision-free prefix and suffix, and pagcpNamespace inserts a real parent field to isolate each source
pagcpMergeCompatible joins compatible terminal text, choice, checkbox or radio fields so their widgets share one value; it requires compatible flags, values, defaults, options and default appearance meaning, and rejects field actions and unsupported field types; rich text joins only where both sides carry one direct identical RV and DS pair, and checkbox or radio export-option arrays join where both sides carry compatible direct or indirect /Opt export arrays, dynamically remapping widget /AS appearance states and field /V / /DV indices across the combined widget list; same-name pushbutton fields also join: their stateless widgets keep their own appearances through terminal-widget, action and appearance-presence checks without value or export-state comparisons; same-name container fields join their whole trees when every named child pairs exactly: leaves join through the regular compatible checks, whilst unmatched or duplicate child names, mixed widget and field children, and container actions are rejected
Page selection prunes unused widgets and fields and retains a filtered, deduplicated calculation order; default resources are isolated and default-appearance names rewritten, but cloned appearance streams keep their own resources
Combined field/widget objects are normalised before compatible fields are joined; separately named fields can still be edited independently after saving and reopening
InsertAt counts from zero, with -1 meaning append; Default allows 10,000 pages overall and 256 MiB per document, and grafting permits 100,000 objects to depth 128
TPdfFormMergeOptions configures the staged merge: GraftOptions forwards the TPdfAcroFormGraftOptions collision and resource policies into field grafting, MaxPages caps total staged pages, and MaxDocumentBytes caps each document captured during staging
Grafting has hard limits of 1,000,000 objects, depth 256 and 4,096 root fields; XFA sources or destinations, signed sources or destinations and encrypted destinations are refused
An optional CancellationToken of type IPdfCancellationToken is polled before each source, before each repeated-page pass and during reopen verification; a cancelled token raises EPdfOperationCancelled and preserves the destination
A source password can decrypt input for the staged merge; output is unencrypted, so explicitly encrypt it afterwards if required
The original GraftPdfAcroForm overload continues to reject collisions; the overload taking TPdfAcroFormGraftOptions makes the same collision and resource policies available for streams
Source outlines and named destinations are outside this form helper's remit; use the navigation helper for the navigation workflow it documents
Options := TPdfFormMergeOptions.Default; Options.GraftOptions.CollisionPolicy := pagcpNamespace; if not MergePdfFilesWithForms(Pdf, Sources, Options, Report) then raise EPdfError.Create(Report.ErrorMessage);
RepeatedPagePolicy draws on TPdfFormRepeatPolicy; pfrpReject is the Default and keeps the earlier unique-page requirement
pfrpIndependentPasses splits the selected page sequence into consecutive passes, opening a new pass just before a page would come round again; 1,2,1,2 turns into two passes of 1,2, whilst 1,2,1,3 becomes passes 1,2 and 1,3
Where repetition is present, each independent pass takes a genuine namespace built from NamespacePrefix, the one-based source entry index, an underscore and the pass index; widgets share their original field values within a pass, and separate passes stay independently editable
The independent policy opts those passes into namespace isolation; with a unique selection the caller's original GraftOptions collision policy and existing namespace naming are kept
pfrpMergeCompatiblePasses calls for GraftOptions.CollisionPolicy to be pagcpMergeCompatible; matching fields across passes share a single value, and any incompatible field aborts the whole staged merge
Field, widget and resource counters roll up the pass operations; a rejected report may describe staging work that was thrown away and does not imply any committed change to the destination
Page order, insertion position and ordinary source-entry counts are preserved; ImportedPassCount counts the import passes actually made, and RepeatedPageCount counts selected occurrences after a page's first occurrence within its source entry
The low-level GraftPdfAcroForm mapping stays unique per call; the high-level helper creates separate bounded graft calls and never duplicates a source page mapping within one call
Default appearances are tokenised with whitespace disregarded; font resource names may differ where the referenced dependency graphs share the same fingerprints, and unrelated unused destination fonts no longer stand in the way of a merge
Font fingerprints swap indirect identities for typed dependency digests and keep the dictionary/stream bytes; acyclic referenced font graphs are supported, while differing dictionary representations, compressed encodings or font contents may conservatively refuse a match
Fingerprint traversal draws on MaxObjects and MaxDepth together with a combined 256 MiB fingerprint input budget, a 65,536-character default-appearance limit and 1,024 tokens per appearance; compound default-appearance operands and named appearance resources beyond Tf font operands remain unsupported, and MaxDepth also caps resource-container nesting
Confirmed compatible indirect fonts are reused instead of being copied for every repeated pass; existing caller-provided font mappings must keep the same fingerprint
Existing appearance resources hang on to their local names; an appearance stream without Resources receives a shared snapshot of the source AcroForm default resources, so a same-name font in the destination cannot take over its original meaning
Checkbox widgets need Off and one matching on-state apiece, with field value and appearance state in step; absent or null unchecked states count as Off, and differing export states are refused
Radio widgets need Off and one on-state apiece; ordinary groups keep the destination's selection where one exists and clear imported selections, whilst RadiosInUnison retains matching selected appearances
Checkbox and radio fields with direct or indirect /Opt export-value arrays join across differing widget counts or reordered/differing export values, either by concatenating widget export entries into a combined /Opt array or, where compatible radio groups share logical export values at reordered indices, by order-preserving option union whilst remapping each widget's numeric /AP (/N, /R, /D) key, /AS state and field /V / /DV value to its merged zero-based index
Choice fields (/FT /Ch) carrying differing direct or indirect /Opt arrays join by order-preserving option union across both string options and two-element [exportText displayText] option pairs, verifying consistent display labels and remapping selection index arrays (/I) into the merged option order
type
TPdfFormRepeatPolicy= (pfrpReject, pfrpIndependentPasses, pfrpMergeCompatiblePasses);
TPdfFormMergeOptions= record
GraftOptions: TPdfAcroFormGraftOptions;
InsertAt: Integer;
MaxPages: Integer;
MaxDocumentBytes: Int64;
CancellationToken: IPdfCancellationToken;
RepeatedPagePolicy: TPdfFormRepeatPolicy;
class function Default: TPdfFormMergeOptions; static;
end;
TPdfFormMergeReport= record
Succeeded, Cancelled: Boolean;
ImportedSourceCount, ImportedPageCount: Integer;
ImportedPassCount, RepeatedPageCount: Integer;
GraftedFieldCount, RenamedFieldCount, MergedFieldCount: Integer;
FilteredFieldCount, FilteredWidgetCount, ResourceRenameCount: Integer;
ErrorMessage: string;
end;
function MergePdfFilesWithForms(Destination: TPdf;
const Sources: TPdfBatchMergeSources; const Options: TPdfFormMergeOptions;
out Report: TPdfFormMergeReport): Boolean;
type
TPdfFormMergeHelper= class helper for TPdf
function MergeFilesWithForms(const Sources: TPdfBatchMergeSources;
const Options: TPdfFormMergeOptions;
out Report: TPdfFormMergeReport): Boolean;
end;