How to Compress PDF in C#

This quick topic will walk you through how to compress PDF in C# with the help of complete configuration steps including setting up the environment and other information about necessary classes to be used. You can easily use simple API calls in C# compress PDF file size and save it to disk. You can customize the output PDF compression options by using the OptimizationOptions class object.

Steps to Compress PDF in C#

  1. Configure your application to add reference to Aspose.PDF from the NuGet package manager
  2. Load the source PDF file from disk using Document class instance
  3. Instantiate the OptimizationOptions class object to configure the compression of PDF file and set different PDF optimization settings
  4. The OptimizeResources method in C# compress PDF file based on settings used
  5. Save the compressed PDF file on disk

The above steps in C# reduce PDF size by following the defined sequence of operations. You will begin the process by adding API reference using NuGet package manager and importing required namespaces. Then source PDF file will be loaded using Document class object for compression. The OptimizationOptions class object will be used to set different compression options for PDF including compression of images and PDF image quality. The OptimizeResources method apply the optimization settings to compress PDF based on selected options. Finally, the compressed PDF will be saved on the disk.

Code to Compress PDF in C#

using System;
using Aspose.Pdf;
using Aspose.Pdf.Optimization;
namespace TestPDF
{
public class CompressPDFFile
{
public static void CompressPDF()
{
// Applying product license to create HTML from PDF in C#
License CompressPdfLic = new License();
CompressPdfLic.SetLicense("PDF.Product.Family.lic");
// Load source PDF document for compression
Document CompressPdfDocument = new Document("TestCompress.pdf");
// Optimize PDF document.
// Note that this method cannot guarantee document shrinking and depends
// on content of PDF
OptimizationOptions PdfoptimizeOptions = new OptimizationOptions();
// Set CompressImages option
PdfoptimizeOptions.ImageCompressionOptions.CompressImages = true;
// Set the image quality option
PdfoptimizeOptions.ImageCompressionOptions.ImageQuality = 50;
// Applying set options on PDF resources
CompressPdfDocument.OptimizeResources(PdfoptimizeOptions);
// Save the compressed PDF document
CompressPdfDocument.Save("CompressPdf.pdf");
}
}
}

The aforementioned example in C# compress PDF size by adopting simple API calls and few lines of code. The OptimizationOptions also allow you to set different other compression settings including ImageEncoding, LinkDuplicateStreams, MaxResolution, RemovePrivateInfo, RemoveUnusedStreams, RemoveUnusedObjects and many other settings. If the source PDF has content that can be referred by using above settings, it will contribute towards compression of PDF once these properties are used.

In this topic, we have learned that in order to compress PDF file programmatically C# based application can be easily developed. If you want to learn about splitting PDF to pages, refer to the article how to split PDF file by Pages in C#.

 English