How to Convert DocX Files to JPG/JPEG Image Arrays using Java

Cloudmersive
2 min readJun 9, 2022

Converting Word Documents to Image Arrays is a great way to make the contents of those documents readable across any operating system. If you’re looking to make that conversion to JPG, we can help: our DocX to JPG Conversion API will make it possible to build that operation into your application and customize the output image quality. To help you structure your API call, we’ve provided instructions to use complementary, ready-to-run code in Java below.

Let’s begin by installing the Java SDK. To do so with Maven, first add a reference to the repository in pom.xml:

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

Then, add a reference to the dependency in pom.xml:

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

Lastly, it’s time to include the import classes and call the API:

// 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.ConvertDocumentApi;
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");
ConvertDocumentApi apiInstance = new ConvertDocumentApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
Integer quality = 56; // Integer | Optional; Set the JPEG quality level; lowest quality is 1 (highest compression), highest quality (lowest compression) is 100; recommended value is 75. Default value is 75.
try {
DocxToJpgResult result = apiInstance.convertDocumentDocxToJpg(inputFile, quality);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertDocumentApi#convertDocumentDocxToJpg");
e.printStackTrace();
}

Make sure to include your Cloudmersive API key where indicated in the documentation, and you’re all set. To get a key, head to our website and register a free account.

--

--

Cloudmersive

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