![](/img/trans.png)
[英]How to insert bookmarks and bookmark values from the table in the existing template repeatedly (in loop) using Aspose.Words in.Net
[英]How to determine if Aspose.Words bookmark contains nested bookmarks
我正在使用以下代碼遍歷Microsoft Word文檔中的所有書簽:
foreach (var bookmark in _document.Range.Bookmarks.Cast<Bookmark>())
{
//code
}
如何確定bookmark
包含嵌套書簽? 我需要根據書簽中是否包含其他書簽來執行一組單獨的邏輯。
上面的foreach循環將獲取Bookmark的所有實例。 獲取嵌套書簽有點棘手,因為Aspose.Words中沒有直接的API可以做到這一點。
我已經編寫了一個程序來執行此操作,源代碼不是很小,所以我在這里在Google雲端硬盤上共享Visual Studio項目。
以下是摘要
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");
}
}
有關ExtractContent()方法的詳細信息,請訪問http://www.aspose.com/docs/display/wordsnet/Extract+Content+Overview+and+Code
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.