procedure LoadCustomDocument(AStream: TStream; AOwnsStream: Boolean = False);
LoadCustomDocument is the streaming counterpart to the buffered
LoadDocument overloads. It is backed by PDFium's
FPDF_LoadCustomDocument entry point through the
TPdfStreamAdapter helper class introduced in v1.8.0.
PDFium pulls blocks from AStream only when needed, so the
stream is not copied into memory: a TFileStream over a 4 GB
PDF, an HTTP-backed TStream, or a database BLOB stream all become
usable without staging the entire payload in RAM first.
AStream must support seeking. Its lifetime must extend
until the document is closed (call UnloadDocument or set
Active back to False). Pass
AOwnsStream = True to hand stream ownership over to this
TPdf — the adapter then frees AStream when
UnloadDocument runs. Pass False when the
caller wants to keep the stream alive after the document closes.
LoadCustomDocument is intentionally a separate method (not another
LoadDocument overload) so callers passing a TMemoryStream
subclass cannot accidentally bind to the buffered
LoadDocument(TMemoryStream, Boolean) overload — the
Pascal argument shape is identical but the semantics are very
different (buffered vs. on-demand).
Password before calling LoadCustomDocument when opening encrypted documents.AStream — it can be a TFileStream, a TMemoryStream, or any TStream subclass that honours Seek and Read.AOwnsStream = True.
// 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;