How to get all tables in a Word DOCX document in C# .NET Framework

Cloudmersive
2 min readFeb 3, 2020

--

Extracting table data from a DOCX file is certainly a headache, however useful it might be. Luckily for you, today’s post is about how to circumvent this aggravation and get straight to the results. We’ll be done in 5 minutes flat.

In your package manager console, run the following command, which will install the API client that we will be calling on.

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

Now we simply call EditDocumentDocxGetTables with the chunk of code you see here:

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 EditDocumentDocxGetTablesExample{public void main(){// Configure API key authorization: ApikeyConfiguration.Default.AddApiKey("Apikey", "YOUR_API_KEY");// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed// Configuration.Default.AddApiKeyPrefix("Apikey", "Bearer");var apiInstance = new EditDocumentApi();var reqConfig = new GetDocxTablesRequest(); // GetDocxTablesRequest | Document input requesttry{// Get all tables in Word DOCX documentGetDocxTablesResponse result = apiInstance.EditDocumentDocxGetTables(reqConfig);Debug.WriteLine(result);}catch (Exception e){Debug.Print("Exception when calling EditDocumentApi.EditDocumentDocxGetTables: " + e.Message );}}}}

And that’s about all there is to it. Wondering what your table data will look like? Below you can see the format of your output.

{"Successful": true,"Tables": [{"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}]}

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet