C#/VB.NET: Compress PDF Documents

In certain cases, large PDF documents are not welcome. For instance, when you want to transfer PDF files by email, or when you want to store PDF files on a device with limited storage capacity. Compressing the sizes of PDF files could be a perfect solution to this problem. This article will show you how to compress contents (unwanted commented lines, white spaces, etc.) and images in a PDF document by using Spire.PDF for .NET with C# and VB.NET.

Reducing PDF size by removing objects like images, fields, annotations, bookmarks, attachments, and embedded fonts is not involved in this article. If you need to perform some of these operations using our library, check the corresponding articles listed here.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Compress Contents and Images in PDF

The following are detailed steps to compress contents and images in a PDF document using Spire.PDF for .NET.

  • Create an object of PdfDocument class.
  • Load a sample PDF document using PdfDocument.LoadFromFile() method.
  • Disable incremental update by setting the PdfDocument.FileInfo.IncrementalUpdate property to false.
  • Set the compression level to best to compress the contents in the document. There are a few other levels that you can choose from the PdfCompressionLevel enumeration.
  • Loop through the pages in the document, and get the image information collection of each page using PdfImageHelper.GetImagesInfo() method.
  • Traverse all items in the collection, and use the method PdfImageHelper.ReplaceImage() to place the specific image with the compressed one.
  • Save the document to another PDF file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
using Spire.Pdf.Utilities;

namespace CompressPdf
    class Program
        static void Main(string[] args)
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Load a PDF file

            //Disable incremental update
            doc.FileInfo.IncrementalUpdate = false;

            //Set the compression level to best
            doc.CompressionLevel = PdfCompressionLevel.Best;

            //Loop through the pages in the document
            foreach (PdfPageBase page in doc.Pages)
                //Create a PdfImageHelper object
                PdfImageHelper helper = new PdfImageHelper();

                //Get image information collection of a certain page
                PdfImageInfo[] imagesInfo = helper.GetImagesInfo(page);

                //Loop through the image information collection
                foreach (PdfImageInfo imageInfo in imagesInfo)
                    //Replace a certain image with the compressed image
                    helper.ReplaceImage(imageInfo, CompressImage(imageInfo.Image));


            //Save the document to a PDF file

        //Compress an image by reducing the quality
        private static PdfBitmap CompressImage(Image img)
            PdfBitmap newImage = new PdfBitmap(img);
            newImage.Quality = 10;
            return newImage;

C#/VB.NET: Compress PDF Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.