How to rasterize a PDF into an image-based PDF in Java
1 min readFeb 12, 2020
Rasterizing PDF files just got a whole lot easier. Forget about those hours of coding you were dreading, because today we are going to use an API to cut things down to a handful of minutes.
We will be using Jitpack to compile our library, which will require the following references:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
And:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.34</version>
</dependency>
</dependencies>
Once our library is ready, we can invoke editPdfRasterize and drop in our PDF file.
// 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: ApikeyApiKeyAuth 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/file"); // File | Input file to perform the operation on.try {byte[] result = apiInstance.editPdfRasterize(inputFile);System.out.println(result);} catch (ApiException e) {System.err.println("Exception when calling EditPdfApi#editPdfRasterize");e.printStackTrace();}
Ok, our PDF is now rasterized. Not bad, huh? If you liked this approach, we have a cornucopia of similar functions that run the productivity gamut.