Solution 1:
Get the bookmarks text by accessing its parent's inner text:
using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(athleteFile, false))
{
foreach (BookmarkStart bookmark in wordDocument.MainDocumentPart.Document.Body.Descendants<BookmarkStart>())
{
// Get name of bookmark
string bookmarkNameOriginal = bookmark.Name;
// Get bookmark text from parent elements text
string bookmarkText = bookmark.Parent.InnerText;
}
}
Solution 2:
I have found another solution using DocX by Xceed.
Note:
Reading bookmarks is slow in free version (v1.3 Docx). However it is fixed in v1.4 of Docx (free version is slower to get this update).
Import DocX:
using Xceed.Words.NET;
Create method to read bookmark name and text:
/// <summary>
/// Read bookmark text/names in word document
/// </summary>
/// <param name="filePath"></param>
/// <remarks>
/// Uses free DocX by Xceed
/// </remarks>
public void ReadBookmarks(string filePath)
{
//Load document
using (DocX Document = DocX.Load(filePath))
{
//This is slow in free version (v1.3 Docx), is fixed in v1.4Docx (free version is slower to get this)
BookmarkCollection bookmarks = Document.Bookmarks;
//Iterate over bookmarks in document
foreach (Bookmark bookmark in bookmarks) {
//Name of bookmark
string bookmarkName = bookmark.Name;
//Text of bookmark, usually a word heading (1, 2, 3...)
string bookmarkText = bookmark.Paragraph.Text;
}
}
}