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; overload;

function OptimizeLoadedStreams(
  FilterFamilies: THPDFLoadedStreamFilterFamilies;
  MinimumSavingsBytes: Int64;
  RecompressionMode: THPDFFlateRecompressionMode;
  out AInfo: THPDFLoadedStreamOptimizationInfo): Integer; overload;

Unit

HPDFDoc

Parameters

NamePurpose
FilterFamiliesSelects unfiltered, Flate, LZW, ASCII85, ASCIIHex, and RunLength candidates
MinimumSavingsBytesPositive minimum byte reduction required before a candidate replaces its source stream
RecompressionModeSelects the standard single-pass path or the high-ratio zlib and Zopfli selection path
AInfoReceives 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

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;
  FilterStatistics: THPDFLoadedStreamFilterStatistics;
  RecompressionMode: THPDFFlateRecompressionMode;
  CompressionTrialCount: Integer;
  ZopfliAttemptCount: Integer;
  ZopfliCompletedCount: Integer;
  ZopfliSelectedCount: Integer;
  ZopfliBudgetExceededCount: Integer;
  ZopfliInputBytes: Int64;
  ZopfliBytesSavedOverBaseline: Int64;
  ZopfliElapsedMilliseconds: Int64;
end;

FilterStatistics records the pre-optimization 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, frmHighRatio, Info);
if Count > 0 then
  PDF.SaveLoadedDocument('optimized.pdf');

Related APIs