TPDFlib

核心物件、文件建立、文件管理

描述

TPDFlib(單元 Lib\PDFlibrary.pas)是 PDF Library for Delphi 的核心類別。單一實例可同時持有多份 PDF 文件:NewDocument 開出空白文件、LoadFromFile 載入既有文件、SelectDocument 切換作用中的文件,DocumentCount 則回報目前開了幾份。所有繪圖、文字、表單、註解、安全性、簽章、擷取、描繪、列印與驗證呼叫,都作用在目前選取的文件上

PDFlibrary 單元是總成單元:加進 uses 子句就會拉進整個公開介面,大多數程式因此只需要 Uses PDFlibrary;

語法

Delphi

Uses PDFlibrary;

Var
  PDF: TPDFlib;
Begin
  PDF:= TPDFlib.Create;
  Try
    ...
  Finally
    PDF.Free;
  End;
End;

參數

Create / Destroy實例的生命週期;釋放它會關閉每份開啟的文件
NewDocument開始新的空白 PDF 文件並選取它
LoadFromFile從磁碟載入既有 PDF(可帶密碼)並選取它
SelectDocument把另一份開啟的文件設為作用中文件
DocumentCount實例目前開啟的文件數量
SaveToFile把作用中的文件寫到磁碟(遞增更新會保留較早的簽章)
LastErrorCode最近一次錯誤的數值代碼;上次呼叫成功時為 0
Abort / Aborted以協作方式取消長時間操作,並回報取消狀態
OnProgress / OnOperationProgress長時間操作的粗粒度與細粒度進度回呼
OnPassword加密文件需要密碼時,向宿主要求密碼
LoggerOptional TPDFlibLogger instance receiving structured log records
MailProvider郵件函式使用的選配 IPDFlibMailProvider

備註

本說明文件的參考頁逐一介紹各成員,按任務分組:文件管理、頁面版面、向量圖形、字型與文字(含 HTML 故事與文字流)、影像、表單與 JavaScript、註解與連結、大綱與目的地、安全性與數位簽章、標籤式 PDF、PDF/A 與 PDF/UA 符合性、內容擷取與直接存取、描繪(SelectRenderer 可選 GDI+、Cairo 或 PDFium)、列印、預檢報告,以及透過 TPDFlibViewer 的檢視器整合

大多數成員有三種並行形式:接受 WideString 的 TPDFlib 方法、帶 DL 字首的 DLL 進入點(窄字串呼叫端另有 A 字尾的 ANSI 變體,見 SetAnsiMode),以及 PDFlib library automation 物件上的 ActiveX/COM 方法。各成員頁面會並列顯示 Delphi 與 DLL 語法

範例

// Create a document, draw one line of text and save it
Uses PDFlibrary;

Procedure Demo;
Var
  PDF: TPDFlib;
Begin
  PDF:= TPDFlib.Create;
  Try
    PDF.NewDocument;
    PDF.AddStandardFont('Helvetica');
    PDF.PrintText(50, 750, 'Hello from PDF Library for Delphi');
    PDF.SaveToFile('hello.pdf');
  Finally
    PDF.Free;
  End;
End;

另見

DocumentationHome, GettingStarted, NewDocument, LoadFromFile, SelectDocument, DocumentCount, LastErrorCode, SelectRenderer, TPDFlibViewer, TPDFlibLogger