ImageGear .NET - Updated
Compress a PDF Document
User Guide > How to Work with... > Formats with Additional Functionality > PDF > How to... > Compress a PDF Document

ImageGear .NET 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 .NET 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 .NET 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 .NET:

  1. Read the PDF document into a System.IO.Stream object.
  2. Read the stream using ImGearFileFormats.LoadDocument, 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.

The following is a sample method that illustrates how to re-compress a PDF file to memory:

C# Example
Copy Code
using System;
using System.IO;
using System.Text;
using ImageGear.Core;
using ImageGear.Formats;
using ImageGear.Windows.Forms;
using ImageGear.Formats.PDF;
public Stream SaveCompressedPDFtoStream(String inputFileName)
{
    // Load PDF document.
    ImGearPDFDocument pdfDocument = null;
    using (Stream fileStream = new FileStream(inputFileName, FileMode.Open,
       FileAccess.Read, FileShare.Read))
    {
        pdfDocument = (ImGearPDFDocument)ImGearFileFormats.LoadDocument(fileStream);
    }
 
    // Save compressed PDF document to stream.
    Stream stream = new MemoryStream();
    pdfDocument.SaveCompressed(stream);
    return stream;
}
VB.NET Example
Copy Code
Imports System
Imports System.IO
Imports System.Text
Imports ImageGear.Core
Imports ImageGear.Formats
Imports ImageGear.Formats.PDF

Public Function SaveCompressedPDFtoStream(inputFileName As String) As Stream
 
    ' Load PDF document.
    Dim pdfDocument As ImGearPDFDocument
    Using fileStream As New FileStream(inputFileName, FileMode.Open, FileAccess.Read,
            FileShare.Read)
        pdfDocument = DirectCast(ImGearFileFormats.LoadDocument(fileStream),
                ImGearPDFDocument)
    End Using

    ' Save compressed PDF document to stream.
    Dim stream As Stream = New MemoryStream()
    pdfDocument.SaveCompressed(stream)
    Return stream

End Function

Refer to the sample SaveCompressedPDF for a more complete example of re-saving PDF files (see PDF Samples).

Compression Controlling Options

ImageGear .NET 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:

C#
Copy Code
// Prevent downsampling high resolution images in the PDF document.
ImGearCompressOptions compressOptions = new ImGearCompressOptions();
compressOptions.IsDownsampleImagesEnabled = false;
pdfDocument.SaveCompressed(stream, compressOptions);
VB.NET
Copy Code
' Prevent downsampling high resolution images in the PDF document.
Dim compressOptions as ImGearCompressOptions = new ImGearCompressOptions()
compressOptions.IsDownsampleImagesEnabled = False
pdfDocument.SaveCompressed(stream, compressOptions)

Re-compress Bitonal Images

Re-compress Bitonal Images to JBIG2: Lossless Generic

Use JBIG2 lossless generic compression when the content of the image is unknown.

C#
Copy Code
ImGearCompressOptions compressOptions = new ImGearCompressOptions();
compressOptions.RecompressImageJbig2CompressionLevel = ImGearJbig2CompressionLevel.LosslessGeneric;
igSourceDocument.SaveCompressed(outputPath, compressOptions);
VB.NET
Copy Code
Dim compressOptions As ImGearCompressOptions = New ImGearCompressOptions()
compressOptions.RecompressImageJbig2CompressionLevel = ImGearJbig2CompressionLevel.LosslessGeneric
igSourceDocument.SaveCompressed(outputPath, compressOptions)

Re-compress Bitonal Images to JBIG2: Lossless Text

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.

C#
Copy Code
ImGearCompressOptions compressOptions = new ImGearCompressOptions();
compressOptions.RecompressImageJbig2CompressionLevel = ImGearJbig2CompressionLevel.LosslessText;
igSourceDocument.SaveCompressed(outputPath, compressOptions);
VB.NET
Copy Code
Dim compressOptions As ImGearCompressOptions = New ImGearCompressOptions()
compressOptions.RecompressImageJbig2CompressionLevel = ImGearJbig2CompressionLevel.LosslessText
igSourceDocument.SaveCompressed(outputPath, compressOptions)

Re-compress Bitonal Images to JBIG2: Lossy Text

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.

C#
Copy Code
ImGearCompressOptions compressOptions = new ImGearCompressOptions();
compressOptions.RecompressImageJbig2CompressionLevel = ImGearJbig2CompressionLevel.LossyText;
igSourceDocument.SaveCompressed(outputPath, compressOptions);
VB.NET
Copy Code
Dim compressOptions As ImGearCompressOptions = New ImGearCompressOptions()
compressOptions.RecompressImageJbig2CompressionLevel = ImGearJbig2CompressionLevel.LossyText
igSourceDocument.SaveCompressed(outputPath, compressOptions)

Re-compress Images with JPEG Compression

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

C#
Copy Code
ImGearCompressOptions compressOptions = new ImGearCompressOptions();
compressOptions.RecompressImageJpegCompressionLevel = ImGearJpegCompressionLevel.LossyMedium;
igSourceDocument.SaveCompressed(outputPath, compressOptions);
VB.NET
Copy Code
Dim compressOptions As ImGearCompressOptions = New ImGearCompressOptions()
compressOptions.RecompressImageJpegCompressionLevel = ImGearJpegCompressionLevel.LossyMedium
igSourceDocument.SaveCompressed(outputPath, compressOptions)

Re-compress Images with JPEG2000 Compression

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

C#
Copy Code
ImGearCompressOptions compressOptions = new ImGearCompressOptions();
compressOptions.RecompressUsingJpeg2K = true;
compressOptions.RecompressImageJpeg2kCompressionLevel = ImGearJpeg2kCompressionLevel.Lossless;
igSourceDocument.SaveCompressed(outputPath, compressOptions);
VB.NET
Copy Code
Dim compressOptions As ImGearCompressOptions = New ImGearCompressOptions()
compressOptions.RecompressUsingJpeg2K = True
compressOptions.RecompressImageJpeg2kCompressionLevel = ImGearJpeg2kCompressionLevel.Lossless
igSourceDocument.SaveCompressed(outputPath, compressOptions)

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.

C#
Copy Code
// Enable removing metadata
ImGearCompressOptions compressOptions = new ImGearCompressOptions();
compressOptions.IsRemoveMetadataEnabled = true;
pdfDocument.SaveCompressed(stream, compressOptions);
VB.NET
Copy Code
' Enable removing metadata
Dim compressOptions as ImGearCompressOptions = new ImGearCompressOptions()
compressOptions.IsRemoveMetadataEnabled = True
pdfDocument.SaveCompressed(stream, compressOptions)

Remove Image Thumbnails

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

This option is disabled by default.

C#
Copy Code
// Enable removing metadata.
ImGearCompressOptions compressOptions = new ImGearCompressOptions();
compressOptions.IsRemoveThumbnailEnabled = true;
pdfDocument.SaveCompressed(stream, compressOptions);
VB.NET
Copy Code
' Enable removing metadata.
Dim compressOptions as ImGearCompressOptions = new ImGearCompressOptions()
compressOptions.IsRemoveThumbnailEnabled = True
pdfDocument.SaveCompressed(stream, compressOptions)