GemBox Support Forum

Add a new table between 2 tables

I have a DOCX file with 20 tables and I want to add new tables between them, but I only could add in the beginning or in the end.

Hi Vitor,

Here is an example that will add a new Table after the first Table in the document:

var document = DocumentModel.Load("input.docx");

// Get first table in the document.
var table = (Table)document.GetChildElements(true, ElementType.Table).First();

// Find table's index.
int index = table.ParentCollection.IndexOf(table);

// Add empty paragraph to separate the previous and the new table.
table.ParentCollection.Insert(index + 1, new Paragraph(document));

// Add new table.
var newTable = new Table(document, 10, 4, (r, c) => new TableCell(document, new Paragraph(document, "Sample")));
table.ParentCollection.Insert(index + 2, newTable);


Note, I’m separating those two tables with empty Paragraph because otherwise, Microsoft Word would treat them as a single Table (it would silently combine them into one Table element).

I hope this helps.