How to convert HTML to a Word DOCX Document in Java

HTML allows for a high degree of customization and power, but sometimes this comes at the cost of convenience and accessibility. Sometimes you really just need to convert to an easier format for general viewing and printing. Today we will be taking a look at how to accomplish this easily and automatically using a Cloudmersive API. Let’s get started.

First we need to establish our repository and dependency references in Maven POM.





Next we must call the function convertWebHtmlToDocx with the following lines of code:

// 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.ConvertWebApi;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");ConvertWebApi apiInstance = new ConvertWebApi();HtmlToOfficeRequest inputRequest = new HtmlToOfficeRequest(); // HtmlToOfficeRequest | HTL input to convert to DOCXtry {byte[] result = apiInstance.convertWebHtmlToDocx(inputRequest);System.out.println(result);} catch (ApiException e) {System.err.println("Exception when calling ConvertWebApi#convertWebHtmlToDocx");e.printStackTrace();}

And that’s all there is to it. This can be used to rapidly convert vast quantities of files in a very short time. Simple, right?

Image for post

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store