Compress PDF Files in C#
PDF documents are widely used in various fields, such as education, business, and government. However, PDF files can be very large, which can cause problems when sharing or storing them. To solve this problem, compressing PDF documents has become a common practice. In this article, we will explore how to programmatically compress a PDF document using a .NET PDF library.
Installation
To get started, we'll need to download the library Spire.PDF for .NET via the below link or install it directly via Nuget.
https://www.e-iceblue.com/Download/download-pdf-for-net-now.html
Compress PDF Documents Using C#
Two main factors that affect the size of a PDF document are fonts and high-quality images. To reduce the file size, we can compress the font resources and the image quality.
Sample C# Code:
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("Island.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");
}
}
}
Other Features Provided by the .NET PDF API.
In addition to compressing PDF documents, Spire.PDF for .NET provides many other features that can help developers manipulate PDF documents. Here are some of the key features:
Convert HTML or Images to PDF in C#/VB.NET
Extract text or images from PDF in C#/VB.NET
Subscribe to my newsletter
Read articles from Jonathon directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by