Add Rows to Existing Table

I am creating a document based on an existing Word template that has existing tables within it.
I am trying to insert rows at a certain position within the table. It is a 5 row table with the first row being a header and rows 3-5 are footer rows. So when I insert a row I need to add it between row 2 and 3. The column layout of the new row needs to duplicate row 2.
So far all I have is:

var nodes = document.GetChildNodes(NodeType.Table, true);
Aspose.Words.Tables.Table table = nodes[6] as Table;

Ideally I could just do something like:

table.Rows[2].AddRow();

and that would do it.
I have found table.Rows.Add(node); but I don’t know where the value of this node param comes from nor can I specify at which row position to place it.
How can I do this?

Hi Walter,

Thanks for your inquiry. Sure, you can duplicate an existing Table Row, it’s column layout and insert the cloned Row at any position in Table. Please try run the following code snippet:

Document doc = new Document(@"C:\Temp\in.docx");
Table table = doc.FirstSection.Body.Tables[0];
Row clonedRow = (Row) table.Rows[1].Clone(true);
foreach(Cell cell in clonedRow.Cells)
{
    cell.RemoveAllChildren();
    cell.EnsureMinimum();
    cell.FirstParagraph.Runs.Add(new Run(doc, "New Row"));
}
table.Rows.Insert(2, clonedRow);
doc.Save(@"C:\Temp\out.docx");

I hope, this helps.

PS: Sample input / output Word documents are attached with this post.

Best regards,

Hello,
Thank you for the help. That worked but when the row is cloned it loses the cloned row’s font formatting. I see properties to format a cell but it doesn’t format the font. How can I do this?
Also, I need to write text inside specific cells of the row. I see a GetText method but no corresponding SetText. For now I’m writing the text to it as so:

clonedRow.Cells[0].FirstParagraph.Runs.Add(new Run(document, "a"));

Is there a better way to do this?

Hi Walter,

this is the solution I used. The code is VB6 but I think you can figure out, what I’m doing…

Sub insertRows(oTable As Aspose_Words.Table, Rows As Long)

    On Error GoTo Hell
    Dim row As Long
    Dim oRow As Aspose_Words.row
    Dim oCell As Aspose_Words.Cell

    doTrace "Inserting " & Rows & " rows in table"

    For row = 1 To Rows
    'Clone the last row for each row

    Set oRow = oTable.LastRow.Clone(True)
    'Clear the text in the newly added row (because there are
    'CustomDocumentProperties in each cell I have to do this)

    For Each oCell In oRow.Cells
            Do While (oCell.Paragraphs.Count > 1)
                oCell.LastParagraph.Remove
            Loop
            'Remove content of first paragraph
            oCell.FirstParagraph.ChildNodes.Clear
        Next oCell
        'Add row to table
        oTable.Rows.Add oRow
    Next
    Exit Sub
Hell:
    ReportError Erl, "modDefinitions.insertRows"
End Sub

I hope this helps…

As for the writing of the cells…you need to use the DocumentBuilder…like this:

oDocumentBuilder.MoveToCell TableIndex, oWordDoc, Row, Col, 0
oDocumentBuilder.Write "text"

Regards,
Michael

Hello Michael,
Thanks for the response but unfortunately, the second call in my loop to this line:

builder.MoveToCell(tableIndex, InsertAtRowIndex, 0, 0);

fails with an invalid rowIndex error even though my table has 7 rows and the InsertAtRowIndex value is 3.
Again, the first call of it with a value of 2 works but if I try it again, failure.
I’ve now have spent 6 hours on this cell formatting today with no success.

Hi Walter,

Thanks for your inquiry.

Walter:
That worked but when the row is cloned it loses the cloned row’s font formatting. I see properties to format a cell but it doesn’t format the font. How can I do this?

Could you please attach your input Word document along with the target document showing the desired output here for testing? I will investigate the structure of your documents on my side and provide you code to achieve the same using Aspose.Words. You can create your target Word document by using Microsoft Word application.

*Walter:
Also, I need to write text inside specific cells of the row. I see a GetText method but no corresponding SetText. For now I’m writing the text to it as so:

clonedRow.Cells[0].FirstParagraph.Runs.Add(new Run(document, “a”));*

You can use DocumentBuidler.MoveToCell method to move cursor to the beginning of the cell:

// Open document and create DocumentBuidler.
Document doc = new Document(@"Test001\in.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
// move builder cursor to the beggining of the first cell of the first table.
builder.MoveToCell(0, 0, 0, 0);
// Insert some text.
builder.Write("This is text at the begginng of the cell");
// Save output document.
doc.Save(@"Test001\out.doc");

Best regards,