PDFium Delphi Component Docs

独立于框架的文档 core

FPdfDocumentCore 只依赖 RTL 单元和 FPdfAsync,不导入 PDFium、FPdfView、VCL、LCL、Graphics 或窗口控件

TPdfDocumentCore 独立持有一个原生文档及其输入字节,支持 load/stream 快照、Unicode 提取、页面尺寸、原始 BGRA 渲染、插入空白页、删除页面、旋转以及有界的整体保存字节

与经典组件集成时使用现有的 TPdf.CreateCoreSnapshot 适配器;既有 TPdf 与查看器源保持兼容,编辑返回的 core 不会改动源包装器

原生宿主与生命周期

独立的 RTL 宿主负责加载并初始化 PDFium,向 BindPdfDocumentCoreRuntime 提供 TPdfCoreSymbolResolver 与 IInterface 生命周期所有者,然后把返回的 IPdfDocumentCoreRuntime 交给各个 core 文档

resolver 在打开任何文档之前绑定全部必需的导出;生命周期持有者必须让同一个已初始化的原生模块及其配置保持存活,直到最后一个引用释放

core 从不自行初始化或销毁调用方的原生模块;多个文档可以共享同一个运行时,所有句柄/缓冲区都会在运行时的生命周期持有者之前释放

经典快照适配器会钉住所配置的包装器模块,在其 core 运行时仍存活期间拒绝显式 UnloadLibrary;请在宿主/单元关闭前释放 core

PDFium 调用仍由调用方负责同步;运行时和 core 文档不引入后台工作,也不会让原生 API 变成线程安全

文档操作

LoadBytes 复制源字节,只有原生加载与页面/表单检查都通过后才接受新文档;输入无效或被取消时保留之前的活动文档

LoadStream 在输入预算内从偏移零读取整个流,并在接受替换文档之前恢复原始读取位置;LoadFile 以禁止写入共享的方式打开文件,再委托给 LoadStream

密码是显式的原生 UTF-8 字节串,不含嵌入的 null;Password 与 SecurityHandlerRevision 检查活动文档的安全状态,SaveBytes(RemoveSecurity) 在保存时保留或剥离加密,InsertBlankPage、DeletePage 与 RotatePage 在密码保护文档上保留加密、同时拒绝已签名文档

XFA 初始化、JavaScript 宿主 UI、表单填充覆盖层和 widget 编辑仍是经典组件的职责;core 渲染只绘制原生页面内容和常规注释,不提供表单填充环境

所有页面索引从零开始;PageText 返回真实的原生 UTF-16 文本(包括提取时生成的空白),同时受原生字符数和 UTF-16 输出上限约束;GetMetaText 读取标准 Info 字典标签(如 Title、Author、Subject、Keywords、Creator、Producer、CreationDate 与 ModDate)为有界的 UTF-16 文本,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 按显式的从零起源页面索引数组导入,插入到暂存编辑管线中基于 0 的位置(-1 表示追加),并强制执行 MaxPages 与字节预算

GetBookmarks 带环保护地遍历文档书签层次,返回带 Title、从零的 Depth、ChildCount、IsOpen、从零的 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 返回 TPdfCorePixels,包含紧密排列、自上而下的原生 BGRA 字节以及 Width、Height 和 Stride;Stride 等于 Width 乘以四,不创建任何 bitmap/canvas 对象

Background 是打包的 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 点/像素

协作式取消在原生调用前后以及写入回调内部检查;原生加载、文本和渲染的内部执行不会在调用中途被打断

独立的 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;