Документация PDFiumPas

CreateXObjectFromPage Метод

Эта запись API сохраняет идентификаторы, сигнатуры, блоки кода и термины PDF в исходном виде.
Компонент: TPdf  ·  Модуль: PDFium
Оборачивает страницу из другого TPdf как повторно используемый дескриптор Form XObject, который можно вставлять в этот документ сколько угодно раз

Синтаксис

function CreateXObjectFromPage(
  Source : TPdf;
  SourcePageIndex: Integer): TPdfXObject;

Описание

CreateXObjectFromPage manufactures a reusable handle that lets one TPdf re-use a page from ANOTHER TPdf as a stamp, watermark, or template overlay. Internally it wraps PDFium's FPDF_NewXObjectFromPage and produces a TPdfXObject Pascal object that closes the underlying FPDF_XOBJECT handle automatically in its destructor, eliminating manual FPDF_CloseXObject ceremony.

Pair it with InsertFormObjectFromXObject to actually drop one instance of the wrapped page into the current page of this document. Each call inserts a fresh FPDF_PAGEOBJECT that can be positioned, scaled, and rotated through FPDFPageObj_SetMatrix and the TPdfMatrix helpers — so the same XObject can be placed at multiple sizes / rotations / positions across many pages while sharing a single page-content cache.

Returns nil on failure (Source not Active, invalid SourcePageIndex, OOM). Raises EPdfError when Source is nil or not Active.

Caller owns the returned TPdfXObject and MUST free it before THIS TPdf is closed. The destructor calls FPDF_CloseXObject, which detaches the handle from this TPdf cleanly. After insertion the page object inserted into the page is owned by the page, not by the XObject — closing the XObject does not invalidate already-inserted page objects.

Примечания

Пример

uses FPdfMatrix;

var
  Watermark: TPdf;
  XObj: TPdfXObject;
  PageObj: FPDF_PAGEOBJECT;
  M: TPdfMatrix;
  I: Integer;
begin
  Watermark := TPdf.Create(nil);
  try
    Watermark.FileName := 'C:\Confidential.pdf';
    Watermark.Active := True;
    XObj := Pdf1.CreateXObjectFromPage(Watermark, 0);
    try
      for I := 1 to Pdf1.PageCount do
      begin
        Pdf1.PageNumber := I;
        PageObj := Pdf1.InsertFormObjectFromXObject(XObj);
        if PageObj = nil then Continue;
        M := TPdfMatrix.Create;
        try
          M.Translate(100, 200);
          M.Scale(0.5, 0.5);
          FPDFPageObj_SetMatrix(PageObj, M.AsMatrix);
        finally
          M.Free;
        end;
        Pdf1.UpdatePage;
      end;
    finally
      XObj.Free;
    end;
  finally
    Watermark.Free;
  end;
end;

См. также

InsertFormObjectFromXObject, ImportPagesByIndex, AddPicture