PDFium Delphi Component Docs

Framework'ten bağımsız belge çekirdeği

FPdfDocumentCore yalnızca RTL unit'lerine ve FPdfAsync'e bağlıdır; PDFium, FPdfView, VCL, LCL, Graphics ya da pencere denetimlerini import etmez

TPdfDocumentCore bağımsız bir yerel belgeyi ve girdi baytlarını sahiplenir; yükleme/akış snapshot'ları, Unicode ayıklama, sayfa boyutları, ham BGRA render, boş sayfa ekleme, sayfa silme, döndürme ve sınırlı tam kaydetme baytlarını destekler

Klasik bileşenle entegrasyon için mevcut TPdf.CreateCoreSnapshot adaptörünü kullanın; mevcut TPdf ve viewer kaynakları uyumlu kalır ve döndürülen core'u düzenlemek kaynak sarmalayıcıyı değiştirmez

Yerel ana bilgisayar ve yaşam süresi

Bağımsız bir RTL host, PDFium'u yükleyip ilklendirir; BindPdfDocumentCoreRuntime'a bir TPdfCoreSymbolResolver ve IInterface ömür sahibi verir, ardından döndürülen IPdfDocumentCoreRuntime'ı core belgelere iletir

Resolver, hiçbir belge açılmadan önce tüm gerekli export'ları bağlar; ömür sahibi, son referansı serbest bırakılana kadar aynı ilklendirilmiş yerel modülü ve yapılandırmasını canlı tutmak zorundadır

Core, hiçbir zaman çağıranın yerel modülünü kendisi ilklendirmez ya da yok etmez; birden fazla belge tek runtime'ı paylaşabilir ve tüm handle/buffer'lar runtime'ın ömür sahibinden önce serbest bırakılır

Klasik snapshot adaptörü, yapılandırılmış sarmalayıcı modülü sabitler ve core runtime'ları canlı kaldığı sürece açık UnloadLibrary çağrılarını reddeder; host/unit kapanmadan önce core'ları serbest bırakın

PDFium çağrıları çağıran tarafından senkronize edilir; runtime'lar ve core belgeler arka plan işi başlatmaz ya da yerel API'leri thread-safe yapmaz

Belge işlemleri

LoadBytes kaynak baytları kopyalar ve yeni belgeyi ancak yerel yükleme ile sayfa/form denetimlerinden sonra kabul eder; geçersiz girdi ya da iptal, önceki etkin belgeyi korur

LoadStream akışı, girdi bütçesi altında sıfır ofsetinden baştan sona okur ve yerine konacak belgeyi kabul etmeden önce özgün konumu geri alır; LoadFile, yazma paylaşımı engellenmiş şekilde bir dosya açar ve işi LoadStream'e devreder

Parolalar gömülü null içermeyen, açıkça verilmiş yerel UTF-8 bayt dizileridir; Password ve SecurityHandlerRevision etkin belgenin güvenlik durumunu inceler, SaveBytes(RemoveSecurity) kaydederken şifrelemeyi korur ya da söker, InsertBlankPage, DeletePage ve RotatePage ise parola korumalı belgelerde şifrelemeyi korurken imzalı belgeleri reddeder

XFA ilklendirmesi, JavaScript host arayüzü, form doldurma katmanları ve widget düzenlemesi klasik bileşenin sorumluluğunda kalır; core render, form doldurma ortamı olmaksızın yerel sayfa içeriğini ve normal anotasyonları çizer

Tüm sayfa indeksleri sıfır tabanlıdır; PageText, hem yerel karakter hem UTF-16 çıktı sınırları altında, üretilen ayıklama boşlukları dahil gerçek yerel UTF-16 metnini döndürür; GetMetaText standart Info sözlüğü etiketlerini (Title, Author, Subject, Keywords, Creator, Producer, CreationDate ve ModDate gibi) sınırlı UTF-16 metni olarak okur, GetPageLabel sıfır tabanlı sayfanın etiket dizgesini okur, GetFileVersion, GetDocPermissions ve GetUserPermissions ise PDF başlık sürümünü (PDF 1.7 için 17 gibi) ve izin bit maskelerini inceler

GetMediaBox, GetCropBox, GetTrimBox, GetBleedBox, GetArtBox ve GetPageBoundingBox, sayfa sınırlayıcı kutularını TPdfCoreBox içine (Left, Bottom, Right, Top) okur ve kutu yokken False döndürür; SetMediaBox, SetCropBox, SetTrimBox, SetBleedBox ve SetArtBox, sonlu ve pozitif kutu boyutlarını doğrulayıp hedef sayfayı aşamalı düzenleme hattında günceller; FlattenPage ise aşamalı düzenleme hattında sayfa anotasyonlarını ya da widget'ları düzleştirir (düzleştirildiyse 1, düzleştirilecek bir şey yoksa 2 döndürür)

ImportPages tüm sayfaları ya da bir tabanlı bir PDFium sayfa aralığı dizgesini (1,3-5 gibi) içe aktarır; ImportPagesByIndex, başka bir etkin, formsuz TPdfDocumentCore'dan sıfır tabanlı kaynak sayfa indekslerinin açık bir dizisini, aşamalı düzenleme hattı içinde sıfır tabanlı bir ekleme indeksiyle (ya da -1 ile sonda) içe aktarır; MaxPages ve bayt bütçeleri uygulanır

GetBookmarks, belge outline hiyerarşisini döngü korumasıyla gezer ve Title, sıfır tabanlı Depth, ChildCount, IsOpen, sıfır tabanlı TargetPageIndex (ya da -1) ve ActionUri taşıyan TPdfCoreBookmarks öğelerini döndürür; GetAttachmentCount, GetAttachmentInfo, AddAttachment ve DeleteAttachment, gömülü dosya eklerini aşamalı düzenleme hattında inceler ve değiştirir; GetSignatureCount ve GetSignatureInfo dijital imza nesnelerini inceler (SubFilter, Time, Reason, DocMDPPermission, ByteRange ve Contents); GetPageCharCount, GetPageCharUnicode, GetPageCharBox ve FindPageText, karakter başına kod noktalarını, kullanıcı-uzayı sınırlayıcı kutularını ve alt dize arama eşleşmelerini açığa çıkarır; SaveBytesWithVersion, SaveToStream ve SaveToFile ise isteğe bağlı PDF başlık sürümü geçersiz kılmasıyla (10..20) artımlı olmayan snapshot'lar dışa aktarır

Sayfa düzenlemeleri etkileşimli form içermeyen bir belge gerektirir; forma duyarlı düzenleme klasik sarmalayıcıda kalır

InsertBlankPage, DeletePage ve RotatePage özel bir tam kaydetme/yeniden açma aşamasında çalışır ve etkin belgeyi ancak yerel geometri/sayı denetimleri ile iptal denetimleri geçtikten sonra değiştirir

SaveBytes artımlı olmayan bir snapshot döndürür; yerel writer callback'leri çıktı bütçesini uygular ve callback istisnalarını yakalar, Pascal istisnalarının C sınırını geçmesine izin vermez

Ham pikseller ve kayan nokta durumu

RenderPage, sıkıca paketlenmiş yukarıdan aşağıya yerel BGRA baytları, Width, Height ve Stride içeren bir TPdfCorePixels döndürür; Stride, Width'in dört katıdır ve hiçbir bitmap/canvas nesnesi oluşturulmaz

Arka plan, paketlenmiş bir ARGB değeridir ve varsayılanı opak beyazdır; Rotation 0-3 arası çeyrek turdur ve render profili normal anotasyonları kapsar

Yerel işlemler kayan nokta trap'lerini geçici olarak maskeler ve çağıranın özgün istisna maskesini geri yüklemeden önce yerel bekleyen istisna bayraklarını temizler; bu, FPC altında geçerli yerel yazı tipi işlemlerine, çağıranın trap politikasını kalıcı olarak değiştirmeden izin verir

Sonlu sınırlar ve nitelendirme

TPdfDocumentCoreOptions.Default, 128 MiB girdi/çıktı, 10,000 sayfa, 16,777,216 yerel karakter/UTF-16 çıktı birimi ve 16,777,216 render pikseline izin verir

Sert üst sınırlar; 512 MiB girdi/çıktı, 100,000 sayfa, 16,777,216 karakter/UTF-16 çıktı birimi, işaretili buffer boyutuna tabi 268,435,456 piksel ve eklenen sayfa/render boyutu başına 32,768 nokta/pikseldir

İşbirlikçi iptal, yerel çağrıların çevresinde ve writer callback'lerinin içinde denetlenir; yerel yükleme, metin ve render iç işlemleri çağrı ortasında kesilmez

Bağımsız RTL probu, bildirilen yedek DLL ile Delphi ve FPC Win64 üzerinde geçer; klasik sarmalayıcı/viewer köprüsü Delphi Win64 üzerinde ayrıca nitelendirilir, diğer platformlar belgelenmiş nitelendirme sınırlarını korur

Ortak bildirimler

uses SysUtils, Classes, FPdfAsync;

type
  EPdfDocumentCore=class(Exception);
{$IFDEF MSWINDOWS}
  TPdfCoreUnsignedLong=Cardinal;
{$ELSE}
  TPdfCoreUnsignedLong=NativeUInt;
{$ENDIF}
  TPdfCoreSymbolResolver=function(const Name:AnsiString):Pointer of object;
  TPdfCoreLoadMemory=function(Data:Pointer; Size:NativeUInt; Password:PAnsiChar):Pointer; cdecl;
  TPdfCoreClose=procedure(Handle:Pointer); cdecl;
  TPdfCoreCount=function(Handle:Pointer):Integer; cdecl;
  TPdfCoreLoadPage=function(Document:Pointer; Index:Integer):Pointer; cdecl;
  TPdfCoreDimension=function(Page:Pointer):Single; cdecl;
  TPdfCoreTextPage=function(Page:Pointer):Pointer; cdecl;
  TPdfCoreReadText=function(TextPage:Pointer; Start,Count:Integer; Buffer:PWideChar):Integer; cdecl;
  TPdfCoreBitmapCreate=function(Width,Height,Format:Integer; Buffer:Pointer; Stride:Integer):Pointer; cdecl;
  TPdfCoreBitmapFill=function(Bitmap:Pointer; Left,Top,Width,Height:Integer; Color:TPdfCoreUnsignedLong):Integer; cdecl;
  TPdfCoreRender=procedure(Bitmap,Page:Pointer; X,Y,Width,Height,Rotation,Flags:Integer); cdecl;
  TPdfCoreLastError=function:TPdfCoreUnsignedLong; cdecl;
  PPdfCoreFileWrite=^TPdfCoreFileWrite;
  TPdfCoreWriteBlock=function(Self:PPdfCoreFileWrite; Buffer:Pointer; Size:TPdfCoreUnsignedLong):Integer; cdecl;
  TPdfCoreFileWrite=record
    Version:Integer;
    WriteBlock:TPdfCoreWriteBlock;
  end;
  TPdfCoreSave=function(Document:Pointer; Writer:PPdfCoreFileWrite; Flags:TPdfCoreUnsignedLong):Integer; cdecl;
  TPdfCoreNewDocument=function:Pointer; cdecl;
  TPdfCoreNewPage=function(Document:Pointer; Index:Integer; Width,Height:Double):Pointer; cdecl;
  TPdfCoreDeletePage=procedure(Document:Pointer; Index:Integer); cdecl;
  TPdfCoreRotatePage=procedure(Page:Pointer; Rotation:Integer); cdecl;
  TPdfCoreMetaText=function(Document:Pointer; Tag:PAnsiChar; Buffer:PWideChar; BufferLen:TPdfCoreUnsignedLong):TPdfCoreUnsignedLong; cdecl;
  TPdfCorePageLabel=function(Document:Pointer; PageIndex:Integer; Buffer:PWideChar; BufferLen:TPdfCoreUnsignedLong):TPdfCoreUnsignedLong; cdecl;
  TPdfCoreFileVersion=function(Document:Pointer; var FileVersion:Integer):Integer; cdecl;
  TPdfCorePermissions=function(Document:Pointer):TPdfCoreUnsignedLong; cdecl;
  TPdfCoreGetPageBox=function(Page:Pointer; var Left,Bottom,Right,Top:Single):Integer; cdecl;
  TPdfCoreSetPageBox=procedure(Page:Pointer; Left,Bottom,Right,Top:Single); cdecl;
  TPdfCoreRectF=record Left,Top,Right,Bottom:Single end;
  TPdfCoreGetPageBoundingBox=function(Page:Pointer; var Rect:TPdfCoreRectF):Integer; cdecl;
  TPdfCoreFlattenPage=function(Page:Pointer; Flag:Integer):Integer; cdecl;
  TPdfCoreImportPages=function(DestDoc,SrcDoc:Pointer; PageRange:PAnsiChar; Index:Integer):Integer; cdecl;
  TPdfCoreImportPagesByIndex=function(DestDoc,SrcDoc:Pointer; PageIndices:PInteger; Length:TPdfCoreUnsignedLong; Index:Integer):Integer; cdecl;
  TPdfCoreSaveWithVersion=function(Document:Pointer; Writer:PPdfCoreFileWrite; Flags:TPdfCoreUnsignedLong; FileVersion:Integer):Integer; cdecl;
  TPdfCoreBookmarkRel=function(Document,Handle:Pointer):Pointer; cdecl;
  TPdfCoreBufferRead=function(Handle,Buffer:Pointer; BufferLen:TPdfCoreUnsignedLong):TPdfCoreUnsignedLong; cdecl;
  TPdfCoreActionUriPath=function(Document,Action,Buffer:Pointer; BufferLen:TPdfCoreUnsignedLong):TPdfCoreUnsignedLong; cdecl;
  TPdfCoreDestPageIndex=function(Document,Dest:Pointer):Integer; cdecl;
  TPdfCoreAddAttachment=function(Document:Pointer; Name:PWideChar):Pointer; cdecl;
  TPdfCoreDeleteAttachment=function(Document:Pointer; Index:Integer):Integer; cdecl;
  TPdfCoreAttachmentSetFile=function(Attachment,Document,Contents:Pointer; Len:TPdfCoreUnsignedLong):Integer; cdecl;
  TPdfCoreAttachmentGetFile=function(Attachment,Buffer:Pointer; BufferLen:TPdfCoreUnsignedLong; var OutBufLen:TPdfCoreUnsignedLong):Integer; cdecl;
  TPdfCoreTextUnicode=function(TextPage:Pointer; Index:Integer):Cardinal; cdecl;
  TPdfCoreTextCharBox=function(TextPage:Pointer; Index:Integer; var Left,Right,Bottom,Top:Double):Integer; cdecl;
  TPdfCoreTextFindStart=function(TextPage:Pointer; FindWhat:PWideChar; Flags:TPdfCoreUnsignedLong; StartIndex:Integer):Pointer; cdecl;
  TPdfCoreSigByteRange=function(Signature:Pointer; Buffer:PInteger; Length:TPdfCoreUnsignedLong):TPdfCoreUnsignedLong; cdecl;
  TPdfCoreSigPermission=function(Signature:Pointer):Cardinal; cdecl;
  TPdfCoreApi=record
    LoadMemory:TPdfCoreLoadMemory;
    CloseDocument,ClosePage,CloseTextPage,DestroyBitmap,TextFindClose:TPdfCoreClose;
    PageCount,CharacterCount,FormType,SignatureCount,SecurityRevision,PageRotation,
      BookmarkGetCount,AttachmentCount,TextFindNext,TextGetSchResultIndex,TextGetSchCount:TPdfCoreCount;
    LoadPage,GetAttachment,GetSignatureObject:TPdfCoreLoadPage;
    PageWidth,PageHeight:TPdfCoreDimension;
    LoadTextPage,BookmarkGetAction:TPdfCoreTextPage;
    ReadText:TPdfCoreReadText;
    CreateBitmap:TPdfCoreBitmapCreate;
    FillBitmap:TPdfCoreBitmapFill;
    Render:TPdfCoreRender;
    LastError:TPdfCoreLastError;
    Save:TPdfCoreSave;
    SaveWithVersion:TPdfCoreSaveWithVersion;
    NewDocument:TPdfCoreNewDocument;
    NewPage:TPdfCoreNewPage;
    DeletePage:TPdfCoreDeletePage;
    RotatePage:TPdfCoreRotatePage;
    MetaText,AttachmentGetStringValue:TPdfCoreMetaText;
    PageLabel:TPdfCorePageLabel;
    FileVersion:TPdfCoreFileVersion;
    DocPermissions,UserPermissions,ActionGetType:TPdfCorePermissions;
    GetMediaBox,GetCropBox,GetTrimBox,GetBleedBox,GetArtBox:TPdfCoreGetPageBox;
    SetMediaBox,SetCropBox,SetTrimBox,SetBleedBox,SetArtBox:TPdfCoreSetPageBox;
    GetPageBoundingBox:TPdfCoreGetPageBoundingBox;
    FlattenPage:TPdfCoreFlattenPage;
    ImportPages:TPdfCoreImportPages;
    ImportPagesByIndex:TPdfCoreImportPagesByIndex;
    BookmarkGetFirstChild,BookmarkGetNextSibling,BookmarkGetDest,ActionGetDest:TPdfCoreBookmarkRel;
    BookmarkGetTitle,AttachmentGetName,SigGetContents,SigGetSubFilter,SigGetReason,SigGetTime:TPdfCoreBufferRead;
    ActionGetUriPath:TPdfCoreActionUriPath;
    DestGetPageIndex:TPdfCoreDestPageIndex;
    AddAttachment:TPdfCoreAddAttachment;
    DeleteAttachment:TPdfCoreDeleteAttachment;
    AttachmentSetFile:TPdfCoreAttachmentSetFile;
    AttachmentGetFile:TPdfCoreAttachmentGetFile;
    TextGetUnicode:TPdfCoreTextUnicode;
    TextGetCharBox:TPdfCoreTextCharBox;
    TextFindStart:TPdfCoreTextFindStart;
    SigGetByteRange:TPdfCoreSigByteRange;
    SigGetDocMDPPermission:TPdfCoreSigPermission;
  end;
  IPdfDocumentCoreRuntime=interface
    ['{92C8C1EB-5EE2-437D-A2A1-EF50DBB30A68}']
    function Api:TPdfCoreApi;
  end;
  TPdfDocumentCoreOptions=record
    MaxDocumentBytes,MaxOutputBytes,MaxPages,MaxCharacters,MaxRenderPixels:Integer;
    CancellationToken:IPdfCancellationToken;
    class function Default:TPdfDocumentCoreOptions; static;
  end;
  TPdfCorePageSize=record Width,Height:Double end;
  TPdfCoreBox=record Left,Bottom,Right,Top:Double end;
  TPdfCorePixels=record
    Width,Height,Stride:Integer;
    Samples:TBytes;
  end;
  TPdfCoreBookmarkInfo=record
    Title:WideString;
    Depth,ChildCount,TargetPageIndex:Integer;
    IsOpen:Boolean;
    ActionUri:AnsiString;
  end;
  TPdfCoreBookmarks=array of TPdfCoreBookmarkInfo;
  TPdfCoreAttachmentInfo=record
    Name,CreationDate,ModificationDate,Checksum:WideString;
    Data:TBytes;
  end;
  TPdfCoreTextMatch=record
    StartIndex,Count:Integer;
  end;
  TPdfCoreTextMatches=array of TPdfCoreTextMatch;
  TPdfCoreSignatureInfo=record
    SubFilter,Time:AnsiString;
    Reason:WideString;
    DocMDPPermission:Cardinal;
    ByteRange:array of Integer;
    Contents:TBytes;
  end;
  TPdfDocumentCore=class
  private
    FRuntime:IPdfDocumentCoreRuntime;
    FApi:TPdfCoreApi;
    FOptions:TPdfDocumentCoreOptions;
    FDocument:Pointer;
    FSource:TBytes;
    procedure Check;
    procedure RequireActive;
    function GetActive:Boolean;
    function GetPageCount:Integer;
    function OpenPage(Index:Integer):Pointer;
    procedure AcceptDocument(Document:Pointer; const Source:TBytes);
    procedure ChangePage(Kind,Index,QuarterTurns:Integer; Width,Height:Double);
    procedure ChangePageBox(IsCrop:Boolean; Index:Integer; const Box:TPdfCoreBox);
  public
    constructor Create(const Runtime:IPdfDocumentCoreRuntime; const Options:TPdfDocumentCoreOptions);
    destructor Destroy; override;
    procedure Close;
    procedure LoadBytes(const Data:TBytes; const Password:AnsiString='');
    procedure LoadStream(Source:TStream; const Password:AnsiString='');
    procedure LoadFile(const FileName:string; const Password:AnsiString='');
    procedure NewDocument;
    function GetMetaText(const Tag:AnsiString):WideString;
    function GetPageLabel(Index:Integer):WideString;
    function GetFileVersion:Integer;
    function GetDocPermissions:Cardinal;
    function GetUserPermissions:Cardinal;
    function GetBookmarks:TPdfCoreBookmarks;
    function GetAttachmentCount:Integer;
    function GetAttachmentInfo(Index:Integer):TPdfCoreAttachmentInfo;
    procedure AddAttachment(const Name:WideString; const Data:TBytes);
    procedure DeleteAttachment(Index:Integer);
    function GetSignatureCount:Integer;
    function GetSignatureInfo(Index:Integer):TPdfCoreSignatureInfo;
    function PageSize(Index:Integer):TPdfCorePageSize;
    function GetMediaBox(Index:Integer; out Box:TPdfCoreBox):Boolean;
    procedure SetMediaBox(Index:Integer; const Box:TPdfCoreBox);
    function GetCropBox(Index:Integer; out Box:TPdfCoreBox):Boolean;
    procedure SetCropBox(Index:Integer; const Box:TPdfCoreBox);
    function GetTrimBox(Index:Integer; out Box:TPdfCoreBox):Boolean;
    procedure SetTrimBox(Index:Integer; const Box:TPdfCoreBox);
    function GetBleedBox(Index:Integer; out Box:TPdfCoreBox):Boolean;
    procedure SetBleedBox(Index:Integer; const Box:TPdfCoreBox);
    function GetArtBox(Index:Integer; out Box:TPdfCoreBox):Boolean;
    procedure SetArtBox(Index:Integer; const Box:TPdfCoreBox);
    function GetPageBoundingBox(Index:Integer; out Box:TPdfCoreBox):Boolean;
    function FlattenPage(Index:Integer; ForPrint:Boolean=False):Integer;
    function PageText(Index:Integer):WideString;
    function GetPageCharCount(Index:Integer):Integer;
    function GetPageCharUnicode(Index,CharIndex:Integer):Cardinal;
    function GetPageCharBox(Index,CharIndex:Integer; out Box:TPdfCoreBox):Boolean;
    function FindPageText(Index:Integer; const Pattern:WideString;
      MatchCase:Boolean=False; MatchWholeWord:Boolean=False):TPdfCoreTextMatches;
    function RenderPage(Index,Width,Height:Integer; Rotation:Integer=0;
      Background:Cardinal=$FFFFFFFF):TPdfCorePixels;
    procedure InsertBlankPage(Index:Integer; Width,Height:Double);
    procedure DeletePage(Index:Integer);
    procedure RotatePage(Index,QuarterTurns:Integer);
    procedure ImportPages(Source:TPdfDocumentCore; const PageRange:AnsiString=''; InsertIndex:Integer=-1);
    procedure ImportPagesByIndex(Source:TPdfDocumentCore; const PageIndices:array of Integer; InsertIndex:Integer=-1);
    function SaveBytes(RemoveSecurity:Boolean=False):TBytes;
    function SaveBytesWithVersion(FileVersion:Integer; RemoveSecurity:Boolean=False):TBytes;
    procedure SaveToStream(Dest:TStream; RemoveSecurity:Boolean=False; FileVersion:Integer=0);
    procedure SaveToFile(const FileName:string; RemoveSecurity:Boolean=False; FileVersion:Integer=0);
    property Active:Boolean read GetActive;
    property PageCount:Integer read GetPageCount;
    property SecurityHandlerRevision:Integer read GetSecurityHandlerRevision;
    property Password:AnsiString read FPassword;
  end;

// The lifetime object owns an already initialized native library
// The runtime pins it until every core document releases its reference
function BindPdfDocumentCoreRuntime(Resolver:TPdfCoreSymbolResolver;
  const Lifetime:IInterface):IPdfDocumentCoreRuntime;