How to Optimize a PDF (Reduce File Size) in Java

Cloudmersive
2 min readAug 25, 2022

--

A PDF document is often made up of a variety of different content types, including vector graphics, raster graphics, formatted/unformatted text, and more. Even though it’s common to use PDFs as a way of compressing larger file types (like PPTX for example), the resulting PDF file can still be a little hefty — and it often makes sense to further compress the file to reduce its burden on your network. You can accomplish this easily with a PDF optimization API, which will minimize the file size of an input PDF and provide customizable parameters concerning the quality of content within the document itself.

Our iteration of this PDF optimization service allows you to select the exact quality of the images within your PDF by inputting a number between 0.0 (low quality) and 1.0 (high quality). The default quality rating is 0.3 on this scale. Below, I’ll demonstrate how you can use this API by structuring your API call with ready-to-run Java code examples provided below.

To get started, let’s install the SDK with Maven. Add the below reference to the pom.xml repository:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Next, add a reference the pom.xml dependency:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

Lastly, complete the API call with the below code. Below the imports, you’ll notice a prompt to include an API key; you can get a secure API key by registering a free account on our website:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.EditPdfApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
EditPdfApi apiInstance = new EditPdfApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
BigDecimal quality = new BigDecimal(); // BigDecimal | Quality level for the images in the PDF, ranging from 0.0 (low quality) to 1.0 (high quality); default is 0.3
try {
byte[] result = apiInstance.editPdfReduceFileSize(inputFile, quality);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling EditPdfApi#editPdfReduceFileSize");
e.printStackTrace();
}

And that’s all you need — no further code required. Nice and easy.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.