Finding a table and adding a row

Hello! Apologies if this is not the right spot. :)

I'm working on a ASP.Net core website/application where I am pulling in a word document and doing operations on them. What I'm having trouble is figuring out how to "find" a specific table, and then add a row(s) to that table. 

So - reductively, I have a word document (WordDocument sourceDocument) with the text "Table 1" with a table right after it, and a "Table 2" with a table right after it.  I want to be able to specifically look for the table after "Table 2" and then add a row to it. But I'm kind of stumped. Any pointers or any direction would be helpful!


1 Reply 1 reply marked as answer

SB Sneha Biju Syncfusion Team March 27, 2024 10:01 AM UTC

Hi Sydney,

From the given details, we found that your requirement is to find a specific table from a Word document and add a new row to that table. In order to achieve this,

Suggestion 1:
We suggest you set alternate text, such as a title or description, to the table. This will allow you to locate the table using the FindItemByProperty API.

//Open an existing Word document

WordDocument doc = new WordDocument(@"Sample.docx");

//Find the table using the table Title

WTable table = doc.FindItemByProperty(EntityType.Table, "Title", "Table2") as WTable;

//Add a row

table.AddRow();

//Save the Word document

doc.Save(@"Output.docx");


Alternatively, you can utilize DocIO to set the alternate text for the table.

//Specifies the title for the table

table.Title = "Table2";

//Specifies the description of the table

table.Description = "This is the second table";


Suggestion 2:
Another approach is to find the table by its index within the Word document.

//Opens an existing Word document

WordDocument document = new WordDocument(@"Sample.docx");

//Accesses the instance of the first section in the Word document

WSection section = document.Sections[0];

//Accesses the instance of the second table in the section

WTable table = section.Tables[1] as WTable;

//Add row

table.AddRow();

//Save the document

document.Save(@"Output.docx");


Regards,
Sneha.


Marked as answer
Loader.
Up arrow icon