Owais Ahmed
Owais Ahmed

Reputation: 1438

Read Word bookmarks

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

Upvotes: 1

Views: 2618

Answers (2)

MaheshMajeti
MaheshMajeti

Reputation: 187

Try this.I have used same in my project

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

Upvotes: 0

balexandre
balexandre

Reputation: 75103

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

Upvotes: 1

Related Questions