Compress PDF Java

Compressing PDF files and reducing their size not only saves storage space but also ensures faster uploads and downloads. In this blog post, we’ll explore how to compress PDF in Java, uncovering techniques to reduce file sizes and optimize overall performance.

PDF Compression Java API - Installation

You can install the library from the Downloads section or configure Conholdate.Total for Java with the following Maven configurations:

<dependency>
<groupId>com.conholdate</groupId>
<artifactId>conholdate-total</artifactId>
<version>23.10</version>
<type>pom</type>
</dependency>

Why Compress PDFs in Java?

Faster Uploads and Downloads: Smaller PDF sizes mean quicker uploads and downloads, improving user experience and saving bandwidth.

Storage Efficiency: Compressed PDFs occupy less disk space, making them more manageable and cost-effective for storage solutions.

Optimized Performance: Compressed files lead to faster loading times, especially in web applications, resulting in a more responsive and efficient user interface.

Compress PDF in Java

You can compress PDF documents in Java by following the steps below:

  • Load the source PDF document using the Document class.
  • Compress PDF with the optimizeResources() method.
  • Render the compressed PDF file with the save() method.

The code snippet below shows how to compress PDF in Java:

// Open document
Document pdfDocument = new Document("Compress.pdf");
// Compress PDF document by optimizing resources
pdfDocument.optimizeResources();
// Save output document
pdfDocument.save("Compressed.pdf");

Compress PDF by Shrinking Images in Java

Sometimes PDF files can contain a lot of images when the documents are scanned or contain catalogs or other visual contents. Here you will learn another approach to compressing PDF documents by working with embedded images. The following steps explain how to shrink images to compress PDF files:

  • Load the source PDF file with the Document class.
  • Create an instance of the OptimizationOptions class.
  • Set CompressImages option and the qulaity of the images.
  • Compress the PDF file with optimizeResources() method.
  • Write the updated compressed PDF document with the save() method.

The following sample code elaborates on how to compress PDF by shrinking images in Java:

// Open document
Document pdfDocument = new Document("Compress.pdf");
// Initialize OptimizationOptions
com.aspose.pdf.optimization.OptimizationOptions optimizationOptions = new com.aspose.pdf.optimization.OptimizationOptions();
// Set CompressImages option
optimizationOptions.getImageCompressionOptions().setCompressImages(true);
// Set ImageQuality option
optimizationOptions.getImageCompressionOptions().setImageQuality(50);
// Compress PDF document using OptimizationOptions
pdfDocument.optimizeResources(optimizationOptions);
// Save updated compressed PDF document
pdfDocument.save("Compressed.pdf");

Compress PDF File by Converting RGB to Grayscale in Java

You can compress a PDF file by converting the color space from RGB to Greyscale with the following steps:

  • Open the input PDF with the Document class.
  • Convert RGB to Grayscale PDF with RgbToDeviceGrayConversionStrategy class.
  • Render the grayscale PDF file with the save() method.

The following code snippet explains how to compress PDF by converting RGB to Grayscale in Java:

// Open document
Document pdfDocument = new Document("Compress.pdf");
// Convert RGB to Grayscale PDF
com.aspose.pdf.RgbToDeviceGrayConversionStrategy strategy = new com.aspose.pdf.RgbToDeviceGrayConversionStrategy();
for (int idxPage = 1; idxPage <= pdfDocument.getPages().size(); idxPage++) {
com.aspose.pdf.Page page = pdfDocument.getPages().get_Item(idxPage);
strategy.convert(page);
}
// Save the grayscale PDF file
pdfDocument.save("Compressed.pdf");

Free Evaluation License

You can request a free temporary license to test all the features and evaluate the API to its full capacity without any limitations.

Wrapping Up

Compressing PDF files is a key feature for efficient file management. This article has covered different approaches to compressing PDF documents in Java. By implementing these techniques, you can ensure that your PDFs are not only smaller in size but also optimized for faster interactions and improved user experiences. In case you have any ambiguities, please feel free to write to us at forum.

See Also