How do I use Optimizer to minimize the size of a PDF?

Question:

I want to make PDF files as small as possible, before I transfer them over a network for storage, and am using Optimizer for this. Sometimes the files are larger then they were before? Also, I don’t want the quality of the PDF to be noticeably worse.

Answer:

The following settings will maximize the likely hood of decreasing the size of the PDF, and maximize the reduction.

Oct 23 2020 Update: PDFNet 8.0 introduces new CCITT mono image compression, which we now recommend over JBIG2. Code below reflects this update.

`
Optimizer.ImageSettings ims = new Optimizer.ImageSettings();
ims.SetCompressionMode(Optimizer.ImageSettings.CompressionMode.e_jpeg);
ims.SetDownsampleMode(Optimizer.ImageSettings.DownsampleMode.e_default);
ims.ForceChanges(false);
ims.ForceRecompression(true);

Optimizer.MonoImageSettings mims = new Optimizer.MonoImageSettings();
mims.SetCompressionMode(Optimizer.MonoImageSettings.CompressionMode.e_ccitt);
mims.SetDownsampleMode(Optimizer.MonoImageSettings.DownsampleMode.e_default);
mims.ForceChanges(false);
mims.ForceRecompression(true);

// Embedding and subsetting fonts is recommended practice, but can make the file larger. By setting EmbedFonts to false, but SubsetFonts to true, PDFNet will convert all
// existing embedded fonts into a minimal sized compact font, while leaving any fonts not embedded already out of the file.
Optimizer.TextSettings ts = new Optimizer.TextSettings();
ts.EmbedFonts(false); // set to true to embed all fonts
ts.SubsetFonts(true);

Optimizer.OptimizerSettings os = new Optimizer.OptimizerSettings();
os.SetColorImageSettings(ims);
os.SetGrayscaleImageSettings(ims);
os.SetMonoImageSettings(mims);
os.SetTextSettings(ts);

Optimizer.Optimize(doc, os);
`

Note that this does not embed fonts that are not already embedded. While this is normally the best practice, as without embedded fonts the correct rendering of the file cannot be garuanteed, it will (if there are missing fonts) cause the file size to grow, which this question explicitly doesn’t want.