Compress, or Optimize PDF Files with Same Quality using Java

Different organizations use PDF files for secure and organized exchange of information. However, sometimes PDF files become huge in size owing to embedded contents like images, videos, drawings, etc. You can easily optimize or compress such a PDF file size without any compromise on the quality. Let us explore the following scenarios of PDF size compression and optimization which you can incorporate in your Java applications:

PDF Size Optimization and Compression API – Installation

You can utilize the efficient and reliable routines of Aspose.PDF for Java API for optimizing or compressing huge-size PDF files while keeping the same quality. You can download the JAR files from Downloads or with Maven configurations in your project.

Optimize PDF Documents for the Web using Java

PDF document can be optimized when you need to use them in your web pages. This optimization is helpful to display the first page of PDF document as quickly as possible. You can have the optimized PDF file by following the steps below:

  1. Open source PDF file
  2. Call optimize method for PDF Optimization
  3. Save the output PDF file

The code snippet below is an example of how to optimize PDF documents for the web in your Java environment:

Compress or Optimize the Size of PDF containing Images using Java

Here we will mainly be discussing the scenarios where PDF files contain a lot of images thus are huge in size. For instance, a PDF file containing drawing for different models of airplanes and information about each part, minor or major, included as images or pictures of all components. Moreover, many professional documents could contain images as major artifacts of the file. In such scenarios, we can compress the PDF files with following approaches:

Shrinking, Compressing and Resizing All Images using Java

You can minimize the size of PDF file containing many images by shrinking, compressing and resizing the images. The size improvements could be noticeable because most of the file size is covered by the pictures that we now intend to shrink. You need to follow the steps below in order to shrink, compress and resize the pictures or images in a PDF file:

  1. Load input PDF file
  2. Initialize OptimizationOptions object
  3. Set Image Quality and Resolution
  4. Call optimizeResources method
  5. Save the output PDF document

The code snippet below shows how to shrink or compress images in order to reduce and minimize the PDF file size using Java:

Removing Embedded Fonts, Unused Streams and Linking Duplicate Streams using Java

When you need to reduce PDF file size then every byte matters. Embedded fonts can help reducing file size with different approaches. For example, you can either unembed all the fonts or you can keep only the subset of font characters that are being used in the PDF file. It would be a partial unembedding of fonts that would still help in minimizing the file size. Moreover, you can remove unused streams or link duplicate streams to save further space. These PDF optimizations will reduce the file size considerably. You need to follow the following steps to optimize and reduce PDF file size:

  1. Load input PDF document
  2. Initialize OptimizationOptions class object
  3. Either unembed all fonts or the subset of fonts
  4. Link duplicate streams
  5. Remove unused streams

The following code elaborates how to compress PDF files for optimizing, reducing and minimizing size of PDF documents:

So far we have discussed the optimization approaches majorly for the PDF files with images. Now let us proceed with some more ways for PDF optimization.

Compress or Reduce PDF Document Size using Java

PDF files often contain annotations, editable form fields and color artifacts that collectively take up space. Let us explore the following procedures to compress PDF file size.

Removing or Flattening Annotations to Reduce Size with Java

PDF files can contain a lot of annotations. For instance, watermark, comments, shapes, etc. You can remove annotations if they are not required anymore or fatten the annotations if no further changes are needed. Please follow the steps below for removing or flattening annotations to optimize the PDF file size:

  1. Open source PDF document
  2. Iterate through each page
  3. Flatten or delete annotations
  4. Save the output PDF document

The code snippet below is an example how to remove or flatten annotations in PDF documents using Java:

Removing Form Fields to Minimize PDF File Size with Java

Fillable PDF forms are common where you need submission of data on large scale. After submission of data, fillable form fields can be removed to optimize and minimize PDF file size. You need to follow the below steps for removing form fields:

  1. Load input PDF document
  2. Check for form fields in PDF document
  3. Iterate through each field and flatten it
  4. Save the updated compressed PDF file

Convert RGB Color Space to Grayscale for PDF Compression and Optimization using Java

Most of the PDF files contain textual contents which can be represented well in Grayscale color space as well. Moreover, when the purpose and priority is to save each byte then even the images can be converted to Greyscale because the focus is on archiving the data. You may follow the below steps for compressing and optimizing PDF file size by converting RGB color space to Grayscale:

  1. Access source PDF document
  2. Initialize RgbToDeviceGrayConversionStrategy instance
  3. Convert color space of each color to Greyscale
  4. Save output optimized PDF file

The following code snippet shows how to compress and optimize PDF size by changing the color space in Java environment:

Info: Using a new online web app from Aspose, you can transform a set of images into a video. You may also want to check out another service, one that extracts the sounds from videos.


In this article, we have covered different ways and approaches to optimize and compress PDF files for reduced size. First of all, we have optimized PDF file size for the web using Java. Then we have learned shrinking, compressing, and resizing for images in a PDF file containing several images. Moreover, we have also explored working with fonts, streams, annotations, form fields as well as converting the color space of PDF document to greyscale so that a PDF file with lesser size but the same quality can be created using Java. Furthermore, in case of any query related to this topic, you may contact us via Free Support Forum.

See Also

Convert PDF to TXT or TXT to PDF using C# VB.NET