function SaveAsSanitised(Stream: TStream;
const Options: TPdfSanitiseOptions;
out Report: TPdfSanitiseReport): Boolean; overload;
function SaveAsSanitised(const FileName: string;
const Options: TPdfSanitiseOptions;
out Report: TPdfSanitiseReport): Boolean; overload;
SaveAsSanitised produces a sanitised single-revision PDF copy that removes active content vectors from the document catalogue and then optionally runs a reachability prune to eliminate orphaned objects from the file body
The two-stage pipeline first strips catalogue entries
(/OpenAction, /AA, /Names,
/AF,
/AcroForm, /Metadata) via
StripDictionaryEntries as an incremental update, then
runs PruneUnreachablePdfStream to rewrite the file as
one clean classic revision
The catalogue and trailer update uses sparse xref lookup, parser windows no larger than 1 MiB, and 64 KiB range copying on the normal xref path instead of materialising the complete saved PDF in one byte array
When pruning is enabled, the intermediate workspace is a
TPdfRandomAccessStore governed by
SaveMemoryLimit and SaveTemporaryDirectory
Its peak memory and spill state are included in
LastSaveLargestMemoryBuffer and
LastSaveSpillCount
Every strip flag is independent
TPdfSanitiseOptions.Default enables the full security
profile: StripOpenAction,
StripCatalogActions, StripNames,
StripAssociatedFiles, plus
RemoveUnreachable prune. StripAcroForm
and StripMetadata default to False
because they remove form fields and metadata that callers may want
to keep
The output is always a single revision with no incremental
history when RemoveUnreachable=True
Options — TPdfSanitiseOptions record; use Default for the standard security profileReport — receives stripped entry count, pruned object count, byte counts, and any errorStripNames removes the entire /Names dictionary including /JavaScript, /EmbeddedFiles, /Dests and /AP; this is intentionally aggressive for sanitisationStripAssociatedFiles removes the catalogue-level /AF entry and defaults to TrueStripMetadata removes both catalogue /Metadata (XMP) and trailer /Info/AA and /A actions are not stripped in this method; only catalogue-level active content is removedTryReadTrailerInfo guard/AcroForm removes signature fields and invalidates existing signatures
var
Options: TPdfSanitiseOptions;
Report: TPdfSanitiseReport;
begin
Pdf1.FileName := 'C:\Untrusted.pdf';
Pdf1.Active := True;
Options := TPdfSanitiseOptions.Default;
Pdf1.SaveAsSanitised('C:\Sanitised.pdf', Options, Report);
end;