by BehindJava

How to Convert DOCX to HTML in Java

Home » java » How to Convert DOCX to HTML in Java

In this tutorial we are going to learn about converting DOCX to HTML in Java While the Word DOCX format is the go-to for creating text documents, it can be insufficient when we enter the web-based territory.

As we have discussed in previous articles, while the Word DOCX format is the go-to for creating text documents, it can be insufficient when we enter the web-based territory. When considering formatting for online documents, it is Hyper Text Markup Language (HTML) that emerges as one of the clear winners for applications and websites. This dynamic language utilizes set cues or elements to construct documents that can be transmitted to browsers and presented to end-users as a readable web page. The structure of HTML even allows for the integration of images, interactive forms, and other objects that are more difficult to create with a straightforward Word document.

The Word DOCX format is actually based on a different markup language, XML (Extensible Markup Language). Microsoft transitioned its most popular programs – Word, Excel, and PowerPoint – to an open standard, XML-based format in the mid-2000s. This move was to create improvements in file size, image compression, and security, as well as to maintain an edge over their competitors. While some users still prefer the older DOC version due to its compatibility with other platforms, the DOCX format is generally the better choice for current word processing projects.

Due to the nuances of each format, if you are attempting to draft HTML code from a Word document, the additional formatting and styles associated with the file format can hinder its use. In addition to this, the complexities of the DOCX design make parsing the information an unpleasant task. In this tutorial, we will demonstrate how you can use an API in Java to automatically convert any DOCX file to HTML, providing instant improvements in compatibility and product quality.

To kick things off, we will install with Maven by adding a reference to the repository in pom.xml:

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

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

<dependencies>
<dependency>
    <groupId>com.github.Cloudmersive</groupId>
    <artifactId>Cloudmersive.APIClient.Java</artifactId>
    <version>v3.90</version>
</dependency>
</dependencies>

After the installation, we are ready to add the imports to the top of our controller and call the conversion function with the following 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.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.
try {
    byte[] result = apiInstance.convertDocumentDocxToHtml(inputFile);
    System.out.println(result);
} catch (ApiException e) {
    System.err.println("Exception when calling ConvertDocumentApi#convertDocumentDocxToHtml");
    e.printStackTrace();
}

To ensure the process runs correctly, you will need to input a couple of parameters:

  • File – the DOCX file to perform the operation on.
  • API Key – your personal API key. If you need to obtain an API key, you can do so by visiting the Cloudmersive website to register for a free account; this provides 800 calls/month across our library of APIs.

The process will return an HTML document that can be easily utilized for applicable websites and/or applications.