PDFium Delphi Component Docs

LoadDocument policies and repaired output

Component: TPdf  ·  Unit: PDFium
Open a PDF with compatible, strict, or recovery policy and optionally write an independently verified repaired copy

Syntax

procedure LoadDocument;

 

procedure LoadDocument(Data: Pointer; Size: NativeUInt; Buffered: Boolean = True);

DataPointer. A pointer to the raw PDF data in memory
SizeNativeUInt. The size of the data buffer in bytes
BufferedBoolean. When True (default), TPdf retains its own copy. When False, the buffer must remain valid until the document is unloaded

 

procedure LoadDocument(const Data: TBytes; Buffered: Boolean = True);

DataTBytes. The PDF file content as a byte array
BufferedBoolean. When True (default), TPdf retains its own copy. When False, the byte array must remain valid and unchanged until the document is unloaded

 

procedure LoadDocument(const Data: TBytes; Index, Count: Integer; Buffered: Boolean = True);

DataTBytes. The backing byte array that contains the PDF window
IndexInteger. The zero-based offset of the first PDF byte
CountInteger. The number of PDF bytes to load
BufferedBoolean. When True (default), TPdf copies only the selected window. When False, the backing byte array must remain valid and unchanged until the document is unloaded

 

procedure LoadDocument(Data: TMemoryStream; Buffered: Boolean = True);

DataTMemoryStream. A TMemoryStream containing the PDF data
BufferedBoolean. When True (default), TPdf retains its own copy. When False, the stream memory must remain valid until the document is unloaded

 

procedure LoadDocument(const Options: TPdfLoadOptions; out Report: TPdfLoadReport);

procedure LoadDocument(Data: Pointer; Size: NativeUInt; const Options: TPdfLoadOptions; out Report: TPdfLoadReport; Buffered: Boolean = True);

procedure LoadDocument(const Data: TBytes; const Options: TPdfLoadOptions; out Report: TPdfLoadReport; Buffered: Boolean = True);

procedure LoadDocument(const Data: TBytes; Index, Count: Integer; const Options: TPdfLoadOptions; out Report: TPdfLoadReport; Buffered: Boolean = True);

 

function SaveAsRepaired(Stream: TStream; out Report: TPdfRepairReport; AllowSignedDocument: Boolean = False): Boolean;

function SaveAsRepaired(const FileName: string; out Report: TPdfRepairReport; AllowSignedDocument: Boolean = False): Boolean;

Description

The legacy overloads use compatible mode and preserve their existing behavior while publishing native xref validity and recovery status through LastLoadReport. The no-argument overload reads from the path stored in FileName. The Pointer, TBytes, and TMemoryStream overloads load directly from in-memory data, bypassing the file system. The byte-range overload selects an embedded PDF from a larger container, protocol frame, or reusable receive buffer without creating a temporary TBytes slice

When Buffered is True (default), TPdf copies the supplied bytes into its retained buffer. When False, the caller's buffer must remain valid and unchanged until UnloadDocument or Active := False — this avoids an extra copy but requires careful lifetime management

The byte-range overload rejects negative values, an Index beyond the data length, or a Count that extends past the remaining bytes. The pointer overload rejects nil when Size is nonzero; the TMemoryStream overload rejects a nil stream before any document state is changed

On success, Active becomes True and all page properties reflect the first page (PageNumber = 1). On failure, an EPdfError exception is raised

Load policies

TPdfLoadOptions.Default supplies bounded defaults for source bytes, issues, xref sections, xref entries, and object numbers. PDFium remains the semantic parser in every mode; the wrapper audit exists because the public PDFium ABI reports xref validity but does not expose a switch that disables parser recovery

The audit follows classic xref tables, xref streams, hybrid revisions, and /Prev chains. It reports malformed sections, cycles, conflicting entries, damaged object offsets, missing terminal markers, unsupported stream filters, budget exhaustion, and native recovery. Exceeding a configured audit budget sets AuditComplete to False and produces a warning; it does not by itself claim that unchecked bytes are invalid

InspectAvailability remains the lightweight native availability probe for linearization and page-first loading; it does not replace the structural load report

Reports

TPdfLoadReport includes Status, CrossReferenceTableValid, UsedRecovery, RecoveryRoute, source and structural offsets, section and entry counters, conflict and damage counters, issue totals, truncation state, and a typed Issues array. The same report is stored in LastLoadReport

TPdfRepairReport records whether the source used recovery, output size and page count, output xref validity, terminal error text, and the complete independent strict-load verification report

Repaired output

SaveAsRepaired asks PDFium to write a complete non-incremental copy, then opens that output in an isolated TPdf with plmStrict. The method delivers bytes only after xref validity and page-count consistency pass. The file overload writes through a same-directory temporary file and atomically replaces the destination

A complete rewrite removes prior incremental history and invalidates existing signature byte ranges. Signed documents are rejected unless AllowSignedDocument is explicitly True. The method repairs structures that PDFium can successfully load and rewrite; it does not guarantee recovery of arbitrary damaged content or preservation of digital signatures

Example

// From file
Pdf.FileName := 'C:\docs\file.pdf';
Pdf.LoadDocument;

// From TBytes
var Bytes: TBytes;
Bytes := TFile.ReadAllBytes('file.pdf');
Pdf.LoadDocument(Bytes);

// From a PDF embedded in a larger byte array
Pdf.LoadDocument(Container, PdfOffset, PdfLength);

// From TMemoryStream (unbuffered)
Pdf.LoadDocument(FStream, False);

// Reject hidden parser recovery
Options := TPdfLoadOptions.Default(plmStrict);
Pdf.LoadDocument(Options, Report);

// Rewrite and independently verify a recovered document
Pdf.SaveAsRepaired('C:\docs\repaired.pdf', RepairReport);

See Also

Active, FileName, LoadCustomDocument, SaveAs