How to insert a cloned table into a bookmark (or anywhere)?

I’m having problems positioning a cloned table where I want.

I’m experimenting with having a sort of reusable “table template” within a bookmark, as I can’t be bothered to create it manually (it has some complex layout).

This will be used as the basis when creating multiple new tables, inbetween new headings.

I’ve been able to get a clone of this “template” table, by using the sample “ExtractContent” method from https://docs.aspose.com/words/net/how-to-extract-selected-content-between-nodes-in-a-document/.

I’ve also been looking at this page:

https://docs.aspose.com/words/net/introduction-and-creating-tables/

But I’m unable to position my cloned table. It says “Cannot insert a node of this type at this location.”.

The code I’ve tried is the following:

bmk.BookmarkEnd.ParentNode.InsertAfter(table, bmk.BookmarkEnd);

The clone example above uses the original table’s “ParentNode” as the basis for inserting the new node (the table clone).

As this is not what I want in my case, I’m struggling to locate a suitable node to insert the table clone after.

The typical scenario will be (in pseudo-code):

  • Locate starting bookmark (where all my custom content will start being added)

  • Create a new level 2 heading

  • Insert a table clone (from the “template table”)

  • Create another level 2 heading

  • Insert another table clone

I typically use the DocumentBuilder to create these headings, but I can’t seem to be able to insert nodes through the builder (get the same error as above when trying to insert the table clone with the builder’s “InsertNode” method).

Also, I don’t get a reference to the newly added heading when using the builder, so can’t use that as the node for inserting the table clone either.

Sorry for not including any sample code at this point. I just wanted to see if there’s an obvious way to do this that I’m missing.

I’ve also tried to create a new empty paragraph to use as the starting node to insert the table clone after, but that’s also failing with the same error:

var bmk = doc.Range.Bookmarks["bookmark"];
builder.MoveToBookmark(bmk.Name, isStart: false, isAfter: true);
var para = new Paragraph(doc);
builder.InsertNode(para);
para.InsertAfter(table, para);

Now I’ve solved it, but not in an especially pretty way.

Additional requirement in my case:

Leave a bookmark around the cloned table, to enable further processing later (for reasons not relevant here).

It goes down like this:

  • Move to bookmark (with builder) (the overall bookmark where all new content is to be added)

  • [Start of loop]

** Add new heading 2 (with builder)

** Get a ref to builder’s current paragraph

** Get the cloned nodes from the bookmark containing the template (stating that it should be “inclusive”, so that bookmark itself is also returned)

** Write a line break to the builder (crucial part, to avoid mangling of contents)

** Insert the cloned nodes (paragraphs and table) into the doc, through the builder (crucial part is to nest these inserts after each other, starting with the previously gotten ref to current paragraph - “cursor” if you like)

  • [End of loop]

  • Cleanup the now duplicate bookmark names, by using the sample bookmark visitor class from a forum post (https://forum.aspose.com/t/59821)

  • Delete the bookmark with the template table

Workarounds, which otherwise lead to mangled results:

  • The bookmark containing the template table needs to start with a line break, not within the first cell of the table.

  • The master bookmark where everything is to end up needs to be separated from the template bookmark by a line break.

Due to the extra line break after the heading and the one at the start of the template bookmark, qute a lot of unwanted whitespace is left. Don’t know what to do with that.

Hi Morten,

Thanks for your inquiry. Please use following code example to insert the cloned table between two headings. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToBookmark("bookmark");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder.Writeln("Heading 2");
Paragraph para = (Paragraph)doc.Range.Bookmarks["bookmark"].BookmarkStart.ParentNode;
// Retrieve the first table in the document.
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);
// Create a clone of the table.
Table tableClone = (Table)table.Clone(true);
// Insert the cloned table into the document after the Heading 2
table.ParentNode.InsertAfter(tableClone, para);
table.ParentNode.InsertAfter(new Paragraph(doc), table);
builder.Writeln("Heading 2 after inserting cloned table");
// Save the document to disk.
doc.Save(MyDir + "Out.docx");

If you still face problem, please share following resources here for our reference.

  • Your input Word document which contains the table
  • Please attach your target Word document showing the desired behavior. You can use Microsoft Word to create your target Word document. We will investigate as to how you are expecting your final document be generated like.

PS: To attach these resources, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.