ImageGear v26.3 - Updated
Developer Guide / How to Work with ... / PDF / How to... / Compress a PDF Document
In This Topic
    Compress a PDF Document
    In This Topic

    ImageGear improves the overall size of saved PDF documents by reducing or removing data structures in a PDF document.

    Refer to our PDF Compression web demo to see ImageGear compression rates for PDF files.

    File size improvements will vary with each PDF file. In some cases, file size reduction is dramatic. By default, ImageGear removes standard fonts embedded in a PDF document for additional compression benefits. Re-saving an optimized PDF file is not expected to further reduce its size. Less frequently, PDF file size may increase due to metadata that is always written when PDF documents are saved.

    WARNING: Saving compressed may invalidate PDF/A and PDF/X compliant documents. Refer to API reference topic ImGearPDFDocument.SaveCompressed() for additional details.

    To compress a PDF document using ImageGear:

    1. Read the PDF document into a System.IO.Stream object.
    2. Read the stream using the ImGearFileFormats.LoadDocument() method, casting the ImGearDocument object returned to an ImGearPDFDocument object.
    3. Save the PDF to disk or memory using the ImGearPDFDocument.SaveCompressed() method.

    If compressing a file would result in an increased file size, the original file is saved instead.

    Refer to the CompressPDFFile sample for an example of how to save a PDF in compressed form. To re-compress a PDF file in memory, use the ImGearPDFDocument.SaveCompressed(Stream) method overload with a System.IO.MemoryStream as argument.

    Compression Controlling Options

    ImageGear provides below options to control compression:

    Downsample High-Resolution Images

    Downsampling replaces higher resolution images with lower resolution equivalents when visual quality is not too severely reduced. The process of downsampling will reduce the number of pixels in an image.

    This option is enabled by default.

    If the PDF is intended to be viewed at large magnification values, then disable this option by setting the ImGearCompressOptions.IsDownsampleImagesEnabled property to false.

    Re-compress Bitonal Images to JBIG2

    ImageGear offers three compression levels for recompressing bitonal images: JBIG2 lossless generic, JBIG2 lossless text, and JBIG2 lossy text.

    • Use JBIG2 lossless generic compression when the content of the image is unknown.
    • Use JBIG2 lossless text compression when the content of the image is known to contain text or symbols, since it can provide greater compression than JBIG2 lossless generic
    • Use JBIG2 lossy text compression when the content of the image is known to contain only text or symbols, since it can provide even greater compression than JBIG2 lossless text with low risk of introducing errors.

    The compression level can be controlled by setting the ImGearCompressOptions.RecompressImageJbig2CompressionLevel property to LosslessGeneric, LosslessText, or LossyText.

    Re-compress Images with JPEG Compression

    ImGearCompressOptions uses JPEG compression by default. The compression level can be controlled (or disabled) by setting ImGearCompressOptions.RecompressImageJpegCompressionLevel to one of the following five values: None, LossyLow, LossyMedium, LossyHigh, LossyMaximum.

    Re-compress Images with JPEG2000 Compression

    JPEG2000 compression must be enabled by setting the ImGearCompressOptions.RecompressUsingJpeg2K property to true. The compression level can be controlled by setting RecompressImageJpeg2kCompressionLevel to one of the following six values: Lossless, LossyMinimum, LossyLow, LossyMedium, LossyHigh, LossyMaximum.

    Remove Metadata

    When enabled, all metadata streams, except for the document's main metadata stream located in the catalog dictionary, are removed.

    This option is disabled by default, and is controlled by the ImGearCompressOptions.IsRemoveMetadataEnabled property.

    Remove Image Thumbnails

    When enabled, image thumbnails are removed from all pages of the document.

    This option is disabled by default, and is controlled by the ImGearCompressOptions.IsRemoveImageThumbnailEnabled property.