FlateDecode streams at maximum compression, replacing a stream only when the measured byte saving clears a caller thresholdfunction SaveAsFlateRecompressed(Stream: TStream; const Options: TPdfFlateRecompressOptions; out Report: TPdfFlateRecompressReport; AllowSignedDocument: Boolean = False): Boolean;
function SaveAsFlateRecompressed(const FileName: string; const Options: TPdfFlateRecompressOptions; out Report: TPdfFlateRecompressReport; AllowSignedDocument: Boolean = False): Boolean;
PDFium first writes a non-incremental copy of the current in-memory document, then the wrapper performs a document-wide reachability walk exactly like SaveAsReachable
Each reachable FlateDecode stream is decoded with PdfInflate and re-encoded with DeflateCompress at maximum compression; only streams whose measured saving clears both MinSavingBytes and MinSavingRatio are replaced
Object numbers and generations stay stable, so only the recompressed stream payloads and their direct /Length values change; every surviving cross-reference remains valid without renumbering
The output is a single classic revision with the rebuilt trailer carrying /Root, /Info and /ID forward
TPdfFlateRecompressOptions.MinSavingBytes and MinSavingRatio prevent re-encoding streams that would not save enough, leaving already-optimal streams untouchedTPdfFlateRecompressReport reports recompressed, skipped and failed stream counts plus the original and recompressed stream byte totals/Length entries and streams that fail to decode are skipped or failed without corrupting the documentAllowSignedDocument is explicitly enabled because a full rewrite invalidates existing signature byte ranges
Options.MinSavingBytes:= 64;
Options.MinSavingRatio:= 0.1;
Pdf.SaveAsFlateRecompressed('recompressed.pdf', Options, Report);
WriteLn('recompressed ', Report.RecompressedCount, ' streams');