How to Fill In a Data Table within a DOCX Document using Java

Cloudmersive
2 min readSep 9, 2022

--

When it comes to programmatically filling out tables & charts, working within Excel spreadsheets is probably the first & most relevant business application that comes to mind. It isn’t too often we think of targeting DOCX documents with that process, but it’s just as easy to do — and DOCX files offer a natural advantage over Excel in some respects, capable of structuring tables/charts alongside formatted, written text in a vertically or horizontally aligned view. With our Fill DOCX Table API, you can programmatically add content to tables within a DOCX document, specifying the TableStart & TableEnd values and providing specific data to add into the relevant cells.

Below, I’ve demonstrated how you can structure an API call & format your parameters using ready-to-run Java code examples. The best part? You can use this API for free by registering a free account on our website & authenticating the operation with a secure API key. Free accounts provide a limit of 800 API calls per month, which is perfect for keeping costs down on small-scale operations & achieving scale in the long run.

Our first step is to install the API client with Maven. We can accomplish that starting by adding a reference to the repository in pom.xml:

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

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

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

Now we can jump over to the controller & add our imports to the top of our file:

// 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.TransformDocumentApi;

Lastly, we can begin to structure our API call. Let’s first ready our input parameters using the following JSON format:

{
"InputFileUrl": "string",
"InputFileData": "string",
"TableStartTag": "string",
"TableEndTag": "string",
"DataToFillIn": [
{
"Cells": [
{
"TargetTag": "string",
"ReplacementValue": "string"
}
]
}
]
}

And then let’s pass our arguments through the function:

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");
TransformDocumentApi apiInstance = new TransformDocumentApi();
DocxTableTableFillRequest request = new DocxTableTableFillRequest(); // DocxTableTableFillRequest |
try {
byte[] result = apiInstance.transformDocumentDocxTableFillIn(request);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling TransformDocumentApi#transformDocumentDocxTableFillIn");
e.printStackTrace();
}

All you need to do is authenticate your API key where indicated in the comments, and you’re good to go. Nice & easy!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet