Frage

I have read everything I can find that is even remotely related to this (including Read Word bookmarks), but have not been able to get anything to work.

I am trying to walk through a Word document that has bookmarks in it, and get the values for each of the bookmarks. I can walk the document and get the names of the bookmarks, but cannot figure out how to get the value/text of the bookmark.

Here is what I am using to get the bookmark names:

using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(athleteFile, false))
{
    foreach (BookmarkStart bookmark in wordDocument.MainDocumentPart.Document.Body.Descendants<BookmarkStart>())
    {       
        System.Diagnostics.Debug.WriteLine(bookmark.Name + " - " + bookmark.InnerText);
    }
}
War es hilfreich?

Lösung

First of all I'd highly recommend you use the Open XML SDK 2.5 Productivity tool, that way you'll have a better idea of what you're working with.

Secondly a bookmark in Word does not have any value associated with it. It is usually marks a location in the word document. So what you're trying to do wont work.

<w:bookmarkStart w:name="bkStart" w:id="0" />

that is the XML element that is created in the docx file when you add a bookmark to the document.

Andere Tipps

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;
        }
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top