How do I control compression level when embedding images in PDF?

Q:

I’d like to control the compression level (or any compression at all) when embedding images in the pdf document. Can you help point me to the right samples or documentation?

A:

If you are embedding brand new images you may want to take look at AddImage sample project:

http://www.pdftron.com/pdfnet/samplecode.html#AddImage

The sample shows how to embed different types and how to define different compression levels (using encoder hints parameter).

For details re: different encoder hints parameter options please see:

http://www.pdftron.com/pdfnet/PDFNetC/d2/d12/classpdftron_1_1_p_d_f_1_1_image.html#ac98b19bbffd54c4870a9df1e47697568

encoder_hints - An optional SDF::Obj containing a hint (or an array of hints) that could be used to select a specific compression method and compression parameters. For a concrete example of how to create encoder hints, please take a look at JBIG2Test and AddImage sample projects. The image encoder accepts the following hints:

• /JBIG2; SDF::Name(“JBIG2”), An SDF::Name Object with value equal to “JBIG2”. If the image is monochrome (i.e. bpc == 1), the encoder will compress the image using JBIG2Decode filter.

• [/JBIG2 /Threshold 0.6 /SharePages 50] - Compress a monochrome image using lossy JBIG2Decode compression with the given image threshold and by sharing segments from a specified number of pages. The threshold is a floating point number in the range from 0.4 to 0.9. Increasing the threshold value will increase image quality, but may increase the file size. The default value for threshold is 0.85. “SharePages” parameter can be used to specify the maximum number of pages sharing a common ‘JBIG2Globals’ segment stream. Increasing the value of this parameter improves compression ratio at the expense of memory usage.

• [/JPEG] - Use JPEG compression with default compression.

• [/JPEG /Quality 60] - Use JPEG compression with given quality setting. The “Quality” value is expressed on the 0…100 scale.

• [/JP2] or [/JPEG2000] - Use JPEG2000 compression to compress a RGB or a grayscale image

• [/Flate] - Use Flate compression with maximum compression at the expense of speed.

• [/Flate /Level 9] - Use Flate compression using specified compression level. Compression “Level” must be a number between 0 and 9: 1 gives best speed, 9 gives best compression, 0 gives no compression at all (the input data is simply copied a block at a time).

• /RAW or [/RAW] - The encoder will not use any compression method and the image will be stored in the raw format.

If you are dealing with existing PDF files / images you can optimize them (changing compression etc.) as shown in Optimizer sample project: http://www.pdftron.com/pdfnet/samplecode.html#Optimizer