function StampPagesFromPdf(
Source : TPdf;
SourcePageIndex : Integer;
const TargetPages : array of Integer;
const Options : TPdfOverlayOptions;
out Report : TPdfOverlayReport): Boolean;
StampPagesFromPdf is the high-level overlay, underlay and
watermark composer. It manufactures a Form XObject from a source
page through FPDF_NewXObjectFromPage, then drops one
positioned, scaled, optionally rotated copy of that XObject into
each requested target page. Placement, scaling, rotation, blend
mode and z-order are controlled through the
TPdfOverlayOptions record (Placement,
X, Y, Scale,
Rotation, Margin, UseCropBox,
ZUnderlay and BlendMode); the
TPdfOverlayReport out parameter records
StampedPageCount, FailedPageCount,
SkippedPageCount, SourcePageWidth,
SourcePageHeight, Succeeded and
ErrorMessage
Internally each stamp combines
FPDF_NewFormObjectFromXObject,
FPDFPageObj_SetMatrix,
FPDFPageObj_SetBlendMode and either
FPDFPage_InsertObject (overlay) or
FPDFPage_InsertObjectAtIndex with stack index 0
(underlay). The placement matrix is built from
PageWidth/PageHeight plus the option
fields, so all anchors, margins, scaling and rotation map to a
single transform that PDFium applies to the inserted form object
Source and destination must both be Active. The
source may be a separate TPdf instance or even THIS
document (self-stamp): the caller's current
PageNumber on both documents is saved and restored
across the call
Source — another active TPdf that owns the page to reuse. May be the same instance as the destinationSourcePageIndex — zero-based index of the page inside SourceTargetPages — zero-based target page indices inside THIS document. Pass an empty array to stamp every pageOptions — a TPdfOverlayOptions record. Use TPdfOverlayOptions.Default for a centred, auto-fit, on-top overlayReport — receives stamped, failed and skipped counts plus the source page width/height used to compute the placement matrixopTopLeft, opTopCenter, opTopRight, opMiddleLeft, opCenter, opMiddleRight, opBottomLeft, opBottomCenter, opBottomRight, opStretch (independent axis scaling) and opCustom (uses explicit X/Y)Scale=0 means auto-fit inside the page box minus the requested margin while preserving aspect ratio, unless Placement=opStretch in which case the image is stretched to fill the boxRotation is anticlockwise in degrees and rotates the placed image around its own centre, so a 45° watermark stays visually centredZUnderlay=True inserts the stamp at stack index 0 so it sits behind every existing page object. The native PDFium API has no public 'move object to index' call, so the underlay path uses a fresh Form XObject inserted directly at the bottom rather than repositioning an appended copyBlendMode is applied to the inserted form object via FPDFPageObj_SetBlendMode. Pure alpha transparency on a Form XObject is not exposed by the public PDFium API; use blend modes such as bmMultiply or bmScreen, or pre-render the source with the desired alpha before stampingReport.SkippedPageCount and do not abort the run
var
Watermark: TPdf;
Options: TPdfOverlayOptions;
Report: TPdfOverlayReport;
begin
Watermark := TPdf.Create(nil);
try
Watermark.FileName := 'C:\Confidential.pdf';
Watermark.Active := True;
Options := TPdfOverlayOptions.Default;
Options.Placement := opCenter;
Options.Rotation := 45;
Options.BlendMode := bmMultiply;
Pdf1.StampPagesFromPdf(Watermark, 0, [], Options, Report);
finally
Watermark.Free;
end;
end;