DEV Community

Jeremy K.
Jeremy K.

Posted on • Updated on

How to Compress PDF Files in C#?

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 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;

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

//Compress fonts
textCompression.CompressFonts = true;

//Unembed fonts
textCompression.UnembedFonts = true;
Enter fullscreen mode Exit fullscreen mode

❹ Compress images in the PDF file through the ImageCompressionOptions class;

//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;
Enter fullscreen mode Exit fullscreen mode

❺ Save the compressed PDF file to the specified location.

🔥 Original PDF vs. Compressed PDF

CompressPDF

Spire.PDF for .NET 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:

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");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

For more examples of manipulating PDF files in C# using the .NET PDF library, click:
https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Spire.PDF-Program-Guide-Content.html

Top comments (0)