PDFium Delphi Component Docs

Transactional AcroForm merge

Add FPdfFormMerge to enable MergePdfFilesWithForms or TPdfFormMergeHelper.MergeFilesWithForms

The helper stages selected page imports and field grafting, removes orphaned imported widgets and independently reopens the saved result before replacing the caller document; errors and cancellation preserve the destination

TPdfFormMergeReport summarizes 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 when both sides carry one direct identical RV and DS pair, and checkbox or radio export-option arrays join when both sides carry compatible direct or indirect /Opt export arrays and dynamically remap 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, unmatched or duplicate child names, mixed widget and field children, and container actions reject

Selected-page filtering prunes unused widgets and fields and preserves the filtered, deduplicated calculation order; default resources are isolated and default appearance names rewritten while cloned appearance streams retain their own resources

Combined field/widgets are normalized before joining compatible fields; independent names remain independently editable after saving and reopening

InsertAt is a zero-based insertion index; -1 appends pages; Default limits are 10,000 total pages and 256 MiB per document, with graft limits of 100,000 objects and 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

Hard graft limits are 1,000,000 objects, depth 256 and 4,096 root fields; XFA sources or destinations, signed sources or destinations and encrypted destinations are rejected

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

Source passwords may decrypt a source for the staged merge; the result is unencrypted, so apply an explicit encryption workflow afterward when required

The legacy GraftPdfAcroForm overload retains its reject policy; the overload accepting TPdfAcroFormGraftOptions exposes the same collision and resource policies at stream level

This form helper does not preserve source outlines or named destinations; use the navigation helper for its declared navigation workflow

Options := TPdfFormMergeOptions.Default;
Options.GraftOptions.CollisionPolicy := pagcpNamespace;
if not MergePdfFilesWithForms(Pdf, Sources, Options, Report) then
  raise EPdfError.Create(Report.ErrorMessage);

Repeated selected pages

RepeatedPagePolicy uses TPdfFormRepeatPolicy; pfrpReject is the Default and retains the earlier unique-page requirement

pfrpIndependentPasses partitions the selected page sequence into consecutive passes, starting a new pass just before a page would repeat; 1,2,1,2 becomes two passes of 1,2, while 1,2,1,3 becomes passes 1,2 and 1,3

When repetition is present, each independent pass uses a real namespace derived from NamespacePrefix, the one-based source entry index, an underscore and the pass index; widgets share their original field values within a pass, while separate passes remain independently editable

The independent policy selects namespace isolation for those passes; a unique selection retains the caller's original GraftOptions collision policy and existing namespace naming

pfrpMergeCompatiblePasses requires GraftOptions.CollisionPolicy to be pagcpMergeCompatible; matching fields across passes share one value, and any incompatible field aborts the complete staged merge

Field, widget and resource counters aggregate pass operations; a rejected report can describe discarded staging work and does not imply a committed destination change

Page order, insertion position and ordinary source-entry counts are preserved; ImportedPassCount counts actual import passes, and RepeatedPageCount counts selected occurrences after the first occurrence of that page within its source entry

The low-level GraftPdfAcroForm mapping remains unique per call; the high-level helper creates separate bounded graft calls without duplicating a source page mapping inside one call

Compatible appearance and button semantics

Default appearances are tokenized with whitespace ignored; font resource names may differ when the referenced dependency graphs have the same fingerprints, and unrelated unused destination fonts no longer reject a merge

Font fingerprints replace indirect identities with typed dependency digests and retain dictionary/stream bytes; acyclic referenced font graphs are supported, while different dictionary representations, compressed encodings or font contents may conservatively reject a match

Fingerprint traversal uses MaxObjects and MaxDepth, plus 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 other than Tf font operands remain unsupported, and MaxDepth also caps resource-container nesting

Confirmed compatible indirect fonts are reused rather than copied for each repeated pass; existing caller-provided font mappings must retain the same fingerprint

Existing appearance resources retain their local names; an appearance stream without Resources receives a shared snapshot of the source AcroForm default resources, preventing a same-name font in the destination from replacing its original meaning

Checkbox widgets require Off and one matching on-state per widget, with consistent field value and appearance state; absent or null unchecked states are equivalent to Off, and different export states are rejected

Radio widgets require Off and one on-state per widget; ordinary groups keep one destination selection when present and clear imported selections, while 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, when compatible radio groups share logical export values at reordered indices, by order-preserving option union while 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

Public declarations

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;