How to Rasterize a PDF to an Image-Based PDF with Java

Cloudmersive
2 min readJun 14, 2022

It’s very common to convert office documents to PDF format so they can be easily viewed on any operating system. Since PDFs are vector files, however, it’s also common for applications to automatically convert them into 2D images for online viewership or printing purposes. That image conversion process is called rasterization, and you can easily include that operation into your own application/website by calling our PDF Rasterization API as an external cloud service. This API will output a new PDF with each page comprised of a high-resolution image.

To help you structure your API call in Java, we’ve provided ready-to-run code examples below.

Let’s begin by installing Maven. Add a reference to the pom.xml repository:

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

Next, let’s add a reference to the pom.xml dependency:

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

After that, let’s copy in our import classes:

// 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;

Finally, use the remaining code to structure your API call. Only two parameters are required here:

  1. Your input PDF File
  2. Your Cloudmersive API key (obtainable by registering a free account on our website — this will provide a limit of 800 API calls per month)
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.
try {
byte[] result = apiInstance.editPdfRasterize(inputFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling EditPdfApi#editPdfRasterize");
e.printStackTrace();
}

--

--

Cloudmersive

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