How to Compress PDF Files in C#?
Tags: csharp, compress, pdf, dotnet
Published: 2023-11-09 07:23:11

PDF files can become large in size when they contain a significant amount of content, including text, images, and other multimedia elements. This can result in slower transmission speeds and consume more storage space. To address this issue, **compressing PDF files** is a good way to reduce file size. Here are the steps on how to compress PDF files quickly and efficiently with C#.


❶ Use the NuGet Package Manager in VS to install "spire.pdf" into the program and import the required namespaces. (You can also download [Spire.PDF](https://www.e-iceblue.com/Download/download-pdf-for-net-now.html) to reference the dlls manually);

❷ Initialize an instance of the **PdfCompressor** class and load the PDF file that need to be compressed;

❸ Compress the fonts in the PDF file through the **TextCompressionOptions** class;
```c#
//Get text compression options
TextCompressionOptions textCompression = compressor.Options.TextCompressionOptions;

//Compress fonts
textCompression.CompressFonts = true;

//Unembed fonts
textCompression.UnembedFonts = true;
```

❹ Compress images in the PDF file through the **ImageCompressionOptions** class;
```c#
//Get image compression options
ImageCompressionOptions imageCompression = compressor.Options.ImageCompressionOptions;

//Set the compressed image quality
imageCompression.ImageQuality = ImageQuality.High;

//Resize images
imageCompression.ResizeImages = true;

//Compress images
imageCompression.CompressImage = true;
```

❺ Save the compressed PDF file to the specified location.


🔥 **Original PDF vs. Compressed PDF**

![CompressPDF](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ok783hly7wktpat5q0vy.png)

**[Spire.PDF for .NET](https://www.e-iceblue.com/Introduce/pdf-for-net-introduce.html)** optimizes methods of compressing PDF files. When compress images in PDF, setting the image quality to **Low** provides the best compression, but the readability of the document may be affected. During compression, you can set the image quality to High, Medium or Low according to your needs.

**The full code is attached:**
```C#
using Spire.Pdf;
using Spire.Pdf.Conversion.Compression;

namespace CompressPdf
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load a PDF document while initializing the PdfCompressor object
            PdfCompressor compressor = new PdfCompressor("sample.pdf");

            //Get text compression options
            TextCompressionOptions textCompression = compressor.Options.TextCompressionOptions;

            //Compress fonts
            textCompression.CompressFonts = true;

            //Unembed fonts
            textCompression.UnembedFonts = true;

            //Get image compression options
            ImageCompressionOptions imageCompression = compressor.Options.ImageCompressionOptions;

            //Set the compressed image quality
            imageCompression.ImageQuality = ImageQuality.High;

            //Resize images
            imageCompression.ResizeImages = true;

            //Compress images
            imageCompression.CompressImage = true;

            //Save the compressed document to file
            compressor.CompressToFile("Compressed.pdf");
        }
    }
}
```

For more examples of manipulating PDF files in C# using the [.NET PDF library](https://www.e-iceblue.com/Introduce/pdf-for-net-introduce.html), click:
[https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Spire.PDF-Program-Guide-Content.html](https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Spire.PDF-Program-Guide-Content.html)