News Category

C#/VB.NET: Compress PDF Documents

2022-06-10 07:12:00 Written by  support iceblue
Rate this item
(0 votes)

Large PDF files can be cumbersome to work with, taking up valuable storage space and slowing down transfers and uploads. Compressing PDF documents is a simple and effective way to reduce their file size and optimize them for various uses. By compressing PDFs, you can make them easier to share over email or cloud storage platforms, speed up downloads, and improve overall document management. In this article, you will learn how to compress a PDF document in C# and VB.NET using Spire.PDF for .NET.

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 Fonts and Images in a PDF Document in C#, VB.NET

Fonts and high-quality images are two main factors that contribute to the size of a PDF document. To reduce the PDF document size, you can compress the font resources (or even unembed fonts) and the image quality. The following are the steps to compress PDF documents using Spire.PDF for .NET.

  • Load a PDF document while initializing the PdfCompressor object.
  • Get text compression options through PdfCompressor.Options.TextCompressionOptions property.
  • Compress font resources by setting TextCompressionOptions.CompressFonts to true.
  • Get image compression options through PdfCompressor.Options.ImageCompressionOptions property.
  • Set the image compression level through ImageCompressionOptions.ImageQuality property.
  • Compress images by setting ImageCompressionOptions.CompressImage to true.
  • Save the compressed document to file using PdfCompressor.CompressToFile() method.
  • C#
  • VB.NET
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("C:\\Users\\Administrator\\Desktop\\ToCompress.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");
        }
    }
}

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.

Additional Info

  • tutorial_title:
Last modified on Monday, 26 June 2023 06:05