Issue of inserting new rows


We are using Aspose.Word

We are currently using the below method to insert a new row at the end of specified table.

private void CopyTableRow(int tableIndex, int rowIndexToCopy, int rowIndexToInsertAfter)
    DocumentBuilder db = new DocumentBuilder(document);
    db.MoveToCell(tableIndex, rowIndexToCopy, 0, 0);
    Node node = db.CurrentNode;
    Node parentNode = node.ParentNode;
    Node cellNode = parentNode.ParentNode;
    Node rowNode = cellNode.ParentNode;

    Node rowClone = rowNode.Clone(true);

    db.MoveToCell(tableIndex, rowIndexToInsertAfter, 0, 0);
    Node nodeAfter = db.CurrentNode;
    Node parentNodeAfter = nodeAfter.ParentNode;
    Node cellNodeAfter = parentNodeAfter.ParentNode;
    Node rowNodeAfter = cellNodeAfter.ParentNode;

    rowNode.ParentNode.InsertAfter(rowClone, rowNodeAfter);

As you can tell, it’s not a very pratical way. It worked till I added another table to the document tamplate. The 1st row has several lines in it, after applied the above method, some of the contents from the 1st row are carried to the 2nd. Then the 3rd row carries some lines from the 2nd and 1st. The last row contains the lines accumulated from 1st to Last -1.

Is there any way to avoid the above issue?


Hi Lewis,

Thanks for your request. Could you please attach your source document, current output document and expected result here? I will investigate the issue and provide you more information.
Best regards.

HI Alexey,

Thanks for your reply.

The source code along with the source document, current output document and the expected result are attached.




Thank you for additional information. You just need clear cells before inserting new values. I think the following code could be useful for you:

// Open document
Document doc = new Document(@"Test297\in.doc");
// Create DocumentBuilder
DocumentBuilder builder = new DocumentBuilder(doc);
// Get table from the first section of document
Table myTable = doc.FirstSection.Body.Tables[0];
// Get last row of table
Row myRow = myTable.LastRow;
// Add 10 more rows into the table
for (int rowIndex = 0; rowIndex <10; rowIndex++)
    // Clone last row
    Row newRow = (Row) myRow.Clone(true);
    // Insert created row into the table
    // Loop through all cells in row
    foreach(Cell cell in newRow)
        // Remove old content from the cell
        // Remove paragraphs
        while (cell.Paragraphs.Count> 1)
        // Remove content of first paragraph
        // Move DocumentBuilder cursor to the cell
        // Insert new content
        builder.Write("This is new content");
// Save document

Hope this helps.
Best regards.