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

Free Support Forum - aspose.com

Cloning table rows

I am cloning table rows in a Word document that satisfy a specific criteria - e.g. the table row contains the text 'Row B'. After cloning the row a number of times I want to keep processing through the rest of the table rows. At the moment, after cloning, control seems to return to the row I originally cloned - i want to continue processing table rows after the newly added clone rows - otherwise I get an infinite number of clone rows. Any ideas how I might do this.

foreach (Row row in table.Rows)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

{

foreach (Cell cell in row.Cells)

{

NodeCollection cellCollection = cell.GetChildNodes(NodeType.Run, true);

foreach (Run run in cellCollection)

{

if (run.GetText().Contains("Row B"))

{

//clone row B 2 times

}

}

}

}

I have included a document showing before and after examples of the table I want. I am using Aspose.Words 8.0.0.0.

Hi
Andrew,


Thanks for your inquiry. I think, you can achieve what you need by using the following code snippet:

Document
doc = new Document(@“C:\test\AsposeRows.doc”);

Table
table = doc.FirstSection.Body.Tables[0];
for (int i = 0; i < table.Rows.Count; i++)
{
if
(table.Rows[i].Range.Text.Contains(
“Row
B”
))
{
// copy two
times
Row
cloneOfRow = (
Row)table.Rows[i].Clone(true);
cloneOfRow.Cells[0].Paragraphs[0].Runs[0].Font.Color = Color.Red;
table.Rows.Insert(++i, cloneOfRow);

cloneOfRow = (Row)table.Rows[i].Clone(true);
cloneOfRow.Cells[0].Paragraphs[0].Runs[0].Font.Color = Color.Red;
table.Rows.Insert(++i,
cloneOfRow);
}
}

doc.Save(@“C:\Test\out.doc”);

If we can help you with anything else, please feel free to ask.

Best Regards,

Thanks Awais - that worked perfectly.