FlateDecode 流,仅当测得的字节节省超过调用方阈值时才替换流function 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 先写出当前内存文档的非增量副本,然后包装器像 SaveAsReachable 一样执行文档级可达性遍历
每个可达的 FlateDecode 流用 PdfInflate 解码,并用 DeflateCompress 以最大压缩重新编码;仅当测得的节省同时超过 MinSavingBytes 和 MinSavingRatio 时才替换流
对象号和代次保持稳定,因此只有重新压缩的流负载及其直接 /Length 值会改变;每个存活的交叉引用无需重新编号即可保持有效
输出是单个经典修订,重建的 trailer 将 /Root、/Info 和 /ID 向前携带
TPdfFlateRecompressOptions.MinSavingBytes 和 MinSavingRatio 防止重新编码节省不足的流,使已最优的流保持不动TPdfFlateRecompressReport 报告已重新压缩、已跳过和失败的流数,以及原始和重新压缩流字节总数/Length 条目和解码失败的流会被跳过或标记失败而不损坏文档AllowSignedDocument,否则拒绝签名文档,因为完整重写会使现有签名字节范围失效
Options.MinSavingBytes:= 64;
Options.MinSavingRatio:= 0.1;
Pdf.SaveAsFlateRecompressed('recompressed.pdf', Options, Report);
WriteLn('recompressed ', Report.RecompressedCount, ' streams');