SaveAsfunction OptimizeImages(const Options: TPdfImageOptimizeOptions; out Report: TPdfImageOptimizeReport): Boolean;
Unlike the SaveAs* rewrites, OptimizeImages edits the current in-memory document through the native PDFium image-object API, matching the AddImage / GenerateContent editing model
Every page is walked and each image object's effective placement DPI is read from FPDFImageObj_GetImageMetadata; when max(horizontal, vertical) DPI / TargetDpi reaches MinDpiRatio, the image is downsampled
Downsampling measures local luminance detail, selects a box, bilinear, bicubic, Lanczos, or bilevel filter from scale, content class, and preferred quality, and writes the smaller same-format bitmap back with FPDFImageObj_SetBitmap
BGRA samples are accumulated in premultiplied-alpha space and unpremultiplied only at the destination pixel, preventing transparent colour fringes around resampled edges
Each touched page is committed with FPDFPage_GenerateContent; the caller then saves the optimized document with the usual SaveAs call
TPdfImageOptimizeOptions.TargetDpi sets the desired effective DPI; MinDpiRatio prevents re-encoding images that are only marginally above targetPreserveBilevel leaves 1-bit bilevel images (ImageMask / line-art scans) untouched; when disabled, the bilevel filter applies area voting and a binary threshold to preserve crisp edgesMinDimension skips any image whose width or height falls below the threshold, since tiny images are typically icons or decorative elements; the default is 8ResampleFilter selects a fixed filter or pirfAdaptive; ContentClass supplies auto, photo, or line-art intent and PreferredQuality tunes adaptive selection from 0 through 1MaxWorkingBytes bounds each destination bitmap allocation including stride padding; over-budget images remain unchanged and increment BudgetExceededCountSelectPdfImageCodec weighs a TPdfImageCodecCandidate (UniqueColorCount, EntropyBitsPerByte, PlacementDpi, HasAlpha, IsBilevel, OriginalCodec, plus geometry and measured encoded byte lengths) and answers with a TPdfImageCodecDecision whose SelectedBytes and ReplaceOriginal report the winning encodingTPdfImageResampleFilter provides pirfBox, pirfBilinear, pirfBicubic, pirfLanczos, and pirfBilevel fixed choices in addition to adaptive selection; TPdfImageContentClass provides piccAuto, piccPhoto, and piccLineArtOptimizedCount counts the image objects that were actually downsampled and written back through FPDFImageObj_SetBitmap; skipped, failed, and over-budget objects are counted separatelyTPdfImageOptimizeReport includes BoxFilterCount, BilinearFilterCount, BicubicFilterCount, LanczosFilterCount, BilevelFilterCount, PeakWorkingBytes, AverageDetailScore, the OptimizedCount, SkippedCount, FailedCount, and BudgetExceededCount totals, and a structured failure messageSaveAs; OptimizeImages itself only rewrites in-memory image objects
Options:= TPdfImageOptimizeOptions.Default;
Options.TargetDpi:= 150;
Options.ResampleFilter:= pirfAdaptive;
Options.MaxWorkingBytes:= 64 * 1024 * 1024;
Pdf.OptimizeImages(Options, Report);
WriteLn('downsampled ', Report.OptimizedCount, ' images');
Pdf.SaveAs('optimized.pdf');