THotPDF.OptimizeLoadedStreams Method
Rewrites selected loaded stream families as Flate only when the replacement is smaller by the requested threshold
Declaration
function OptimizeLoadedStreams(
FilterFamilies: THPDFLoadedStreamFilterFamilies;
MinimumSavingsBytes: Int64;
out AInfo: THPDFLoadedStreamOptimizationInfo): Integer;
Unit
HPDFDoc
Parameters
| Name | Purpose |
|---|---|
FilterFamilies | Selects unfiltered, Flate, LZW, ASCII85, ASCIIHex, and RunLength candidates |
MinimumSavingsBytes | Positive minimum byte reduction required before a candidate replaces its source stream |
AInfo | Receives scanned, candidate, rewritten, unsupported, failed, before, after, and saved-byte totals |
AInfo | Receives stream totals plus zlib trial and Zopfli attempt, completion, selection, budget, byte-saving, and elapsed-time telemetry |
Return value
Returns the number of stream payloads replaced in the loaded object graph
Usage notes
CompressionLevelselects the zlib speed and ratio, whilstclNonemakes the pass a no-opfrmHighRatioestablishes the smallest of four maximum-compression zlib strategies, then uses budgeted Zopfli block splitting only when it produces a strictly smaller streamZopfliFlateOptions- Decoded and compressed candidates spill to owned temporary files after
LoadedStreamMemoryThreshold, keeping large-document memory bounded - The original payload and dictionary remain unchanged after decode failure or insufficient savings
- Streams with
/DecodeParmsare skipped so predictor and non-default LZW semantics cannot be changed accidentally - Multi-filter chains are eligible only when every family is selected and supported
- Call
SaveLoadedDocumentafter the pass to persist adopted payloads
Types and diagnostics
THPDFLoadedStreamFilterFamily = (lsffUnfiltered, lsffFlate, lsffLZW,
lsffASCII85, lsffASCIIHex, lsffRunLength);
THPDFLoadedStreamFilterFamilies = set of THPDFLoadedStreamFilterFamily;
THPDFLoadedStreamOptimizationInfo = record
ScannedStreamCount: Integer;
CandidateStreamCount: Integer;
RewrittenStreamCount: Integer;
UnsupportedStreamCount: Integer;
DecodeFailureCount: Integer;
BytesBefore: Int64;
BytesAfter: Int64;
BytesSaved: Int64;
end;
FilterStatistics records the pre-optimisation stream count, encoded bytes, and per-family stage attribution without forcing an additional decode pass
Example
Families := [lsffUnfiltered, lsffFlate, lsffLZW];
Count := PDF.OptimizeLoadedStreams(Families, 16, Info);
if Count > 0 then
PDF.SaveLoadedDocument('optimized.pdf');