Bookmark Hell!

We are developing a .net Web Application that produces letters using the Aspose.Words component.
The letter consists of a lot of text marked out with bookmarks which we then remove or leave in the document.
To remove them we use the following methods.

public void DeleteRangeOfBookmark(string FieldName)
{
    try
    {
        Aspose.Words.Bookmark bm = doc.Document.Range.Bookmarks[FieldName];
        if (!(bm == null))
        {
            bm.Text = "";
            bm.Remove();
        }
    }
    catch
    {
        // do nothing
    }
}
public void DeleteParagraphOfBookmark(string FieldName)
{
    try
    {
        Aspose.Words.Bookmark bm = doc.Document.Range.Bookmarks[FieldName];
        if (!(bm == null))
        {
            bm.Text = "";
            builder.MoveToBookmark(FieldName, true, true);
            builder.CurrentParagraph.Remove();
        }
    }
    catch
    {
        // do nothing
    }
}

What we have found is that sometimes the DeleteRangeOfBookmark works okay, but sometimes we
need to use the DeleteParagraphOfBookmark to successfully remove the text.

Also we have bookmarks in numbered lists ie. each numbered item is a bookmarked and in certain conditions
we need to remove these, with the numbering staying intact.
We have the same problem in that it is trial and error which method we should be calling.

Is there any code that works 100% to remove bookmarks and the text associated with it as we are
struggling to find anything that works consistently.

Also when we define the bookmarks does it matter if they incorpororate the end of line paragraph mark or should
this come after the end bookmark.

If you need any more info just ask.

Looking forward to a 100% consistent solution.

Thanks

Good question.

Welcome to the DOM-like approach working with a Word document. The document in Aspose.Words is a tree of nodes pretty much the same way as you would get it if you load a WordML document into XML DOM. It can indeed be very tough to work with bookmarks, not to mention a few other things like find and replace text, insert content of another document etc etc exactly because of the tree-like nature of the model.

Aspose.Words object model is consistent with itself in the way it presents nodes including bookmark nodes. The reason it could be hard to work with bookmarks because you are essentially trying to perform “flat” text operations against a “jagged tree fragment” formed by bookmarks in your document.

BookmarkStart and BookmarkEnd are nodes that you encounter in the tree, they form the actual bookmark. All nodes in between constitute the text of the bookmark. As you can imagine BookmarkStart and BookmarkEnd can be pretty much anywhere in the tree, for example one inside a paragraph and another outside of a paragraph.

This makes the job of deleting or replacing content of a boomark tough because you have to slice and splice the nodes in very fancy ways.

Some of this gruesome work is hidden behind a “facade” class Bookmark. It tries to encapsulate the disparate bookmark start and end nodes as well as providing some “flat” functions that you can do against the content of the bookmark, such as Text set and get. But the Bookmark class is not yet that powerful to handle all possible scenarios of “jaggedness” of the tree fragments that can framed by bookmark start and end nodes.

So the 100% solution for your problem is to open your documents in our DocumentExplorer application (shipped as a demo project with Aspose.Words installer). In DocumentExplorer study the tree of nodes. Watch and notice where the bookmarks are. Read the description of the tree model and node classes in the Programmers Guide https://docs.aspose.com/words/net/

Then use just simple tree node functions to move between nodes, add, remove nodes to your hearts content. Basically you cannot do everything using “flat text” functions (“range-like” should I say), but you can do anything using the “tree-like” functions.

It is interesting to note that the tree-like interface works very well for some tasks, but not for all. The same can be said about the range-like interface - it works best of other tasks, but again not for all. At the moment, Aspose.Words public interfaces are more tree-like with bits of range-like here and there. This means that some tasks that are best done using range-like interface could be hard to do. In the future we will improve our range-like interfaces to make these tasks easier.

Thanks for the response. I have used the Document Explorer and can see that the BookmarkStart and BookmarkEnd nodes are spanning paragarphs.

I guess this is why I am having issues when deleting the paragraph that contains the bookmark as it sometimes deletes both paragraphs (i.e the paragraph with the start node and the paragraph with the end node). I have fixed this by ensuring that the bookmark set in the Word document falls before the Paragraph Break. This seems to have resolved the issue for the moment.

Is it best practice to define bookmarks so that they don’t contain the Paragraph break when dealing with numbered lists etc…