PDF is one of the most popular formats for distributing, archiving, and presenting digital documents. However, when PDFs include high-resolution images, scanned pages, or embedded fonts, their file sizes can grow considerably. Large PDF files can slow down upload and download speeds, take up unnecessary storage space, and even cause issues with email attachments and website performance.
This complete guide shows you how to compress PDF documents in C# using the Spire.PDF for .NET library. It covers multiple compression strategies—image compression, font optimization, and content compression—with practical, ready-to-use C# code examples to help you streamline PDF size effectively in your .NET applications.
Table of Contents
- Why Compress PDF Files
- Install the Library for PDF Compression in .NET
- How to Optimize PDF File Size in C# (Methods and Code Examples)
- Conclusion
- FAQs
Why Compress PDF Files?
Compressing PDF files can bring significant benefits, especially in professional and enterprise environments:
- Faster upload and download speeds
- Reduced storage consumption
- Easier email sharing with smaller attachments
- Improved performance in web-based PDF viewers
- Better experience on mobile and low-bandwidth environments
Whether you're working with reports, invoices, or scanned documents, PDF compression ensures efficient document handling and delivery.
Install the Library for PDF Compression in .NET
Spire.PDF for .NET is a robust and developer-friendly library that enables developers to create, edit, convert, and compress PDF documents without relying on Adobe Acrobat. It supports various compression options to minimize PDF file sizes effectively.
Installation Steps
You can easily install Spire.PDF for .NET via NuGet using one of the following methods:
Option 1: Using NuGet Package Manager
- Open your project in Visual Studio.
- Right-click on the project → Manage NuGet Packages.
- Search for Spire.PDF.
- Click Install.
Option 2: Using the Package Manager Console
Install-Package Spire.PDF
Once installed, you can start using the built-in compression APIs to optimize PDF file size.
How to Optimize PDF File Size in C# (Methods and Code Examples)
Spire.PDF provides multiple techniques to reduce PDF size. In this section, you'll learn how to implement three key methods: compressing images, optimizing fonts, and compressing overall document content.
Method 1. Compressing Images
High-resolution images embedded in PDF files often consume the most space. Spire.PDF offers flexible image compression options that allow you to reduce file size by compressing either all images or individual images in the document.
Example 1: Compress All Images with PdfCompressor
You can compress the images in a PDF document by creating a PdfCompressor object, enabling CompressImage and ResizeImages properties, and setting the ImageQuality to a predefined level such as Low, Medium, or High.
using Spire.Pdf.Conversion.Compression;
namespace CompressImages
{
class Program
{
static void Main(string[] args)
{
// Create a PdfCompressor object and load the PDF file
PdfCompressor compressor = new PdfCompressor("C:\\Users\\Administrator\\Documents\\Example.pdf");
// Get the image compression options
ImageCompressionOptions imageCompression = compressor.Options.ImageCompressionOptions;
// Enable Image resizing
imageCompression.ResizeImages = true;
// Enable image compression
imageCompression.CompressImage = true;
// Set the image quality to Medium (available options: Low, Medium, High)
imageCompression.ImageQuality = ImageQuality.Medium;
// Compress the PDF file according to the compression options and save it to a new file
compressor.CompressToFile("Compressed.pdf");
}
}
}
Example 2: Compress Images Individually Using TryCompressImage()
If you need more precise control over image compression, you can use the PdfImageHelper class to access the images on each page and compress them individually using the TryCompressImage() method.
using Spire.Pdf;
using Spire.Pdf.Utilities;
namespace CompressImagesIndividually
{
internal class Program
{
static void Main(string[] args)
{
// Create a PdfDocument object
PdfDocument pdf = new PdfDocument();
// Load the PDF file
pdf.LoadFromFile("C:\\Users\\Administrator\\Documents\\Example.pdf");
// Disable the incremental update
pdf.FileInfo.IncrementalUpdate = false;
// Create an instance of PdfImageHelper to work with images
PdfImageHelper imageHelper = new PdfImageHelper();
// Iterate through each page in the document
foreach (PdfPageBase page in pdf.Pages)
{
// Retrieve information about the images on the page
foreach (PdfImageInfo info in imageHelper.GetImagesInfo(page))
{
// Attempt to compress the image
info.TryCompressImage();
}
}
// Save the updated file
pdf.SaveToFile("Compressed.pdf");
pdf.Close();
}
}
}
Method 2. Optimize Fonts
Fonts embedded in a PDF file can contribute significantly to its size, especially when multiple fonts or large font sets are used. You can compress or unembed fonts that aren't essential for document rendering through the TextCompressionOptions property.
using Spire.Pdf.Conversion.Compression;
namespace OptimizeFonts
{
internal class Program
{
static void Main(string[] args)
{
// Create a PdfCompressor object and load the PDF file
PdfCompressor compressor = new PdfCompressor("C:\\Users\\Administrator\\Documents\\Example.pdf");
// Get the text compression options
TextCompressionOptions textCompression = compressor.Options.TextCompressionOptions;
// Compress the fonts
textCompression.CompressFonts = true;
// Unembed the fonts
// textCompression.UnembedFonts = true;
// Compress the PDF file according to the compression options and save it to a new file
compressor.CompressToFile("CompressFonts.pdf");
}
}
}
Method 3. Optimizing Document Content
Beyond images and fonts, overall document content can be optimized by setting the CompressionLevel property of the document to PdfCompressionLevel.Best.
using Spire.Pdf;
using Spire.Pdf.Conversion.Compression;
namespace OptimizeDocumentContent
{
internal class Program
{
static void Main(string[] args)
{
// Create a PdfDocument object
PdfDocument pdf = new PdfDocument();
// Load the PDF file
pdf.LoadFromFile("C:\\Users\\Administrator\\Documents\\Example.pdf");
// Disable the incremental update
pdf.FileInfo.IncrementalUpdate = false;
// Set the compression level to best
pdf.CompressionLevel = PdfCompressionLevel.Best;
// Save the updated file
pdf.SaveToFile("OptimizeDocumentContent.pdf");
pdf.Close();
}
}
}
Conclusion
Compressing PDF files in C# using Spire.PDF for .NET is straightforward, efficient, and highly customizable. Whether your goal is to reduce file size for web uploads, email attachments, or storage management, this library offers flexible solutions such as:
- Compressing images
- Optimizing fonts
- Minimizing document content
By applying one or a combination of these techniques, you can significantly reduce PDF file sizes while preserving readability and document structure—making your files easier to share, store, and distribute.
FAQs
Q1: Is it possible to compress PDF files in bulk?
A1: Yes. You can loop through multiple PDF files in a directory and apply compression using the same logic programmatically.
Q2: Can I compress a PDF and then convert it to PDF/A or other formats?
A2: Absolutely. You can compress the PDF first, then convert it to PDF/A, ensuring long-term archival with optimized size.
Q3: Can I preserve hyperlinks, bookmarks, and metadata during compression?
A3: Yes. Compression will not remove document structure like links, bookmarks, or metadata. Spire.PDF preserves document integrity.
Q4: Does Spire.PDF support other PDF operations besides compression?
A4: Yes. Besides compression, Spire.PDF offers a wide range of PDF features, such as:
- Merging/Splitting PDFs
- Extracting text, images and tables
- Adding watermarks
- Digitally signing and encrypting PDFs
For detailed tutorials and sample projects, you can visit the Spire.PDF tutorial page and explore the GitHub demo repository to see practical code examples.
Get a Free License
To fully experience the capabilities of Spire.PDF for .NET without any evaluation limitations, you can request a free 30-day trial license.