IRONSOFTWAREHOME

How to Compress PDF Files in Java

Curtis Chau
Curtis Chau
Updated: August 2, 2026

IronPDF enables Java developers to compress PDF files by reducing image quality and scaling resolution, helping reduce file sizes for easier sharing and storage.

Quickstart: Compress a PDF File in Java
import com.ironsoftware.ironpdf.*;
import java.nio.file.Paths;

// Load your PDF document
PdfDocument pdf = PdfDocument.fromFile(Paths.get("source.pdf"));

// Compress images to 60% quality
pdf.compressImages(60);

// Save the compressed PDF
pdf.saveAs(Paths.get("compressed.pdf"));
Text

PDF documents are widely used for sharing and storing information, but image-heavy files can reach tens of megabytes very quickly. Large file sizes complicate email delivery, slow down uploads, and inflate storage costs. IronPDF's Java compression API addresses this by letting you reduce embedded image quality and scale resolution in a few lines of code.

This article covers how to compress PDF files in Java using IronPDF. The examples show both simple quality-based compression and resolution scaling, so you can match the approach to your specific size and quality requirements. If you are also working with image-heavy PDFs or need to handle merged documents, compression is an essential step before distribution.

What Is the IronPDF Java PDF Library?

IronPDF is a Java PDF library that creates, reads, and manipulates PDF documents programmatically. The library handles HTML to PDF conversion, form filling, image embedding, and file compression without requiring a PDF viewer or external rendering engine. It supports various deployment environments including AWS Lambda, Azure Functions, and Google Cloud.

Compression in IronPDF works by resampling embedded images to a lower JPEG quality level and, optionally, reducing their resolution to match the display size in the PDF. Text, vector graphics, and document structure are not affected. This makes IronPDF a practical choice for large documents that need to be transferred quickly over email or stored in cloud-based systems.

The library also works well alongside other document operations: you can compress a PDF that already has watermarks, background layers, and bookmarks, and all those features are preserved after compression.

Please note: Compression is most effective on PDFs that contain embedded JPEG or PNG images. Documents made up mostly of text and vector graphics will see minimal size reduction.

How Do I Set Up IronPDF in a Java Maven Project?

Before compressing PDFs, add IronPDF to your project's Maven configuration. For full setup instructions, see the Get Started Overview.

  • To install IronPDF in a Maven project, add the IronPDF Maven repository and dependency to your pom.xml file.

  • Add the dependency to the <dependencies> section:

        <dependency>
            <groupId>com.ironsoftware</groupId>
            <artifactId>ironpdf</artifactId>
            <version>your_version_here</version>
        </dependency>
    XML
  • Save your pom.xml and run mvn install to download the IronPDF dependency.

Once the dependency resolves, you can import and use IronPDF classes in your project. The latest published version of the IronPDF Java artifact is available on Maven Central. For production deployment, configure your license key before running.

How Do I Compress a PDF File in Java?

IronPDF compresses PDF files by targeting the embedded images inside the document. The compressImages() method accepts a quality integer from 0 to 100, where lower values produce smaller files at the cost of image sharpness. An optional second parameter instructs the library to scale down image resolution based on the display size in the PDF page.

import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;

public class App {
    public static void main(String[] args) throws IOException {
        // Load the source PDF from disk
        String src = "C:\\Image based PDF.pdf";
        PdfDocument pdf = PdfDocument.fromFile(Paths.get(src));

        // First pass: compress images to 60% quality
        // Lower values produce smaller files with more visible quality loss
        pdf.compressImages(60);
        pdf.saveAs(Paths.get("assets/document_compressed.pdf"));

        // Second pass (optional): compress to 90% quality with resolution scaling
        // The second parameter scales image resolution down to match display size,
        // which can reduce file size further but may cause distortion in some PDFs
        pdf.compressImages(90, true);
        pdf.saveAs(Paths.get("C:\\Compressed.pdf"));
    }
}
Java

The code loads a PDF from disk and applies compression in two passes to illustrate different strategies. The first compressImages(60) call reduces image quality to 60% and saves the result. The second call uses 90% quality but enables resolution scaling, which adjusts each image's pixel dimensions to match its visible area on the page.

The compressImages() method has two signatures:

  • compressImages(int quality): Resamples all embedded images to the given JPEG quality level. Use a value between 40 and 80 for strong size reduction while keeping images recognizable.
  • compressImages(int quality, boolean scaleImageResolutionToVisibleSize): Adds resolution scaling. When true, the library detects images that are rendered at a fraction of their native resolution and scales them down accordingly. This option offers the most aggressive size reduction but increases processing time slightly.
Tips: Apply multiple passes to fine-tune the size and quality balance. Start with a moderate setting like 70%, check the output file size, then decide whether a second pass at lower quality is warranted.

Running a 60% compression pass on an image-heavy PDF typically reduces file size by 50-70% depending on the source images. A document with primarily text content will see much smaller gains. Compare the before and after file sizes in the output directory to evaluate the result for your specific documents.

For documents that need other processing alongside compression, IronPDF supports extracting text, extracting images, and splitting PDFs.

What Does the Original PDF Look Like?

File Explorer showing original PDF file at 458 KB before compression using IronPDF

What Are the Results After Compression?

File Explorer showing compressed PDF output file with noticeably reduced file size after IronPDF compressImages at 60% quality

How Do I Choose the Right Compression Quality Setting?

Choosing the correct quality level requires balancing file size against visual fidelity. The right setting depends on the document's purpose and the sensitivity of its embedded images.

The table below gives practical guidelines for common use cases:

IronPDF Image Compression Quality Level Guide

Quality LevelApprox. Size ReductionBest For
80-100%10-30%Print-quality documents, medical records, legal filings
60-79%40-60%Internal reports, web download PDFs, email attachments
40-59%60-75%Thumbnail previews, draft documents, archival copies
Below 40%75%+Heavily degraded output; generally not recommended for distribution

Resolution scaling works best when the source PDF was assembled from high-resolution photographs or scanned pages but the document is intended only for screen viewing. In those cases, scaling resolution can reduce file size without a perceptible change in quality at normal zoom levels. For further context on PDF image encoding standards, refer to the JPEG 2000 format documentation at the Library of Congress and the PDF image compression discussion on Stack Overflow.

Important: Avoid compressing PDFs that contain digital signatures. Modifying embedded images changes the document's binary content, which will invalidate existing signatures.

How Do I Reduce PDF File Size Further Beyond Image Compression?

Image compression handles the largest contributor to file size in most PDFs, but additional techniques can reduce size further when combined. IronPDF supports several approaches that complement compressImages():

Remove unneeded pages. Deleting pages before compressing reduces both image data and page structure overhead. Use the page deletion examples to remove blank or redundant pages first.

Strip annotations. Annotation objects add binary data to the file. Removing them with the annotation management API before saving can reduce file size noticeably in documents with heavy markup.

Process documents in bulk. For workflows that compress many PDFs, iterate over a list of file paths and apply compressImages() in a loop. IronPDF handles each document independently, so batch processing does not require special configuration; see the pdf-compression example for a starting point.

For a broader discussion of PDF optimization strategies, the PDF file format specification at the Library of Congress documents the structural elements that contribute to file size and how they can be reduced.

What Are the Next Steps for Compressing PDFs in Java?

IronPDF's compressImages() method provides a practical way to reduce PDF file size in Java applications. A single method call with a quality integer covers most use cases, and the optional resolution scaling parameter handles image-heavy source files that need the most aggressive reduction.

Start with a free trial to test compression on your own documents:

Ready to see what else IronPDF can do? Check out the full Java PDF tutorial library here: IronPDF Java How-To Guides

Frequently Asked Questions

How can I compress PDF files in Java using IronPDF?

IronPDF allows you to compress PDF files in Java by using the `compressImages()` method, which adjusts the quality of embedded images and optionally scales their resolution. This helps reduce the overall file size for easier sharing and storage.

What is the advantage of using IronPDF for PDF compression?

The advantage of using IronPDF for PDF compression is its ability to precisely reduce the quality of embedded images and scale their resolution, resulting in effective file size reduction without affecting text or vector graphics.

Can I use IronPDF's PDF compression in a Maven project?

Yes, you can integrate IronPDF into a Maven project by adding it as a dependency in your `pom.xml` file. Once added, you can use IronPDF classes to perform various operations, including PDF compression.

How do I choose the right compression quality setting in IronPDF?

Choosing the right compression quality depends on your specific needs. Higher quality settings are suitable for print-quality documents, while lower settings offer greater file reduction for web and email. IronPDF's compression quality guide can help you decide the best balance for your PDF's purpose.

Does IronPDF's compression affect text and vector graphics?

No, IronPDF's compression techniques specifically target only the embedded images within the PDF, leaving text, vector graphics, and other document structures unchanged.

What is the optional resolution scaling feature in IronPDF?

The optional resolution scaling feature in IronPDF allows you to scale down image resolution to match the display size within the PDF. This feature helps reduce file size even further for image-heavy documents.

How can I ensure digital signatures are not invalidated during compression in IronPDF?

To prevent invalidating digital signatures, avoid modifying the image content in signed PDFs. Compress PDFs before adding digital signatures to maintain their validity.

Can IronPDF perform bulk PDF compression?

Yes, IronPDF can automate the compression of multiple PDFs by iterating over a list of file paths and applying the `compressImages()` method, making it efficient for batch processing.

What additional techniques does IronPDF offer for reducing PDF file size beyond image compression?

Beyond image compression, IronPDF provides techniques such as page deletion, annotation removal, and bulk processing to further reduce PDF file sizes.

Where can I find more resources to start using IronPDF for PDF compression?

You can start using IronPDF with a free trial available on their website. Additionally, explore more resources and how-to guides in the IronPDF Java tutorial library for comprehensive learning.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...
Read More

Ready to Get Started?

Version:2026.8just released

Get your FREE

30-day Trial Key instantly.

bullet_checkedNo credit card or account creation required
bullet_testTest in production
without watermarks
bullet_calendar30 days fully
functional product
bullet_support24/5 technical
support during trial
Get your free 30-day Trial Key instantly.
No credit card or account creation required
Java Maven Library for PDF
Install with Maven

Version: 2026.8

<dependency>
   <groupId>com.ironsoftware</groupId>
   <artifactId>ironpdf</artifactId>
   <version>2026.8.2</version>
</dependency>
https://central.sonatype.com/artifact/com.ironsoftware/ironpdf/2026.8.2
or
Java PDF JAR
Download JAR

Version: 2026.8

Manually install into your project

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

OR
bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried Iron Suite
Book your free Live Demo
Booking Badge

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required
Java Maven Library for PDF
Install with Maven

Version: 2026.8

<dependency>
   <groupId>com.ironsoftware</groupId>
   <artifactId>ironpdf</artifactId>
   <version>2026.8.2</version>
</dependency>
https://central.sonatype.com/artifact/com.ironsoftware/ironpdf/2026.8.2
or
Java PDF JAR
Download JAR

Version: 2026.8

Manually install into your project