Add a Table in an Online Word Document in C#

Cloudmersive
2 min readJan 29, 2021

In previous posts, we explained how to use both our Begin Editing and Finish Editing APIs in C# to open and close out editing for an online Word document. This tutorial will highlight one of the editing functions that you can perform in between these two APIs — inserting a new table.

Before we begin the process, we will need 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"
}

Now, we can run this command to install the .NET Framework SDK:

Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 3.2.8

Then we can call the function with the following code:

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;
namespace Example
{
public class EditDocumentDocxInsertTableExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");
var apiInstance = new EditDocumentApi();
var reqConfig = new InsertDocxTablesRequest(); // InsertDocxTablesRequest | Document input request
try
{
// Insert a new table into a Word DOCX document
InsertDocxTablesResponse result = apiInstance.EditDocumentDocxInsertTable(reqConfig);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling EditDocumentApi.EditDocumentDocxInsertTable: " + e.Message );
}
}
}
}

The returned result will be an updated document with the newly inserted table in its respective spot. If you have no other editing operations to perform, you can download the document with the aforementioned Finish Editing API. To retrieve your API key and gain access to 800 monthly calls across our multitude of APIs, simply go to the Cloudmersive website and register for a free account.

--

--

Cloudmersive

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