In Aspose.Words, use the class
Bookmark to work with a single bookmark ant the BookmarkCollection class to work with a collection of bookmark objects.
The following code example shows how to get and set bookmark text in a document:
You can also do the same using the Open XML SDK. At the same time, note that it looks somewhat more complicated and more cumbersome.
Below is the code example for getting and setting Bookmark text in word document using OpenXML SDK.
Following are the namespaces we need to add:
using System.Collections.Generic;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using NUnit.Framework;
The following code example shows how to get and set bookmark text in a document:
public void GetAndSetBookmarkTextFeature()
{
IDictionary<string, BookmarkStart> bookmarkMap = new Dictionary<string, BookmarkStart>();
using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(MyDir + "Get and set bookmark text.docx", true))
{
foreach (BookmarkStart bookmarkStart in wordDocument.MainDocumentPart.Document.Body.Descendants<BookmarkStart>())
{
bookmarkMap[bookmarkStart.Name] = bookmarkStart;
foreach (BookmarkStart bookmark in bookmarkMap.Values)
{
Run bookmarkText = bookmark.NextSibling<Run>()
if (bookmarkText != null)
bookmarkText.GetFirstChild<Text>().Text = "Test";
}
}
}
}