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:
- Read the PDF document into a
System.IO.Stream
object.
- Read the stream using
ImGearFileFormats.LoadDocument
, casting the ImGearDocument
object returned to an ImGearPDFDocument
object.
- 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:
PDF support needs to be initialized first for this snippet to work. To get familiar with initializing IGNET, initializing PDF support, loading a PDF, saving a PDF, and terminating PDF support, try any one of the tutorials.
C#
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;
}
Refer to the sample SaveCompressedPDF for a more complete example of re-saving PDF files (see PDF Samples).
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:
C#
// Prevent downsampling high resolution images in the PDF document.
ImGearCompressOptions compressOptions = 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#
ImGearCompressOptions compressOptions = 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#
ImGearCompressOptions compressOptions = 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#
ImGearCompressOptions compressOptions = 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#
ImGearCompressOptions compressOptions = new ImGearCompressOptions();
compressOptions.RecompressImageJpegCompressionLevel = ImGearJpegCompressionLevel.LossyMedium;
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#
// Enable removing metadata
ImGearCompressOptions compressOptions = 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#
// Enable removing metadata.
ImGearCompressOptions compressOptions = new ImGearCompressOptions();
compressOptions.IsRemoveThumbnailEnabled = true;
pdfDocument.SaveCompressed(stream, compressOptions);