PDFium Delphi Component 文件

LoadCustomDocument 方法

元件: TPdf  ·  單元: PDFium
使用 PDFium 的隨需區塊讀取器,從任何可定位的 TStream 載入 PDF,讓數 GB 的文件與遠端 / 資料庫後端來源不必預先複製到記憶體

Syntax

procedure LoadCustomDocument(AStream: TStream; AOwnsStream: Boolean = False);

procedure LoadCustomDocument(AStream: TStream; AOwnsStream: Boolean; const Options: TPdfLoadOptions; out Report: TPdfLoadReport);

Description

LoadCustomDocument 是緩衝式 LoadDocument 多載的串流對應版本。它藉由 v1.8.0 引入的 TPdfStreamAdapter 輔助類別,以 PDFium 的 FPDF_LoadCustomDocument 進入點為基礎。PDFium 只在需要時才從 AStream 拉取區塊,因此串流不會被複製到記憶體:位於 4 GB PDF 上的 TFileStream、以 HTTP 為後端的 TStream,或資料庫 BLOB 串流都可直接使用,不必先把整個承載暫存在 RAM 中

AStream 必須支援定位(Seek)。其生命週期必須延續到文件關閉為止(呼叫 UnloadDocument 或把 Active 設回 False)。傳入 AOwnsStream = True 可把串流的擁有權交給這個 TPdf — 配接器之後會在 UnloadDocument 執行時釋放 AStream。呼叫端想在文件關閉後繼續保留串流時,請傳入 False。

LoadCustomDocument 刻意設計為獨立方法(而不是另一個 LoadDocument 多載),讓傳入 TMemoryStream 子類別的呼叫端不會意外繫結到緩衝式的 LoadDocument(TMemoryStream, Boolean) 多載 — 兩者的 Pascal 參數外觀相同,語意卻截然不同(緩衝式 vs. 隨需)。

政策多載套用 LoadDocument 所記載的相同相容、嚴格或復原行為,以及有界 xref 稽核。稽核使用定位讀取,並在 PDFium 繼續隨需存取之前還原來源游標

TPdfLoadOptions 攜帶 Mode、AuditByteLimit、MaxIssues、MaxXrefSections、MaxXrefEntries、MaxObjectNumber、ValidateObjectOffsets 與 RequireFinalEndOfFileMarker,以及 Default 類別函式

Remarks

Example

// Streaming load with stream ownership transferred to Pdf1
var
  Stream: TFileStream;
begin
  Stream := TFileStream.Create('C:\Huge.pdf', fmOpenRead or fmShareDenyWrite);
  Pdf1.LoadCustomDocument(Stream, True); // Pdf1 frees Stream on UnloadDocument
  Pdf1.Active := True;
  ShowMessage('Pages: ' + IntToStr(Pdf1.PageCount));
end;

// Externally-owned stream — caller frees it after UnloadDocument
var
  Stream: TFileStream;
begin
  Stream := TFileStream.Create('C:\Live.pdf', fmOpenRead or fmShareDenyNone);
  try
    Pdf1.LoadCustomDocument(Stream, False);
    Pdf1.Active := True;
    DoWork;
    Pdf1.Active := False;
  finally
    Stream.Free;
  end;
end;

See Also

LoadDocument, Active, Password, FileName