Skip to main content

Command Palette

Search for a command to run...

How to Compress PDF Documents in Java [Pro Guide]

Updated
5 min read

PDF is one of the most frequently used file formats in daily office work. However, because many PDF documents contain multiple page images or a large number of embedded pictures, their file sizes can easily become too large to handle efficiently. Oversized PDF files may slow down transmission or downloads and increase the risk of transfer failures, ultimately affecting productivity.

For this reason, compressing PDF documents is often necessary. In this article, we will introduce two approaches to compressing PDF documents using a Java application.

When working with PDFs, it’s common to encounter transfer failures caused by large file sizes. In such cases, compressing the PDF before sending it can resolve the issue. In addition to compressing document content, reducing image size is one of the most effective ways to shrink a PDF file.

This article is divided into two parts, each demonstrating how to compress PDF documents using Java code. We hope this guide will be helpful to you.

Compress PDF Content and Images

1. Add the JAR File

Method 1: Manual Import

Download Free Spire.PDF for Java from its offical website. In the lib folder, locate the Spire.PDF.jar file. Open your project in IntelliJ IDEA and import the JAR file from the local path into your Java application.

Method 2: Install via Maven

If you prefer to install via Maven, add the following configuration to your pom.xml file:

<repositories>
   <repository>
       <id>com.e-iceblue</id>
       <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
   </repository>
</repositories>

<dependencies>
   <dependency>
       <groupId>e-iceblue</groupId>
       <artifactId>spire.pdf.free</artifactId>
       <version>5.1.0</version>
   </dependency>
</dependencies>

Compress PDF Content and Images

The detailed steps are as follows:

  1. Create an instance of the PdfDocument class.

  2. Load the PDF document using the PdfDocument.loadFromFile() method.

  3. Set incremental update to false using PdfDocument.getFileInfo().setIncrementalUpdate(false).

  4. Set the compression level to the best level using PdfDocument.setCompressionLevel(). You can choose other levels from the PdfCompressionLevel enumeration if needed.

  5. Loop through the pages in the document and retrieve image information for each page using PdfPageBase.getImagesInfo().

  6. Iterate through all items in the image collection and compress each image by setting its quality with PdfBitmap.setQuality().

  7. Replace the original image with the compressed image using PdfPageBase.replaceImage(). Finally, save the document to a new PDF file using PdfDocument.saveToFile().

Complete Code:

import com.spire.pdf.PdfCompressionLevel;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.exporting.PdfImageInfo;
import com.spire.pdf.graphics.PdfBitmap;

public class CompressPDFImage {

   public static void main(String[] args) {

       // Create an instance of the PdfDocument class
       PdfDocument doc = new PdfDocument();

       // Load the PDF document
       doc.loadFromFile("sample.pdf");

       // Disable incremental update
       doc.getFileInfo().setIncrementalUpdate(false);

       // Set the compression level to the best option
       doc.setCompressionLevel(PdfCompressionLevel.Best);

       // Loop through all pages in the document
       for (int i = 0; i < doc.getPages().getCount(); i++) {

           // Get the specified page
           PdfPageBase page = doc.getPages().get(i);

           // Retrieve the image information collection for each page
           PdfImageInfo[] images = page.getImagesInfo();

           // Iterate through the images collection
           if (images != null && images.length > 0)
               for (int j = 0; j < images.length; j++) {

                   // Get the specified image
                   PdfImageInfo image = images[j];
                   PdfBitmap bp = new PdfBitmap(image.getImage());

                   // Set the compression quality
                   bp.setQuality(20);

                   // Replace the original image with the compressed image
                   page.replaceImage(j, bp);
               }

           // Save the file
           doc.saveToFile("compress pdf.pdf");
           doc.close();
       }
   }
}

Result Comparison

(Effect comparison before and after compression)

Compress High-Resolution Images in a PDF

This method performs lossless compression only on high-resolution images in the document, while low-resolution images remain unchanged.

The steps are as follows:

  1. Create an instance of the PdfDocument class.

  2. Load the PDF document using PdfDocument.loadFromFile().

  3. Set IncrementalUpdate to false using PdfDocument.getFileInfo().setIncrementalUpdate(false).

  4. Declare a PdfPageBase variable.

  5. Loop through the pages and retrieve each specific page using PdfDocument.getPages().get().

  6. Iterate through the images on each page and compress high-resolution images using page.tryCompressImage(info.getIndex()).

  7. Save the document to another PDF file using PdfDocument.saveToFile().

Complete Code:

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.exporting.PdfImageInfo;

public class CompressPDFImage {
   public static void main(String[] args) {

       // Create an instance of the PdfDocument class
       PdfDocument doc = new PdfDocument

       // Load the PDF document
       doc.loadFromFile("sample.pdf");

       // Disable incremental update
       doc.getFileInfo().setIncrementalUpdate(false);

       // Declare a PdfPageBase variable
       PdfPageBase page;

       // Loop through all pages
       for (int i = 0; i < doc.getPages().getCount(); i++) {

           // Get the specified page
           page = doc.getPages().get(i);
           if (page != null) {

               if(page.getImagesInfo() != null){

                   // Loop through images on the page
                   for (PdfImageInfo info: page.getImagesInfo()) {

                       // Compress high-resolution images using tryCompressImage
                       page.tryCompressImage(info.getIndex());
                   }
               }
           }
       }

       // Save the file
       doc.saveToFile("compress pdf_image.pdf");
   }
}

Result Comparison

(Effect comparison before and after compression)

Conclusion

Compressing PDF files is an effective way to improve document transmission efficiency and reduce storage costs, especially when dealing with image-heavy files. In this article, we demonstrated two practical approaches for compressing PDF documents in Java: one method that compresses all images and content, and another that selectively compresses only high-resolution images.

Depending on your use case, you can choose the approach that best balances file size reduction and output quality. With these techniques, you can seamlessly integrate PDF compression into your Java applications and ensure smoother file sharing and processing workflows.