We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Reg Adding rows dynamically to the Existing table in the document

I have a document and there is a table with headers in it and rows need to be added dynamically from the List
generating PDF through ASPOSE.Words
I need to add the rows dynamically to the existing table in the document
Please find the code as below:
I am getting the table and cloning the row.
But how do i add the values to the cells in the row.

Table firstTable = (Table)doc.getChild(NodeType.TABLE, 0, true);
Row myRow = firstTable.getLastRow();
Row newRow = (Row)myRow.deepClone(true);
firstTable.appendChild(newRow);

Please find the attached sample document for my project.
Please help me on this ASAP.
Thanks & Regards
Madhava

Hi Madhava,

Thanks for your inquiry. Please check following code snippet. Hopefully it would serve the purpose.

Document doc = new Document(MyDir + "CAPSHEET.docx");
// Retrieve the first table in the document.
Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);
// Clone the last row in the table.
Row clonedRow = (Row)table.getLastRow().deepClone(true);

// Remove all content from the cloned row's cells. This makes the row ready for
// new content to be inserted into.
for (Cell cell: clonedRow.getCells())
{
    cell.getFirstParagraph().getRuns().clear();
    cell.getFirstParagraph().appendChild(new Run(doc, "hello text"));
}
// Add the row to the end of the table.
table.appendChild(clonedRow);

doc.save(MyDir + "Table.AddCloneRowToTable Out.doc");

Please feel free to contact us for any further assistance.

Best Regards,

I need to add the rows dynamically to the existing table in the document, show my .docx template:

Transactions
Reference Number Transaction Date Post Date Description of Transaction or Credit Amount
[TABLE]
Add new rows
Fees
[FEES]
Add new rows
TOTAL FEES FOR THIS PERIOD [TOTALFEESPERIOD]
Interest Charged
[INTEREST]
Add new rows
TOTAL INTEREST FOR THIS PERIOD [TOTALINTEREST]
[YEAR] Totals Year-to-Date
Total fees charged in [YEAR] [YEARTOTALFEES]
Total interest charged in [YEAR] [YEARTOTALINTEREST]
NOTICE: SEE THE NEXT PAGE FOR IMPORTANT INFORMATION

Hi David,

Thanks for your inquiry. You can use the following code to make a clone of the last row of a table and append it to the table.

// Retrieve the first table in the document.
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);
// Clone the last row in the table.
Row clonedRow = (Row)table.LastRow.Clone(true);
// Remove all content from the cloned row’s cells. This makes the row ready for
// new content to be inserted into.
foreach (Cell cell in clonedRow.Cells)
     cell.RemoveAllChildren();
// Add the row to the end of the table.
table.AppendChild(clonedRow);

I hope, this helps.

Best regards,