PDFium Delphi Component Docs

フレームワーク非依存の文書コア

FPdfDocumentCore は RTL ユニットと FPdfAsync だけに依存し、PDFium、FPdfView、VCL、LCL、Graphics、ウィンドウコントロールは import しません

TPdfDocumentCore は独立したネイティブ文書とその入力バイトを所有します。読み込み / ストリームスナップショット、Unicode 抽出、ページ寸法、生 BGRA 描画、空白ページ挿入、ページ削除、回転、上限付きフル保存バイトに対応します

クラシックコンポーネントと統合するときは、既存の TPdf.CreateCoreSnapshot アダプターを使ってください。既存の TPdf とビューアーのソースはそのまま互換を保ち、返されたコアを編集してもソースラッパーは変わりません

ネイティブホストとライフタイム

独立した RTL ホストが PDFium を読み込んで初期化し、TPdfCoreSymbolResolver と IInterface のライフタイムオーナーを BindPdfDocumentCoreRuntime へ渡します。その後、返された IPdfDocumentCoreRuntime をコア文書へ渡します

リゾルバーはどの文書を開くよりも前に、必須エクスポートをすべてバインドします。ライフタイムオーナーは、最後の参照が解放されるまで、同じ初期化済みネイティブモジュールとその構成を生かしておく必要があります

コア自体は呼び出し側のネイティブモジュールを初期化も破棄もしません。複数の文書が 1 つのランタイムを共有でき、すべてのハンドル / バッファはランタイムのライフタイムオーナーより先に解放されます

クラシックのスナップショットアダプターは構成済みラッパーモジュールを固定し、コアランタイムが生きている間は明示的な UnloadLibrary を拒否します。ホストやユニットの終了前にコアを解放してください

PDFium 呼び出しの同期は引き続き呼び出し側の責任です。ランタイムとコア文書はバックグラウンド処理を導入せず、ネイティブ API をスレッドセーフにもしません

文書操作

LoadBytes はソースバイトをコピーし、ネイティブ読み込みとページ / フォームの確認が済んだ後でのみ新しい文書を受け入れます。無効な入力やキャンセルでは、以前のアクティブな文書を保持します

LoadStream は入力予算の下でストリーム全体をオフセット 0 から読み取り、置き換え文書を受け入れる前に元の位置を復元します。LoadFile は書き込み共有を拒否してファイルを開き、LoadStream に委譲します

パスワードは埋め込み null を含まない、明示的なネイティブ UTF-8 バイト文字列です。Password と SecurityHandlerRevision はアクティブな文書のセキュリティ状態を検査し、SaveBytes(RemoveSecurity) は保存時に暗号化を保持または剥がします。InsertBlankPage、DeletePage、RotatePage はパスワード保護された文書の暗号化を保持しつつ、署名付き文書は拒否します

XFA の初期化、JavaScript ホスト UI、フォーム入力オーバーレイ、widget 編集はクラシックコンポーネントの責任のままです。コアの描画はフォーム入力環境なしで、ネイティブページ内容と通常の注釈を描画します

すべてのページインデックスは 0 始まりです。PageText は、生成された抽出用空白を含む実際のネイティブ UTF-16 テキストを、ネイティブ文字数と UTF-16 出力の両上限の下で返します。GetMetaText は標準の Info 辞書タグ(Title、Author、Subject、Keywords、Creator、Producer、CreationDate、ModDate など)を上限付き UTF-16 テキストとして読み取り、GetPageLabel は 0 始まりページのラベル文字列を読み取ります。GetFileVersion、GetDocPermissions、GetUserPermissions は PDF ヘッダーのバージョン(PDF 1.7 なら 17 など)と権限ビットマスクを検査します

GetMediaBox、GetCropBox、GetTrimBox、GetBleedBox、GetArtBox、GetPageBoundingBox はページのバウンディングボックスを TPdfCoreBox(Left、Bottom、Right、Top)として読み取り、ボックスがなければ False を返します。SetMediaBox、SetCropBox、SetTrimBox、SetBleedBox、SetArtBox は有限の正の寸法を検証して、段階編集パイプライン内で対象ページを更新します。FlattenPage は段階編集パイプライン内でページの注釈かウィジェットをフラット化します(フラット化したら 1、必要がなければ 2 を返す)

ImportPages は全ページか 1 始まりの PDFium ページ範囲文字列(1,3-5 など)をインポートし、ImportPagesByIndex は別のアクティブな非フォーム TPdfDocumentCore から 0 始まりのソースページインデックス配列を、段階編集パイプライン内の 0 始まり挿入位置(-1 で末尾追記)へインポートします。MaxPages とバイト予算は強制されます

GetBookmarks は循環保護付きで文書のアウトライン階層をたどり、Title、0 始まりの Depth、ChildCount、IsOpen、0 始まりの TargetPageIndex(または -1)、ActionUri を持つ TPdfCoreBookmarks 項目を返します。GetAttachmentCount、GetAttachmentInfo、AddAttachment、DeleteAttachment は段階編集パイプライン内で埋め込みファイル添付を検査・変更します。GetSignatureCount と GetSignatureInfo はデジタル署名オブジェクト(SubFilter、Time、Reason、DocMDPPermission、ByteRange、Contents)を検査します。GetPageCharCount、GetPageCharUnicode、GetPageCharBox、FindPageText は文字ごとのコードポイント、ユーザー空間のバウンディングボックス、部分文字列検索の一致を公開します。SaveBytesWithVersion、SaveToStream、SaveToFile はオプションで PDF ヘッダーバージョンを上書き(10..20)して、非インクリメンタルなスナップショットをエクスポートします

ページ編集にはインタラクティブフォームを持たない文書が必要です。フォームを考慮した編集はクラシックラッパーに残ります

InsertBlankPage、DeletePage、RotatePage は非公開のフル保存 / 再オープン段階で動作し、ネイティブのジオメトリ / 数の確認とキャンセル確認を通った後でのみアクティブな文書を置き換えます

SaveBytes は非インクリメンタルなスナップショットを返します。ネイティブライターコールバックは出力予算を強制し、コールバック例外を捕捉します。Pascal 例外が C 境界を越えることはありません

生ピクセルと浮動小数点状態

RenderPage は、上から下へ密に詰めたネイティブ BGRA バイトと Width、Height、Stride を持つ TPdfCorePixels を返します。Stride は Width の 4 倍で、ビットマップやキャンバスのオブジェクトは作成しません

背景はパック済み ARGB 値で、既定は不透明な白です。Rotation は 0 から 3 の 4 分の 1 回転で、描画プロファイルには通常の注釈が含まれます

ネイティブ操作は浮動小数点トラップを一時的にマスクし、ネイティブの保留例外フラグをクリアしてから、呼び出し側の元の例外マスクを復元します。これにより、呼び出し側のトラップポリシーを恒久変更せずに、FPC でも有効なネイティブフォント操作が行えます

有限の上限と品質確認

TPdfDocumentCoreOptions.Default は入出力 128 MiB、10,000 ページ、16,777,216 ネイティブ文字 / UTF-16 出力単位、16,777,216 描画ピクセルを許容します

ハード上限は入出力 512 MiB、100,000 ページ、16,777,216 文字 / UTF-16 出力単位、符号付きバッファサイズの制約を受ける 268,435,456 ピクセル、挿入ページと描画寸法ごとに 32,768 ポイント / ピクセルです

協調的キャンセルはネイティブ呼び出しの前後とライターコールバック内で確認します。ネイティブの読み込み、テキスト、描画の内部処理は呼び出し途中で中断しません

単体の RTL プローブは、宣言された置換 DLL により Delphi と FPC の Win64 で合格します。クラシックラッパー / ビューアーブリッジの品質確認は Delphi Win64 で別途行われ、その他のプラットフォームは文書化された品質確認の限界を保持します

公開宣言

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;