阅读单词书签



谁能告诉我如何使用openXml 2.0真实Word 2010文档中的所有书签。我正在使用Microsoft.Office.Interop.Word来读取书签,但我无法部署我的网站,因为它有问题,所以我切换到openxml,谁能告诉我如何阅读所有书签

您可以遍历所有

file.MainDocumentPart.RootElement.Descendants<BookmarkStart>()

喜欢:

IDictionary<String, BookmarkStart> bookmarkMap = 
     new Dictionary<String, BookmarkStart>();
// get all
foreach (BookmarkStart bookmarkStart in file.MainDocumentPart.RootElement.Descendants<BookmarkStart>())
{
    bookmarkMap[bookmarkStart.Name] = bookmarkStart;
}
// get their text
foreach (BookmarkStart bookmarkStart in bookmarkMap.Values)
{
  Run bookmarkText = bookmarkStart.NextSibling<Run>();
  if (bookmarkText != null)
  {
      string bookmarkText = bookmarkText.GetFirstChild<Text>().Text;
  }
}

从 https://stackoverflow.com/a/3318381/28004 中提取的代码

试试这个。我在我的项目中使用了相同的内容

http://www.legalcube.de/post/Word-openxml-sdk-bookmark-handling.aspx

最新更新