DEV Community

Jeremy K.
Jeremy K.

Posted on • Edited on

1 1

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

Image of Stellar post

How a Hackathon Project became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Watch the video

Top comments (0)

Sentry image

Make it make sense

Make sense of fixing your code with straight-forward application monitoring.

Start debugging →

👋 Kindness is contagious

Value this insightful article and join the thriving DEV Community. Developers of every skill level are encouraged to contribute and expand our collective knowledge.

A simple “thank you” can uplift someone’s spirits. Leave your appreciation in the comments!

On DEV, exchanging expertise lightens our path and reinforces our bonds. Enjoyed the read? A quick note of thanks to the author means a lot.

Okay