Editable Diagnostic Documents and Repair
SaveLoadedDocumentDiagnostic produces deterministic, unpacked output for inspection and controlled text editing, while RepairDiagnosticDocument turns the edited file back into a structurally valid PDF
Round-trip workflow
- Load a document and call
SaveLoadedDocumentDiagnostic - Edit object dictionaries or uncompressed stream bytes without trying to maintain
/Length, xref rows, orstartxrefmanually - Call
RepairDiagnosticDocumentwith bounded options - Inspect
THPDFDiagnosticRepairReportand use the repaired target only when the method returnsTrue
Repair contract
- The source must begin with a PDF header and carry the
%QDF-1.0marker emitted by HotPDF - Validated indirect objects are copied in physical order without re-encoding strings, dictionaries, or stream payloads
- Direct, indirect, missing, and stale stream
/Lengthvalues are replaced with the measured payload length - A classic xref table, canonical trailer, and exact
startxrefoffset are rebuilt from measured output positions - The trailer preserves valid
/Info,/ID, and/Encryptvalues and binds/Rootto the single validated Catalog - The target is written through a sibling temporary file and replaces the requested path only after all checks succeed
Strict rejection
The repair fails closed for duplicate object numbers, multiple or missing Catalog objects, broken trailer references, non-comment bytes between objects, invalid object grammar, object or input limits, classic xref offsets wider than ten digits, and strict reload validation failures
Markers inside validated objects are skipped, so object-like text in strings and encoded stream payloads does not become an xref entry
Bounded performance
The scanner reads 1 MiB windows with a short carry, copies object ranges directly between file streams, and retains only validated object metadata plus bounded dictionary probes
MaxInputBytes, MaxObjectCount, and MaxObjectNumber cap scanning, retained metadata, and emitted xref rows before output is published
Pascal example
var
Options: THPDFDiagnosticRepairOptions;
Report: THPDFDiagnosticRepairReport;
begin
Options := THPDFDiagnosticRepairOptions.Default;
Options.MaxInputBytes := Int64(16) * 1024 * 1024 * 1024;
if not THotPDF.RepairDiagnosticDocument(
'edited-diagnostic.pdf',
'repaired.pdf',
Options,
Report
) then
raise Exception.Create(string(Report.Diagnostic));
end;