Hi Joao,
Thanks for this additional information.
I have taken a look into this and was able to reproduce the problem. The name property of the BookmarkStart and BookmarkEnd nodes are read only so that they can’t be changed independently (and made invalid). Instead the Bookmark facade allows you to work with both nodes and would allow you to achieve this. i.e bookmarkStart.Bookmark.Name = “New Name” will work.
I had a play with this but ran into some unexpected behaviour from the facade class due to the presense of duplicate bookmarks in the Document. Instead I was able to correctly implement this by searching for the other bookmark node explicitly and then reinserting a new bookmark at the same place.
The code to achieve this is below, I have reworked some of your original code, you may want to change it to suit your needs.
Bookmark bm = doc2.Range.Bookmarks[“PARECER”];
for (int i = 1; i < n - 1; i++)
{
ArrayList nodes = NodeExtractor.ExtractContent(bm.BookmarkStart, bm.BookmarkEnd, true);
DuplicateContentInBookmark(doc2, nodes, bm, i);
}
private static void DuplicateContentInBookmark(Document doc, ArrayList nodes, Bookmark startbookmark, int id)
{
// Get the parent at the end of the bookmark
CompositeNode bookmarkEndNode = startbookmark.BookmarkEnd.ParentNode;
// Text to add to the end of any duplicate bookmarks found
string suffix = “_” + id;
// Find the block level node to start at
while (bookmarkEndNode.ParentNode.NodeType != NodeType.Body)
bookmarkEndNode = bookmarkEndNode.ParentNode;
DocumentBuilder builder = new DocumentBuilder(doc);
Node currentNode = bookmarkEndNode;
foreach (Node node in nodes)
{
// Search inside composite nodes in the duplicated content
if(node.IsComposite){
foreach (Node inlineNode in ((CompositeNode)node))
{
if (inlineNode.NodeType == NodeType.BookmarkStart)
{
BookmarkStart start = (BookmarkStart)inlineNode;
BookmarkEnd end = FindBookmarkEnd(nodes, start.Name);
// New name for the bookmark.
string name = start.Bookmark.Name + suffix;
// Insert the new starting bookmark
builder.MoveTo(start);
builder.StartBookmark(name);
// Insert the new ending bookmark
builder.MoveTo(end);
builder.EndBookmark(name);
// Remove the old ones as they are not needed anymore
start.Remove();
end.Remove();
}
}
}
currentNode = currentNode.ParentNode.InsertAfter(node, currentNode);
}
}
public static BookmarkEnd FindBookmarkEnd(ArrayList nodes, string name)
{
// Search through all nodes in the ArrayList, including child nodes.
foreach (Node node in nodes)
{
if (node.NodeType == NodeType.BookmarkEnd)
{
BookmarkEnd bookmark = (BookmarkEnd)node;
// Found the bookmark
if (bookmark.Name.Equals(name))
return (BookmarkEnd)node;
}
// If this node has child nodes, recurse down into these nodes to check them.
if(node.IsComposite){
// Wrap child nodes into a new array list and call this method again recursively.
BookmarkEnd result = FindBookmarkEnd(new ArrayList(((CompositeNode)node).ChildNodes.ToArray()), name);
if (result != null)
return result;
}
}
return null;
}
Regarding the gap inbetween duplicated content, this was not actually any empty paragraph, this was simply that the ending paragraph of the content actually has a very large space after set (16pt while the rest of the paragraphs had 3pt from memory). If you highlight the last paragraph in your original document and remove this large spacing then it’s fine.
Also please note the current way you are inserting the duplicated content into your document means that the ordering of inserted blocks is reversed in a way, so that the most recent duplicated content is being inserted near the top of the document. You may want to look into this if it’s not expected, I am glad to help if you have any problems.
If you have any further queries, please feel free to ask.
Thanks,