I'm using the following code to iterate through all of the bookmarks in a Microsoft Word document:
foreach (var bookmark in _document.Range.Bookmarks.Cast<Bookmark>())
{
//code
}
How can I determine if bookmark
contains nested bookmarks? I need to execute a separate set of logic based on whether or not a bookmark has other bookmarks within it.
The above foreach loop will get all the instances of Bookmark. To get the nested bookmark is a bit tricky, as there is no direct API in Aspose.Words to do this.
I have written a program to do this, the source code is not tiny, so I am sharing the Visual Studio project on Google Drive here .
Below is the summary
foreach (var bookmark in wordDoc.Range.Bookmarks.Cast<Aspose.Words.Bookmark>())
{
Console.WriteLine(bookmark.Name);
// Get all the nodes between bookmark start and end
ArrayList extractedNodes = ExtractContent(bookmark.BookmarkStart, bookmark.BookmarkEnd, true);
for (int i = 0; i < extractedNodes.Count; i++)
{
// Skip first and last nodes
if (i == 0 || i == extractedNodes.Count - 1)
continue;
// See if there is any bookmarks in this node
Node node = (Node)extractedNodes[i];
if (node.Range.Bookmarks.Count > 0)
Console.WriteLine("Nested bookmark found");
}
}
For details of ExtractContent() method, please visit http://www.aspose.com/docs/display/wordsnet/Extract+Content+Overview+and+Code
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.