1

Although the Word DOCX format is the first choice for creating text documents, it may not be enough when we enter the web-based field.

As we discussed in the previous article, although the Word DOCX format is the first choice for creating text documents, it may not be enough when we enter the web-based field. When considering formatting online documents, Hypertext Markup Language (HTML) became one of the clear winners for applications and websites. This dynamic language uses set prompts or elements to build documents that can be transmitted to the browser and presented to the end user as a readable web page. The structure of HTML even allows the integration of images, interactive forms, and other objects that are more difficult to create with simple Word documents.

The Word DOCX format is actually based on a different markup language XML (Extensible Markup Language). Microsoft converted its most popular programs—Word, Excel, and PowerPoint—to an open standard, XML-based format in the mid-2000s. This move aims to improve file size, image compression and security, and maintain an advantage over competitors. Although some users still prefer the older DOC version due to compatibility with other platforms, the DOCX format is usually a better choice for current word processing projects.

Due to the nuances of each format, if you try to draft HTML code from a Word document, the additional formatting and styles associated with the file format may hinder its use. In addition, the complexity of the DOCX design makes parsing information an unpleasant task. In this tutorial, we will demonstrate how to use the API in Java to automatically convert any DOCX file to HTML, thereby instantly improving compatibility and product quality.

First, we will install 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 references to dependencies in pom.xml:

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

After the installation is complete, we are ready to add the import to the top of our controller and use the following code to call the conversion function:

// 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 that the process runs correctly, you need to enter a few 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 visit the Cloudmersive website to register for a free account; this provides 800 calls per month in our API library.
The process will return an HTML document that can be easily used on the applicable website and/or application.

Reference: "2020 latest Java basics and detailed video tutorials and learning routes! 》


Java攻城师
451 声望392 粉丝

本人太过于丰富,无法简介