Get Text within a Bookmark in C#, VB.NET

Spire.Doc supports to retrieve, replace and delete bookmark content of a specified bookmark. This article will show you how we can get the plain text within a bookmark by using Spire.Doc with C# and VB.NET.

Step 1: Create a Document instance, and load a sample Word document.

Document doc = new Document();
doc.LoadFromFile("Bookmark.docx");

Step 2: Creates a BookmarkNavigator instance to access the bookmark.

BookmarksNavigator navigator = new BookmarksNavigator(doc);

Step 3: Locate a specific bookmark by bookmark name. Call the method GetBookmarkContent to get content within the bookmark.

navigator.MoveToBookmark("bookmark_1");
TextBodyPart textBodyPart = navigator.GetBookmarkContent();

Step 4: Iterate through the items in the bookmark content to get the plain, unformatted text of the bookmark.

string text = null;
foreach (var item in textBodyPart.BodyItems)
{
    if (item is Paragraph)
    {
        foreach (var childObject in (item as Paragraph).ChildObjects)
        {
            if (childObject is TextRange)
            {
                text += (childObject as TextRange).Text;
            }
        }
    }
}

Result:

Get Text within a Bookmark in C#, VB.NET

Full Code:

[C#]
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;
namespace GetText
{
    class Program
    {

        static void Main(string[] args)
        {

            Document doc = new Document();
            doc.LoadFromFile("Bookmark.docx");

            BookmarksNavigator navigator = new BookmarksNavigator(doc);
            navigator.MoveToBookmark("bookmark_1");
            TextBodyPart textBodyPart = navigator.GetBookmarkContent();

            string text = null;
            foreach (var item in textBodyPart.BodyItems)
            {
                if (item is Paragraph)
                {
                    foreach (var childObject in (item as Paragraph).ChildObjects)
                    {
                        if (childObject is TextRange)
                        {
                            text += (childObject as TextRange).Text;
                        }
                    }
                }
            }
            Console.WriteLine(text);
        }
    }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Namespace GetText
	Class Program

		Private Shared Sub Main(args As String())

			Dim doc As New Document()
			doc.LoadFromFile("Bookmark.docx")

			Dim navigator As New BookmarksNavigator(doc)
			navigator.MoveToBookmark("bookmark_1")
			Dim textBodyPart As TextBodyPart = navigator.GetBookmarkContent()

			Dim text As String = Nothing
			For Each item As var In textBodyPart.BodyItems
				If TypeOf item Is Paragraph Then
					For Each childObject As var In TryCast(item, Paragraph).ChildObjects
						If TypeOf childObject Is TextRange Then
							text += TryCast(childObject, TextRange).Text
						End If
					Next
				End If
			Next
			Console.WriteLine(text)
		End Sub
	End Class
End Namespace