Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
This tutorial will demonstrate how to compress PDF files while maintaining high-quality printing using the IronPDF 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.
Creating a Visual Studio console project is a straightforward process. Follow these steps to create a new Console Application using Visual Studio:
Choose Project Template: In the "Create a new project" window, you'll see a list of project templates. Select Visual C# Console Application.
Open Visual Studio IDE and create a New Project
Configure Project Details: After selecting the template, you'll be prompted to configure the project details.
Configure project details
Here's how you can install IronPDF in your project:
Go to the "Tools" menu and choose "NuGet Package Manager," then click on "Manage NuGet Packages for Solution."
Navigate to NuGet Package Manager
In the "Browse" tab, search for "IronPDF" in the search box.
Search for IronPDF in NuGet Package Manager UI
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.
IronPdf
NamespaceThe 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
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")
Here, the path "files/input.pdf" should be replaced with the actual path to the PDF document you want to work with.
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)
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")
Similar to importing existing PDF document files, replace "files/document_compressed.pdf" with the desired output file path.
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)
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.
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")
As before, adjust "files/document_scaled_compressed.pdf" to the appropriate output file path of your choice.
In the output, you can see the massive size reduction just by compressing the embedded images in the input PDF document.
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.
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.
9 .NET API products for your office documents