How to Compress PDF Files in Java
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));
}
}
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
What is IronPDF?
IronPDF is a Java PDF library that allows users to create, manipulate, compress, download, and read PDF files. It offers features such as document control, form filling, image embedding, and compression.
How can I install IronPDF in a Java Maven project?
To install IronPDF in a Maven project, add the IronPDF Maven repository and dependency to your project's pom.xml file. Then save the file and run MVN install to install the IronPDF dependency.
How do I compress PDF files using IronPDF?
You can compress PDF files in Java using IronPDF by loading the PDF document, compressing images within the PDF, and then saving the compressed PDF to a specified location.
What are the benefits of using IronPDF for PDF compression?
IronPDF allows significant reduction in file size without sacrificing quality, making it ideal for sharing large documents over the internet or email quickly. It also offers additional features like page rotation and HTML to PDF conversion.
Can I scale down the image resolution in a PDF using IronPDF?
Yes, IronPDF allows you to scale down the image resolution according to its visible size in the PDF document, which can further reduce file size but might lead to image distortion.
What is the pricing for IronPDF?
License pricing for IronPDF starts from just $liteLicense, providing affordable access to high-quality software for budget-conscious developers.
What are the steps to compress a PDF using a Java program?
Using IronPDF, you can load a PDF document, compress its images to a specified quality percentage, and save the compressed version to a new location.
What additional features does IronPDF provide?
Besides PDF compression, IronPDF offers features like page rotation, HTML to PDF conversion, and image manipulation, providing a comprehensive solution for PDF optimization.