PDFium Delphi Component Docs

MergeFiles

Component: TPdf  ·  Unit: PDFium
Merges file-backed PDF sources through native page import APIs while bounding source handles and exposing explicit operation policies

Syntax

function MergeFiles(const Sources: TPdfBatchMergeSources; out Report: TPdfBatchMergeReport): Boolean; overload;

function MergeFiles(const Sources: TPdfBatchMergeSources; const Options: TPdfBatchMergeOptions; out Report: TPdfBatchMergeReport): Boolean; overload;

class function TPdfBatchMergeSource.FromFile(const AFileName: string; const APageRange: string = ''; const APassword: string = ''): TPdfBatchMergeSource; static;

class function TPdfBatchMergeOptions.Default: TPdfBatchMergeOptions; static;

Source selection

TPdfBatchMergeSources is a dynamic array of TPdfBatchMergeSource records and is the source list both MergeFiles overloads accept; sources are opened and imported in array order, and the list can be built inline with SetLength and TPdfBatchMergeSource.FromFile

Default options

ErrorPolicybmepStop
FormPolicybmfpPreserveNative
OutlinePolicybmopDiscard
ResourcePolicybmrpSourceScoped
PageChunkSizePdfBatchMergeDefaultPageChunkSize, currently 128 pages
InsertAt-1, which appends to the destination
CreateDestinationTrue
CopyViewerPreferencesTrue
RollbackOnCancelOrFailureTrue

Resource and cancellation policy

bmrpSourceScoped performs one native import per source, allowing PDFium to reuse its cloned-object map across all selected pages from that source

bmrpChunked splits the selection into batches of PageChunkSize, providing finer cancellation points and smaller native calls at the cost of possible cross-chunk resource duplication

At most one source TPdf is open at a time and every source is closed immediately after its import attempt

Cancellation is checked between source operations and native batches; an individual PDFium import call cannot be interrupted

OnProgress receives started, opening, importing, source, rollback, cancellation, failure, and completion stages and can set its Cancel parameter; each TPdfBatchMergeProgress snapshot carries Stage, SourceIndex, SourceCount, FileName, SourcePageCount, ImportedSourceCount, ImportedPageCount, and NativeImportCallCount

The TPdfBatchMergeReport returns Status, SourceCount, AttemptedSourceCount, ImportedSourceCount, SkippedSourceCount, FailedSourceCount, ImportedPageCount, CommittedPageCount, RolledBackPageCount, PageCountBefore, PageCountAfter, NativeImportCallCount, LargestNativeImportPageCount, PeakOpenSourceCount, FormSourceCount, OutlineSourceCount, DestinationCreated, ViewerPreferencesSourceIndex, ErrorMessage, and Issues

CancellationToken accepts the same IPdfCancellationToken contract used by the asynchronous and progressive APIs

Forms, outlines, and viewer preferences

Failure and rollback

A source that fails after one or more chunks has inserted pages is always rolled back to its source boundary before stop or skip handling

bmepSkip records the issue and continues with the next source, while bmepStop terminates the operation

With RollbackOnCancelOrFailure=True, stop failures and cancellation remove all pages inserted by the operation; a destination created by the operation is closed

With rollback disabled, successfully imported sources remain committed and are reflected in CommittedPageCount

Rollback removes inserted pages but cannot guarantee reversal of every native catalog side effect such as producer metadata

Report

Status is bmrsCompleted, bmrsCompletedWithErrors, bmrsCancelled, or bmrsFailed

The report separates attempted, imported, skipped, and failed sources from imported, committed, and rolled-back pages

NativeImportCallCount, LargestNativeImportPageCount, and PeakOpenSourceCount make the chosen scheduling policy measurable

Issues retains source index, file name, and error text for failures and non-fatal terminal callback errors

Example

var
  Sources: TPdfBatchMergeSources;
  Options: TPdfBatchMergeOptions;
  Report: TPdfBatchMergeReport;
begin
  SetLength(Sources, 2);
  Sources[0] := TPdfBatchMergeSource.FromFile('part-a.pdf', '1-10');
  Sources[1] := TPdfBatchMergeSource.FromFile('part-b.pdf', '3,1', Password);
  Options := TPdfBatchMergeOptions.Default;
  Options.ErrorPolicy := bmepSkip;
  Options.FormPolicy := bmfpFlatten;
  if not Pdf.MergeFiles(Sources, Options, Report) then
    Log(Report.ErrorMessage);
end;

ParsePdfBatchMergePageRange exposes the same page-range parser for helper workflows; for transactional outline preservation see FPdfMerge

Use MergePdfFilesWithForms for explicit AcroForm name-collision policies and independent editable values; native MergeFiles retains its existing form import behavior

See Also

ImportPagesByIndex, ImportPages, Asynchronous execution