function InsertFormObjectFromXObject(XObject: TPdfXObject): FPDF_PAGEOBJECT;
InsertFormObjectFromXObject is the second half of the v1.25.0 Form
XObject reuse API. It takes a TPdfXObject handle (built
by CreateXObjectFromPage) and inserts ONE FPDF_PAGEOBJECT
that references that XObject into THIS document's CURRENT page. Each
call inserts a new page object, so the same XObject can be stamped
multiple times across multiple pages of the destination —
every stamp shares the underlying page-content cache, which keeps
file size down even when the watermark / template is busy.
The returned FPDF_PAGEOBJECT handle lets the caller
call FPDFPageObj_SetMatrix or the
TPdfMatrix helpers to position / scale / rotate the
stamp anywhere on the page. Without an explicit transform the
inserted page sits at the bottom-left corner at 1:1 scale, which is
almost never what you want; always set a matrix before calling
UpdatePage.
Returns nil on failure (XObject is nil, this TPdf
not Active, no current page). After insertion the page object is
owned by the page, not by the XObject — freeing the XObject
does not invalidate the inserted page object.
PageNumber. Set PageNumber first, then call InsertFormObjectFromXObject.UpdatePage after positioning the stamp so PDFium re-renders the page content stream.
uses FPdfMatrix;
var
PageObj: FPDF_PAGEOBJECT;
M: TPdfMatrix;
begin
Pdf1.PageNumber := 3;
PageObj := Pdf1.InsertFormObjectFromXObject(XObj);
if PageObj <> nil then
begin
M := TPdfMatrix.Create;
try
M.Translate(100, 200);
M.Scale(0.5, 0.5);
M.Rotate(15);
FPDFPageObj_SetMatrix(PageObj, M.AsMatrix);
finally
M.Free;
end;
Pdf1.UpdatePage;
end;
end;