Add row and cell to existing table

Here is my existing table and i would like to add new row dynamically with C#.
Can you help me?

Her is my table

num hhh money
Sum «Sum»
Dato: «Datetime»

Hi,

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);

Hope, this helps.

Best regards,

What if i want to start from 3 row?
can i do that??

Hi,

Thanks for your inquiry. Sure, you can achieve this by using the following code:

Document doc = new Document(filePath);
// Retrieve the first table in the document.
Table table = (Table)doc.GetChild(NodeType.Table, 1, 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();
    cell.EnsureMinimum();
    cell.FirstParagraph.Runs.Add(new Run(doc, "Row"));
}
// Add the row after any row of the table.
table.Rows[2].ParentNode.InsertAfter(clonedRow, table.Rows[2]);
doc.Save(MyDir + @"16.4.0.docx");

Hope, this helps.

Best regards,

I have problems when inserting several rows into the same table, I get the error Cannot add a node before/after itself. When inserting a third row, I have my code as follows, could you please support me?

Table table = (Table)docWord.GetChildNodes(NodeType.Table, true).Last();
Row clonedRow = (Row)table.LastRow.Clone(true);

foreach (var itemOfList in list){
    table.Rows[2].ParentNode.InsertAfter(clonedRow, table.Rows[2]);
}

When running the foreach on the first and second item it adds the columns without problem but when doing the third it shows the error that I mentioned

@jesus_r You should clone the row for each iteration. Please modify your code like this:

Table table = (Table)docWord.GetChildNodes(NodeType.Table, true).Last();
foreach (var itemOfList in list)
{
    table.Rows[2].ParentNode.InsertAfter((Row)table.LastRow.Clone(true), table.Rows[2]);
}

thanks Alexey

i can resolve the problem with

 Row emptyRow = (Row)clonedRow.Clone(true);
 table.Rows.Add(emptyRow);

is it the same?

@jesus_r Yes, this is the same.

thanks a lot Alexey

Sorry I have the doubt, how can I insert an image in the document, in what format or type do I need the image to place it base64, byte[] ?

I think I don’t have the image in some directory, they send it to me in base64 or byte[]

@jesus_r You can use any source for inserting images into the document. Please see our documentation to learn more about working with images:
https://docs.aspose.com/words/net/working-with-images/

In your case you can write your base64 or byte[] to stream and then insert an image from stream.

Thanks Alexey,
How can I copy the entire page and paste it into a new sheet within the same document?
Depending on a number that they tell me, I’m going to repeat a table that’s in my document How can I do it? i think that i could do

for (int i = 0; i < 3; i++)
{
    Table[] table = (Table)docWord.GetChildNodes(NodeType.Table, true).Last();
}

How do I pull or arrange original tables from a single existing element in my document
i want to clone a table many

NodeCollection tables;

for (int c = 0; c < 3; c++)
{
    tables[c] = (Table)docWord.GetChildNodes(NodeType.Table, true).Last();
}

@jesus_r You can use Node.Clone method to create a copy of a node and then use CompositeNode.InsertBefore or Compositenode.InsertAfter method to insert the cloned node into the document.

There is no direct way to get pages from MS Word document, since MS Word document are flow by their nature and there is no “page” concept. The consumer application reflows the document into pages on the fly. However, Aspose.Words provides a method for extracting page range from the document Document.ExtractPages. Adter extracting you can use Document.AppendDocument or DocumentBuilder.InsertDocument methods to insert the extracted pages into the document.