1

Can any tell me how to real all bookmarks in a word 2010 document using openXml 2.0. I was using Microsoft.Office.Interop.Word to read bookmarks but i am not able to deploy my website as it was having issues so i switched to openxml can anyone tell me how to read all bookmarks

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Owais Ahmed
  • 1,364
  • 1
  • 30
  • 62
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jan 05 '14 at 02:38

2 Answers2

1

you can iterate through all

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

like:

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;
  }
}

code extracted from https://stackoverflow.com/a/3318381/28004

Community
  • 1
  • 1
balexandre
  • 73,608
  • 45
  • 233
  • 342
0

Try this.I have used same in my project

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

MaheshMajeti
  • 187
  • 12