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