InterServer Web Hosting and VPS
How to Reduce Pdf File Size Programmatically in Java

Best Way to Reduce Pdf File Size Programmatically in Java

Are you curious about how to reduce pdf file size programmatically in java? Or about how to reduce file size in java? Read this article on compress pdf programmatically. Knowing the tips of how to reduce pdf file size programmatically can help you to decrease pdf file size a lot. By reducing your pdf file size you are reducing input document data which could be your source of problems and issues. You might have a large file for upload and you may be wondering how can we reduce pdf file size programmatically java. You might have found some articles and tutorials claiming that there is no way possible to compress pdf using programming languages such as Java, C++, Android or others etc.. However, this complete guide on how to reduce PDF file size programmatically java is assuming that you are discouraged by those methods and that your mind is set on finding a solution to compress pdf using programming languages including Java.

How to Reduce Pdf File Size Programmatically in Java. Compressing files can save you a lot of space and make it much easier to email them. There are lots of ways to reduce these file sizes down to something more manageable.

Compressing PDF files is a very common thing to do, and you can compress PDF files either in a manual way (by using a graphical user interface) or programmatically (by using code). Although it might sound easier to compress PDF files by using GUI, it’s a good idea to learn how to compress PDF files using code. This is because when you need to compress many files, the best option is to do this in batch.

In this tutorial, you will learn how to reduce pdf file size in java without using any external API or any paid software. Yes, you read it correct, there is an API which can remove the junk from the PDF files and reduce its size programmatically. This API also remove some useless objects (fonts) from the PDF files which is why these PDF files becomes small in size.

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:

// Open document
Document pdfDocument = new Document(“Original.pdf”);
// Optimize for web
// Save output document“Optimized_output.pdf”);

view hosted with ❤ by GitHub

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:

// Load input document
Document doc = new Document(dataDir + “Test.pdf”);
// Initialize OptimizationOptions object
OptimizationOptions opt = new OptimizationOptions();
// Enable image compression
// Set the quality and resolution of images in PDF file
// Save the updated file + “compressingPDFWithImages_out.pdf”);

view hosted with ❤ by GitHub

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:

Document doc = new Document(dataDir + “Test.pdf”);
OptimizationOptions opt = new OptimizationOptions();
// Either
// Unembed all fonts in PDF
// only keep embedded fonts for used characters
// link duplicate streams
// Remove unused streams
// Remove unused objects
// Save the updated file + “compressingPDF.pdf”);

view hosted with ❤ by GitHub

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:

// Open document
Document pdfDocument = new Document(dataDir + “OptimizeDocument.pdf”);
// Iterate through each page and annotation
for (Page page : pdfDocument.getPages())
for (Annotation annotation : page.getAnnotations())
// Either flatten the annotation
// OR delete the annotation
// page.getAnnotations().delete(annotation);
// Save optimized PDF document + “OptimizeDocument_out.pdf”);

view hosted with ❤ by GitHub

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
// Load source PDF form
Document doc = new Document(dataDir + “input.pdf”);
// Flatten Form fields
if (doc.getForm().getFields().length > 0)
for (Field item : doc.getForm().getFields())
dataDir = dataDir + “FlattenForms_out.pdf”;
// Save the updated document;

view hosted with ❤ by GitHub

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:

// Load input PDF document
Document document = new Document(“input.pdf”);
// Initialize RgbToDeviceGrayConversionStrategy instance
RgbToDeviceGrayConversionStrategy strategy = new RgbToDeviceGrayConversionStrategy();
for (int idxPage = 1; idxPage <= document.getPages().size(); idxPage++) {
Page page = document.getPages().get_Item(idxPage);
// Convert color space of each page to Greyscale
// Save output PDF document“output.pdf”);

PDF files can be surprisingly big, and if you don’t know how to reduce PDF file size, they could be hard to manage and share via email or other platforms. There are a few easy-to-use programs and apps that can help you compress the size of a PDF file and make it smaller. Whether it’s standard or encrypted (password-protected) PDF files, these apps and programs have got you covered. What’s more, the methods we’re sharing with you reduce the size of a PDF without comprising on quality. So without any further ado, let’s take a look at how to reduce PDF file size without losing quality.

How to reduce PDF file size

  • Adobe Acrobat
  • SmallPDF
  • DocuPub
  • 4DotsFree PDF Compress
  • Preview

1. Adobe Acrobat

You don’t need any other app or program if you’ve Adobe Acrobat installed on your computer. The software has a tool called PDF Optimiser that lets you cut down additional fat off the PDF file.

  • To reduce PDF file size in Adoba Acrobat, open the file in Acrobat, select ‘Tools‘, and click on ‘PDF Optimiser‘ from the drop-down menu.
  • A toolbar with a few options will appear just above the PDF; select ‘Reduce File Size‘ option here
  • Once you do that, Acrobat will apply default settings to your file to attempt to reduce its size.
  • Within the ‘Advanced Optimisation‘ option, controls for quality of images, fonts, transparency, and among other things are available.
  • Furthermore, click on the ‘Audit Space Usage‘ from the top-left corner in the Advanced Optimisation window to see how much space each kind of element is taking up within the file.

2. SmallPDF

If you don’t have Adobe Acrobat on your laptop, you can take the browser’s help to reduce PDF file size without losing its quality. You can also use this method to reduce PDF files on mobile phones. There are several options out there, but SmallPDF is the easiest to use. The website comes with both free and paid versions; however, the former is good as long as you’re not compressing more than two files a day.

  • To reduce PDF file size, head to the SmallPDF website and select ‘Compress PDF‘ option from the home screen
  • Next, ‘Choose Files‘ you wish to compress; you can also drag and drop the file on the screen
  • Once the file is uploaded, you’ll get two options to download the ‘Basic Compression‘ file or the ‘Strong Compression‘ file. The latter gives you the lowest size of the file, but it does take a hit on the quality and comes with a paywall
  • The Basic Compression is free and doesn’t reduce the quality of the PDF file too much. Select the option and hit the ‘Choose Option‘ button
  • You can download the PDF file in reduced size from the follow-up screen

The process doesn’t require a sign-in, and if you’re worried about security, the file is deleted from SmallPDF’s servers after an hour.

3. DocuPub


If you’re looking for more variations to reduce PDF file size in the browser other than SmallPDF, go with DocuPub. The platform allows you to manually resize and rescale settings of the PDF file. The only trade-off is that DocuPub has an upload limit of just 24MB per file.

  • The process of uploading the PDF file to DocuPub is the same as SmallPDF, i.e. go to the website and select the file you wish to upload
  • After that, adjust the resize/ rescale settings and click on the ‘Upload & Resize‘ option
  • The PDF file will resize and from the following screen, you can download and save the file on your computer

4. 4DotsFree PDF Compress

PDF files can also be compressed and made smaller with several third-party apps on Windows laptop – 4dots Free PDF Compress is one of them. It’s free and doesn’t require any technical knowledge. All you have to do is:

  • Download 4dots Free PDF Compress and install it on your Windows 10 laptop or PC
  • Open the app and click Add file to upload the PDF file that you want to compress
  • You’ve the option to adjust the quality of the images.
  • Once done, hit ‘Compress‘ and you’re done. The compressed PDF file will then be saved locally on your Windows 10 computer.

You can also compress multiple PDF files at once with this freeware application.

5. Preview

Mac users can shrink the size of the PDF file with the built-in tool Preview. To do so,

  • Open your PDF file in Preview. It should be the default option, but in case it isn’t, Right Click on the PDF file, select Open with > Preview.
  • Then, click File > Export, and in the Quartz Filter drop-down box, select Reduce File Size
  • The software will automatically reduce the size of the PDF file. Hit save and store the compressed PDF file on your system.

These were a few ways you can reduce PDF file size for free without losing quality on your computer. It should ease your troubles with large PDF files.

How to determine the size of your PDF document

If you’re using Adobe Acrobat, checking the size of your PDF is simple. Just click on File, then Properties. You’ll see lots of information about your document, including its size.

How to find the size of a PDF

Alternatively, you can navigate to the folder containing your document, right-click on it, and click Properties.

Larger files will take longer to upload, are cumbersome to share electronically (via email, for instance), and will use up your law firm’s file storage more rapidly. And even when the courts accept larger file sizes, they prefer to handle smaller, optimized files.


Reducing PDF file size is an interesting use-case because of the file size inherent to PDF files. Most people have even experienced, at one point or another, the slow downloading speed given by large PDF files. Therefore, reducing PDF file size programmatically comes with a long list of benefits such as reducing the size of the output file, improving the performance and processing speed of your application, etc.

If you’ve been around the Java block you probably know that there’s no direct way to compress / decompress files programmatically using java. Sure, you have a couple of options to do this such as using split functionality and compressing the individual files separately. But what if you wanted to compress your file, as a whole? Keeping in mind that compression is a different process.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *