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

Free Support Forum - aspose.com

Copy table row and add bookmarks to cells in new row

I have template dot files that have tables with one template row. Each cell in this row has a bookmark. I need to clone this row, for each table, and extract the bookmark names from the cells in the first row, rename them, add them to the cells of the cloned row, and then add this new row to the table. I have been experimenting with various techniques and browsing exmples in your forum and documentation but haven’t found an example. I’m using the document nodes collection to find the tables and add the rows, but it appears that the only way to write a bookmark is through a DocumentBuilder but I’m using rows and cells in memory. What I really need is a way to select a row, read the names of the bookmarks from that row and append a “_1” to each then insert these bookmarks into each cell for the cloned row. A New Bookmark object that could be added directly to the cell would be perfect.
Another similar task I need to do is to clone a part of a document (like a range), iterate through the bookmarks, rename them, and then insert them back into the document, although I’ll settle for just figuring out how to do the problem described in the first paragraph for now.

Thanks for your request. You can try using the following code.

// Open document
Document doc = new Document(@"Test152\in.doc");
// Get table from document
Table tab = doc.FirstSection.Body.Tables[0];
// Clone last row 10 times
for (int i = 0; i < 10; i++)
    Node newRow = tab.LastRow.Clone(true);
// Rwname bookamrks in the table
int bookmarkIndex = 0;
foreach (Row row in tab.Rows)
    foreach (Bookmark book in row.Range.Bookmarks)
        book.Name = book.Name + bookmarkIndex.ToString();
// Save output document

Also here you can find example how to rename bookmarks during mail merge.
Best regards.