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

  1. Load a document and call SaveLoadedDocumentDiagnostic
  2. Edit object dictionaries or uncompressed stream bytes without trying to maintain /Length, xref rows, or startxref manually
  3. Call RepairDiagnosticDocument with bounded options
  4. Inspect THPDFDiagnosticRepairReport and use the repaired target only when the method returns True

Repair contract

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;

Related API