FPdfDocumentCore 只依賴 RTL 單元與 FPdfAsync;不會引入 PDFium、FPdfView、VCL、LCL、Graphics 或視窗控制項
TPdfDocumentCore 擁有獨立的原生文件及其輸入位元組;支援載入/串流快照、Unicode 擷取、頁面尺寸、原始 BGRA 算繪、插入空白頁、刪除頁面、旋轉,以及有界的一次性完整儲存位元組
與傳統元件整合時,請使用既有的 TPdf.CreateCoreSnapshot 轉接器;既有 TPdf 與檢視器程式碼保持相容,編輯傳回的核心不會更動來源包裝元件
獨立的 RTL 宿主負責載入並初始化 PDFium,把 TPdfCoreSymbolResolver 與 IInterface 生命週期擁有者交給 BindPdfDocumentCoreRuntime,再把傳回的 IPdfDocumentCoreRuntime 傳給核心文件
解析器必須在任何文件開啟之前綁定所有必要的匯出;生命週期擁有者必須讓同一個已初始化的原生模組及其設定保持存活,直到最後一個參考釋放
核心本身不會初始化或銷毀呼叫端的原生模組;多個文件可共用同一個執行環境,且所有控制代碼/緩衝區都會在執行環境的生命週期擁有者之前釋放
傳統快照轉接器會釘住已設定的包裝模組,只要其核心執行環境仍存活,就拒絕明確的 UnloadLibrary;請在宿主/單元關閉前釋放核心
PDFium 呼叫仍由呼叫端同步;執行環境與核心文件不會引入背景工作,也不會讓原生 API 變成 thread-safe
LoadBytes 會複製來源位元組,只有在原生載入與頁面/表單檢查通過後才接受新文件;輸入無效或取消時,保留先前使用中的文件
LoadStream 在輸入預算內從位移 0 讀取整個串流,並在接受取代文件之前還原原始位置;LoadFile 以拒絕寫入共用的方式開啟檔案,再交給 LoadStream 處理
密碼是明確的原生 UTF-8 位元組字串,不含內嵌 null;Password 與 SecurityHandlerRevision 可檢視使用中文件的安全性狀態,SaveBytes(RemoveSecurity) 在儲存時保留或移除加密,而 InsertBlankPage、DeletePage 與 RotatePage 在密碼保護的文件上保留加密,同時拒絕已簽署的文件
XFA 初始化、JavaScript 宿主 UI、表單填寫疊圖與 Widget 編輯仍由傳統元件負責;核心算繪只畫出原生頁面內容與一般註解,沒有表單填寫環境
所有頁面索引從零起算;PageText 回傳實際的原生 UTF-16 文字,包括擷取產生的空白字元,同時受原生字元數與 UTF-16 輸出上限約束;GetMetaText 以有界 UTF-16 文字讀取標準 Info 字典標籤(如 Title、Author、Subject、Keywords、Creator、Producer、CreationDate 與 ModDate);GetPageLabel 讀取以零起算頁面的標籤字串;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 則在暫存編輯管線中扁平化頁面註解或 Widget(已扁平化傳回 1,無需扁平化傳回 2)
ImportPages 匯入全部頁面或以 1 為基底的 PDFium 頁面範圍字串(如 1,3-5);ImportPagesByIndex 從另一個使用中、非表單的 TPdfDocumentCore,以明確的以零為基底來源頁面索引陣列匯入,插入位置為以零為基底的索引(或 -1 表示附加),全程在暫存編輯管線內進行,並強制執行 MaxPages 與位元組預算
GetBookmarks 以循環防護走訪文件大綱階層,傳回 TPdfCoreBookmarks 項目,含 Title、以零為基底的 Depth、ChildCount、IsOpen、以零為基底的 TargetPageIndex(或 -1)與 ActionUri;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 回傳非增量快照;原生寫入器 Callback 會執行輸出預算檢查並攔截 Callback 例外,不讓 Pascal 例外跨越 C 邊界
RenderPage 回傳 TPdfCorePixels,內含緊密排列、由上而下的原生 BGRA 位元組以及 Width、Height 與 Stride;Stride 等於 Width 乘以四,不會建立任何點陣圖/畫布物件
背景是緊密排列的 ARGB 值,預設為不透明白色;Rotation 為 0 到 3 的四分之一圈數,算繪設定檔包含一般註解
原生操作會暫時遮罩浮點數陷阱並清除原生的擱置例外旗標,再還原呼叫端原本的例外遮罩;這讓 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 點/像素
合作式取消會在原生呼叫前後與寫入器 Callback 內檢查;原生載入、文字與算繪的內部作業不會在呼叫途中被中斷
獨立 RTL 探測已在 Delphi 與 FPC Win64 上以宣告的取代 DLL 通過驗證;傳統包裝元件/檢視器橋接另外在 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;