How to Reduce PDF File Size & Optimize using Java
When PDFs get a little too big and start slowing you down, we’ve got you covered with an excellent file conversion service. Our PDF Optimization API will reduce the PDF file size and optimize its contents automatically. You can customize the level of quality for the output file, ranging from 0.0 (low quality) to 1.0 (high quality); the default setting is 0.3 (medium/low quality). To help you take advantage of this API, we’ve provided complementary code examples in Java to structure your API call.
Let’s get started by installing the Java SDK with Maven. First, include a reference in the pom.xml repository:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Then include a reference in the pom.xml dependency:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
That concludes installation — now we can add in the import classes and call the API. Make sure you have the following parameters ready:
- Your input file for the operation.
- Your Cloudmersive API key (obtain one 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();
}