Hi,
We are currently trying the Evalution version of ASPOSE.Words. We want to read the Word document which acts as a template, and find the Table. On that table, we have 2 rows, one is Header and other one is content. Now we want to copy the content row and append it multiple times to the table depending upon the Run-time logic. Now this modified word document will be saved with some different file name. So the template and the modified document can be distinguished.
Kindly provide us help/code to implement the scenario using ASPOSE.Words API as soon as possible.
We are currently testing this using ASPOSE product for our client. The client would like to purchase the ASPOSE product, if the scenarios we test, succeed.
Thanks,
Sandip
Hi
Thanks for your inquiry. I think the best way to achieve this is using mail merge with regions. But this requires you to insert merge fields into your template. Please follow the links to learn how to prepare template and learn more about mail merge with regions:
https://docs.aspose.com/words/net/mail-merge-template/
https://docs.aspose.com/words/net/types-of-mail-merge-operations/
In additional, you can just copy/paste an existing row (as you mentioned), but I think it is not very good approach. Anyway, here is code, which shows how to achieve this:
// 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
myTable.AppendChild(newRow);
// Loop through all cells in row
foreach (Cell cell in newRow)
{
// Remove old content from the cell
// Remove paragraphs
while (cell.Paragraphs.Count > 1)
{
cell.LastParagraph.Remove();
}
// Remove content of first paragraph
cell.FirstParagraph.ChildNodes.Clear();
// Move DocumentBuilder cursor to the cell
builder.MoveTo(cell.FirstParagraph);
// Insert new content
builder.Write("This is new content");
}
}
// Save document
doc.Save(@"Test297\out.doc");
Hope this helps. Please let me know if you need more assistance.
Best regards.