How to Compress PDF Files in Java

PDF documents are commonly used for storing and sharing documents, but they can often be quite large in PDF file size. This can be a problem when trying to share or upload PDF document, as well as for storage. To address this issue, it is possible to compress and save PDF files using IronPDF, Java PDF Library. Compressing a PDF doc reduces its size by removing unnecessary elements/object, 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 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, images embedding, compression and more.

What sets IronPDF apart is its ability to compress existing PDFs, enabling users to reduce their file size significantly 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 the 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 page.
  • Next, add the IronPDF dependency by adding the following to the section of your pom.xml file:

  • 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.nio.file.Paths;  
public class App{
    // method to compress PDF
    public static void main(String[] args) throws IOException {
        string src = "C:\\Image based PDF.pdf";
        PdfDocument pdf = PdfDocument.fromFile(Paths.get(src));  

        // Valid image compression values range from 1 to 100, where 100 
        // represents 100% of the original image quality.  

        // The second, optional parameter can scale down the image resolution 
        // according to its visible size in the PDF document. Note that this may cause distortion with some image configurations  
        pdf.compressImages(90, true); 
        string dest =  "C:\\Compressed.pdf";

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.

You can see the comparison between the compressed file and the original PDF file.

Before Compression

Original PDF

After Compression

Compressed PDF Output


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 partnered together 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 price starts from just $749 which offers budget-conscious developers an opportunity to access high-quality software without having to pay out large amounts. All things considered, IronPDF provides an excellent choice for developers looking for reliable and robust PDF optimization with no hassle.