USING IRONPDF

How to Compress PDF Files in .NET Library

Updated September 26, 2023
Share:

This tutorial will demonstrate how to compress PDF files while maintaining high-quality printing using the IronPDF library.

IronPDF - .NET PDF Library

IronPDF is a powerful .NET library designed to work with PDF files. It provides a wide range of functionalities, including PDF generation, manipulation, and, of course, compression. IronPDF simplifies the process of working with PDF files in .NET applications, allowing developers to automate tasks and create customized solutions.

One of the standout features of IronPDF is its ability to compress and optimize PDF files without compromising their content quality. This is achieved through advanced algorithms that identify redundant or unnecessary data within the PDF structure and remove or optimize it, resulting in a smaller file size.

Images are often a major contributor to the size of a PDF file. IronPDF employs specialized image compression techniques to reduce the size of images within the PDF. This results in a significant reduction in file size without compromising the image's visual integrity.

Prerequisites

  1. Visual Studio: This is where you'll write your .NET code. You can download and install the latest version from its official Visual Studio website.
  2. IronPDF NuGet Package: This is the library used to work with PDFs. You can add it to your project using NuGet Package Manager.

Create a Visual Studio Project

Creating a Visual Studio console project is a straightforward process. Follow these steps to create a new Console Application using Visual Studio:

  1. Open Visual Studio: Launch your Visual Studio IDE.
  2. Create a New Project: Once Visual Studio is open, click on "Create a new project".
  3. Choose Project Template: In the "Create a new project" window, you'll see a list of project templates. Select Visual C# Console Application.

    How to Compress PDF Files in .NET Library, Figure 1: Open Visual Studio IDE and create a New Project Open Visual Studio IDE and create a New Project

  4. Configure Project Details: After selecting the template, you'll be prompted to configure the project details.

    How to Compress PDF Files in .NET Library, Figure 2: Configure project details Configure project details

  5. Configure Additional Settings: Choose the .NET Framework which has long-term support. IronPDF supports the latest version of the .NET Framework.
  6. Create Project: Once you've configured the project details, click the Create button. Visual Studio will create the project and open it in the IDE.

Installing IronPDF via NuGet

Here's how you can install IronPDF in your project:

  1. Open Visual Studio and your project.
  2. Go to the "Tools" menu and choose "NuGet Package Manager," then click on "Manage NuGet Packages for Solution."

    How to Compress PDF Files in .NET Library, Figure 3: Navigate to NuGet Package Manager Navigate to NuGet Package Manager

  3. In the "Browse" tab, search for "IronPDF" in the search box.

    How to Compress PDF Files in .NET Library, Figure 4: Search for IronPDF in NuGet Package Manager UI Search for IronPDF in NuGet Package Manager UI

  4. Click on the package, select it for your project and then click the "Install" button.

IronPDF for PDF Compression - Step-by-Step Process

IronPDF's compression ratio optimizes page content seamlessly and efficiently handles various elements. Here's a step-by-step explanation to reduce PDF file size.

Importing IronPdf Namespace

The code begins by importing the IronPdf namespace, which allows the use of classes and methods from the IronPDF library.

using IronPdf;
using IronPdf;
Imports IronPdf
VB   C#

Loading the PDF Document

The PdfDocument.FromFile method is used to load a PDF document from the file path "files/input.pdf" into a variable named pdf.

var pdf = PdfDocument.FromFile("files/input.pdf");
var pdf = PdfDocument.FromFile("files/input.pdf");
Dim pdf = PdfDocument.FromFile("files/input.pdf")
VB   C#

Here, the path "files/input.pdf" should be replaced with the actual path to the PDF document you want to work with.

Compressing Images - First Example

The CompressImages method is called with a quality parameter value of 80. This parameter indicates the desired image quality after compression. A lower value like 80 corresponds to a higher compression level and lower image quality.

pdf.CompressImages(80);
pdf.CompressImages(80);
pdf.CompressImages(80)
VB   C#

Saving the Compressed PDF - First Example

After compressing the images, the SaveAs method is used to save the compressed PDF images to the file path "files/document_compressed.pdf".

pdf.SaveAs("files/document_compressed.pdf");
pdf.SaveAs("files/document_compressed.pdf");
pdf.SaveAs("files/document_compressed.pdf")
VB   C#

Similar to importing existing PDF document files, replace "files/document_compressed.pdf" with the desired output file path.

Compressing Images with Scaling - Second Example

The following code sample shows another way to compress images while also scaling them down based on their visible size in the PDF. The pdf.CompressImages method is called with a quality parameter of 90.

pdf.CompressImages(90, true);
pdf.CompressImages(90, true);
pdf.CompressImages(90, True)
VB   C#

This combination of values of parameters indicates that the images will be compressed to 90% quality and scaled down according to their visible size in the PDF.

Saving the Scaled and Compressed PDF - Second Example

Finally, the pdf.SaveAs method is used again to save the scaled and compressed PDF to the file path "files/document_scaled_compressed.pdf".

pdf.SaveAs("files/document_scaled_compressed.pdf");
pdf.SaveAs("files/document_scaled_compressed.pdf");
pdf.SaveAs("files/document_scaled_compressed.pdf")
VB   C#

As before, adjust "files/document_scaled_compressed.pdf" to the appropriate output file path of your choice.

Output - PDF File Size

In the output, you can see the massive size reduction just by compressing the embedded images in the input PDF document.

How to Compress PDF Files in .NET Library, Figure 5: The output image displays the input and compressed output PDF documents, along with their file size The output image displays the input and compressed output PDF documents, along with their file size

IronPDF provides the best compression ratio, and it can be used to recompress PDF images to further compress PDF files. For more detailed information on how to compress PDF documents, visit this code examples page.

Conclusion

Efficient PDF compression is an essential aspect of modern document management and distribution systems. IronPDF offers a reliable solution for .NET developers seeking to incorporate PDF compression capabilities into their applications. With its lossless PDF compression techniques, image optimization, font subsetting, and resource removal, IronPDF empowers developers to reduce PDF file size and create smaller PDF files that retain their visual quality. By reducing file sizes, IronPDF not only saves storage space but also enhances document sharing and loading times, contributing to a more seamless user experience. Whether you're building a document management system, an online collaboration platform, or any other application involving PDFs, IronPDF is a valuable tool to consider.

Additionally, IronPDF is the perfect tool for PDF-related tasks using C#. IronPDF offers developers methods to render PDF documents into images, extract text and content from a PDF, enhancing security with passwords and watermarking programmatically.

IronPDF currently offers a free trial period to explore all its features for commercial use. Following this trial, a license is required for continued usage. You can easily obtain the software by downloading it from the IronPDF website.

< PREVIOUS
How to Read PDF Files in C#
NEXT >
PDF Conversion in C# (Developer Tutorial)

Ready to get started? Version: 2024.7 just released

Free NuGet Download Total downloads: 9,848,088 View Licenses >
123