How to Insert a Table in an Online DOCX Document in Java

Cloudmersive
2 min readJan 27, 2021

--

If you’re editing an online Word document and need to insert a table with relevant data, we’re here to make that a less painful process. This article will detail the process for inserting a table in DOCX using Java, and is a complementary article to the previously posted Begin Editing and Finish Editing tutorials.

First things first — here is the request configuration:

{
"InputFileBytes": "string",
"InputFileUrl": "string",
"TableToInsert": {
"TableID": "string",
"Path": "string",
"Width": "string",
"WidthType": "string",
"TableRows": [
{
"RowIndex": 0,
"Path": "string",
"RowCells": [
{
"CellIndex": 0,
"Path": "string",
"Paragraphs": [
{
"ParagraphIndex": 0,
"Path": "string",
"ContentRuns": [
{
"RunIndex": 0,
"Path": "string",
"TextItems": [
{
"TextIndex": 0,
"Path": "string",
"TextContent": "string"
}
],
"Bold": true,
"Italic": true,
"Underline": "string",
"FontFamily": "string",
"FontSize": "string"
}
],
"StyleID": "string"
}
],
"CellShadingColor": "string",
"CellShadingFill": "string",
"CellShadingPattern": "string",
"CellWidthMode": "string",
"CellWidth": "string"
}
]
}
],
"TopBorderType": "string",
"TopBorderSize": 0,
"TopBorderSpace": 0,
"TopBorderColor": "string",
"BottomBorderType": "string",
"BottomBorderSize": 0,
"BottomBorderSpace": 0,
"BottomBorderColor": "string",
"LeftBorderType": "string",
"LeftBorderSize": 0,
"LeftBorderSpace": 0,
"LeftBorderColor": "string",
"RightBorderType": "string",
"RightBorderSize": 0,
"RightBorderSpace": 0,
"RightBorderColor": "string",
"CellHorizontalBorderType": "string",
"CellHorizontalBorderSize": 0,
"CellHorizontalBorderSpace": 0,
"CellHorizontalBorderColor": "string",
"CellVerticalBorderType": "string",
"CellVerticalBorderSize": 0,
"CellVerticalBorderSpace": 0,
"CellVerticalBorderColor": "string",
"StartBorderType": "string",
"StartBorderSize": 0,
"StartBorderSpace": 0,
"StartBorderColor": "string",
"EndBorderType": "string",
"EndBorderSize": 0,
"EndBorderSpace": 0,
"EndBorderColor": "string",
"TableIndentationMode": "string",
"TableIndentationWidth": 0
},
"InsertPlacement": "string",
"InsertPath": "string"
}

Next, we can install the SDK 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 add a reference to the dependency:

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

Once these have been added, we can call the insert table 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.EditDocumentApi;
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");
EditDocumentApi apiInstance = new EditDocumentApi();
InsertDocxTablesRequest reqConfig = new InsertDocxTablesRequest(); // InsertDocxTablesRequest | Document input request
try {
InsertDocxTablesResponse result = apiInstance.editDocumentDocxInsertTable(reqConfig);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling EditDocumentApi#editDocumentDocxInsertTable");
e.printStackTrace();
}

If your document editing is complete after this transaction, don’t forget to leverage the Finish Editing API to close out the process and download your new document!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet