IronPDF How-Tos Compress PDF in Java How to Compress PDF Files in Java Darrius Serrant Updated:June 22, 2025 PDF documents are commonly used for storing and sharing information, but they can often be quite large in file size. This can be a problem when trying to share or upload PDF documents, as well as for storage. To address this issue, it is possible to compress and save PDF files using IronPDF, a Java PDF Library. Compressing a PDF document reduces its size by removing unnecessary elements or objects, such as embedded fonts and images. In this article, we will explore the IronPDF library to compress PDF files in Java. We will also provide a code sample to help you understand the import process and implement it in your own projects. IronPDF: PDF Library IronPDF is a powerful Java PDF library that offers users the ability to create/generate, manipulate, compress, download, and read PDF files from any language or programming environment. It's capable of creating highly accurate documents with features like document control, form filling, image embedding, compression, and more. What sets IronPDF apart is its ability to compress existing PDFs, enabling users to significantly reduce their file size while retaining the highest quality output. This makes it perfect for large documents that need to be shared quickly over the internet or through email without sacrificing file quality. Compress PDF Documents Using IronPDF Follow the next steps to compress the PDF file size. Install IronPDF in Java Maven Project To install IronPDF in a Maven project, you will need to add the IronPDF Maven repository and dependency to your project's pom.xml file. Next, add the IronPDF dependency by adding the following to the section of your pom.xml file: <dependency> <groupId>com.ironsoftware</groupId> <artifactId>ironpdf</artifactId> <version>your_version_here</version> </dependency> <dependency> <groupId>com.ironsoftware</groupId> <artifactId>ironpdf</artifactId> <version>your_version_here</version> </dependency> XML Save your pom.xml file and run MVN install to install the IronPDF dependency. Once you have completed these steps, you should be able to import and use the IronPDF classes in your project. Compress PDF File This code is a Java program that uses the IronPDF library to compress a PDF document. import com.ironsoftware.ironpdf.*; import java.io.IOException; import java.nio.file.Paths; public class App { // Main method to compress PDF public static void main(String[] args) throws IOException { // Define the path of the source PDF file String src = "C:\\Image based PDF.pdf"; // Load the PDF document from the specified file path PdfDocument pdf = PdfDocument.fromFile(Paths.get(src)); // Compress images within the PDF to 60% of their original quality pdf.compressImages(60); // Save the compressed PDF to a new destination pdf.saveAs(Paths.get("assets/document_compressed.pdf")); // Compress images to 90% with additional resolution scaling // This can potentially reduce file size further by scaling image resolution pdf.compressImages(90, true); // Define the path where the final compressed PDF will be saved String dest = "C:\\Compressed.pdf"; // Save the final compressed PDF pdf.saveAs(Paths.get(dest)); } } import com.ironsoftware.ironpdf.*; import java.io.IOException; import java.nio.file.Paths; public class App { // Main method to compress PDF public static void main(String[] args) throws IOException { // Define the path of the source PDF file String src = "C:\\Image based PDF.pdf"; // Load the PDF document from the specified file path PdfDocument pdf = PdfDocument.fromFile(Paths.get(src)); // Compress images within the PDF to 60% of their original quality pdf.compressImages(60); // Save the compressed PDF to a new destination pdf.saveAs(Paths.get("assets/document_compressed.pdf")); // Compress images to 90% with additional resolution scaling // This can potentially reduce file size further by scaling image resolution pdf.compressImages(90, true); // Define the path where the final compressed PDF will be saved String dest = "C:\\Compressed.pdf"; // Save the final compressed PDF pdf.saveAs(Paths.get(dest)); } } JAVA The code imports the IronPDF library and the necessary Java classes for file handling. The main method of the App class reads a PDF document called "Image based PDF.pdf" from the "C:" directory, compresses the images in the PDF by 60%, and saves the new compressed version as "document_compressed.pdf" in the "assets" directory. Then, it compresses the images in the PDF by 90% and saves the resulting PDF file as "Compressed.pdf" in the "C:" directory. The second optional parameter can scale down the image resolution according to its visible size in the PDF document, which might lead to image distortion with some configurations. You can see the comparison between the compressed file and the original PDF file. Before Compression After Compression Conclusion IronPDF is a powerful yet affordable tool for compressing PDF size in Java with many features like page rotation, HTML to PDF conversion, and image manipulation. These features make IronPDF a great choice when looking for low-cost solutions to compress PDFs while still getting the added benefits of the comprehensive library. License pricing and options start from just $749, which offers budget-conscious developers an opportunity to access high-quality software without having to pay large amounts. All things considered, IronPDF provides an excellent choice for developers looking for reliable and robust PDF optimization with no hassle. Frequently Asked Questions How can I compress PDF files in Java? You can compress PDF files in Java using IronPDF by loading the PDF document, compressing the images within the PDF, and then saving the compressed PDF to a desired location. This process ensures that the file size is reduced while maintaining quality. What steps are involved in setting up IronPDF in a Java Maven project? To set up IronPDF in a Java Maven project, you need to add the IronPDF Maven repository and dependency to your project's pom.xml file. After saving the file, run mvn install to install the IronPDF dependency. Can I reduce the file size of a PDF without losing quality using a Java library? Yes, IronPDF allows you to efficiently reduce the file size of PDFs without losing quality by compressing images and optimizing document content. This makes it ideal for sharing and storing large documents. What additional features does IronPDF offer besides PDF compression? Besides PDF compression, IronPDF provides features such as page rotation, HTML to PDF conversion, and image manipulation, offering a comprehensive solution for various PDF optimization needs. How does IronPDF ensure high output quality when compressing PDFs? IronPDF maintains high output quality by selectively compressing images and optimizing resources within the PDF, ensuring that the reduction in file size does not compromise document quality. Is it possible to adjust image resolution in PDFs using IronPDF? Yes, with IronPDF, you can adjust the image resolution according to its visible size within the PDF, which can help reduce file size further, though it may affect image clarity if reduced too much. What are the advantages of using IronPDF for PDF compression in Java? Using IronPDF for PDF compression in Java offers advantages such as significant file size reduction, preservation of document quality, and additional features like HTML to PDF conversion, making it a versatile and cost-effective tool. How does IronPDF compare original and compressed PDFs visually? IronPDF provides visual comparisons between original and compressed PDFs, demonstrating the effectiveness of the compression process and ensuring that the quality remains high despite the reduced file size. Darrius Serrant Chat with engineering team now Full Stack Software Engineer (WebOps) Darrius Serrant holds a Bachelor’s degree in Computer Science from the University of Miami and works as a Full Stack WebOps Marketing Engineer at Iron Software. Drawn to coding from a young age, he saw computing as both mysterious and accessible, making it the perfect medium for creativity ...Read More Ready to Get Started? Free Maven Download View Licenses