How to convert Word DOCX to PDF in Java

Cloudmersive
Nov 4 · 1 min read

This tutorial will save you a ton of time with document conversion. Without any further ado, let’s get to the step-by-step.

Let’s set up our Repository and Dependency references in Maven:

<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>v2.32</version>
</dependency>
</dependencies>

Now we simply use the function convertDocumentDocToPdf:

// 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/file"); // File | Input file to perform the operation on.
try {
byte[] result = apiInstance.convertDocumentDocToPdf(inputFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertDocumentApi#convertDocumentDocToPdf");
e.printStackTrace();
}

And that is absolutely all there is to it. We offer a range of related APIs, covering other document format conversions as well as editing, so be sure to check out the rest of our documentation for more information.

Cloudmersive

Written by

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs. https://www.cloudmersive.com/

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade