How to Compress PDF Files in Java | IronPDF

How to Compress PDF Files in Java

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

```java {title="Compress PDF in Java"} //:path=/static-assets/pdf/content-code-examples/java/how-to/compress-pdf-java-tutorial/quickstart-compress.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"));


<div class="hsg-featured-snippet">
<h3>Minimal Workflow (5 steps)</h3>

1. Add IronPDF to your Maven `pom.xml` as a dependency
2. Load your PDF using `PdfDocument.fromFile()`
3. Compress images with `pdf.compressImages(60)`
4. Optionally scale image resolution with `pdf.compressImages(90, true)`
5. Save the compressed PDF using `pdf.saveAs()`
</div>

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](https://ironpdf.com/java/how-to/java-pdf-to-image-tutorial/) or need to handle [merged documents](https://ironpdf.com/java/how-to/java-merge-pdf-tutorial/), compression is an essential step before distribution.

## What Is the IronPDF Java PDF Library?

IronPDF is a [Java PDF library](/java/) that creates, reads, and manipulates PDF documents programmatically. The library handles [HTML to PDF conversion](https://ironpdf.com/java/tutorials/html-to-pdf/), form filling, image embedding, and file compression without requiring a PDF viewer or external rendering engine. It supports various deployment environments including [AWS Lambda](https://ironpdf.com/java/get-started/aws/), [Azure Functions](https://ironpdf.com/java/get-started/azure/), and [Google Cloud](https://ironpdf.com/java/get-started/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](https://ironpdf.com/java/how-to/custom-watermark/), [background layers](https://ironpdf.com/java/how-to/background-foreground/), and [bookmarks](https://ironpdf.com/java/how-to/bookmarks/), and all those features are preserved after compression.

Please noteCompression 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](https://ironpdf.com/java/docs/). - To install IronPDF in a [Maven project](/java/docs/), add the `IronPDF` Maven repository and dependency to your `pom.xml` file. - Add the dependency to the `<dependencies>` section: ```xml //:path=/static-assets/pdf/content-code-examples/java/how-to/compress-pdf-java-tutorial/maven-dependency.xml <dependency> <groupId>com.ironsoftware</groupId> <artifactId>ironpdf</artifactId> <version>your_version_here</version> </dependency>
  • 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.

//:path=/static-assets/pdf/content-code-examples/java/how-to/compress-pdf-java-tutorial/compress-pdf-images.java
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"));
    }
}
//:path=/static-assets/pdf/content-code-examples/java/how-to/compress-pdf-java-tutorial/compress-pdf-images.java
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.

TipsApply 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 Level Approx. Size Reduction Best 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.

ImportantAvoid 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

What is PDF compression and why is it important?

PDF compression reduces file size by resampling embedded images and optionally adjusting their display resolution. Smaller files are easier to email, upload, and store — IronPDF's compressImages() method handles this in Java applications with a single method call.

How do I quickly compress a PDF file in Java?

Load your PDF with PdfDocument.fromFile(), call compressImages(60) to reduce image quality to 60%, then save the result with saveAs(). Those three lines handle the core compression workflow.

What compression quality level should I use for my PDFs?

A value of 60-70% provides a good balance between file size and visual quality for most documents. Use 80-100% for print-quality or legal records, and 40-59% only for drafts or thumbnail previews where image fidelity is not critical.

Can I compress PDFs containing multiple merged documents?

Yes, IronPDF compresses any PDF regardless of how it was assembled. The compressImages() method targets all embedded images in the document, whether they come from a single source or from multiple merged files.

Does compression affect other PDF features like watermarks or forms?

No — IronPDF's compression targets only embedded raster images. Text, vector graphics, watermarks, backgrounds, bookmarks, and form fields are preserved in the output document.

What deployment environments support PDF compression with IronPDF?

IronPDF supports PDF compression on AWS Lambda, Azure Functions, Google Cloud, and standard server deployments. No external rendering engine or PDF viewer is required in any of these environments.

What does the scaleImageResolutionToVisibleSize parameter do?

When set to true, IronPDF detects images rendered at a fraction of their native resolution and scales their pixel dimensions down to match the visible display size. This reduces file size further than quality reduction alone, but slightly increases processing time.

Will compressing a PDF invalidate its digital signatures?

Yes. Modifying embedded images changes the document's binary content, which breaks existing digital signatures. Compress PDF files before applying signatures, not after.

Darrius Serrant
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?
Version: 2026.4 just released
Still Scrolling Icon

Still Scrolling?

Want proof fast?
run a sample watch your HTML become a PDF.