Bookmark Class

Represents a single bookmark.
Inheritance Hierarchy

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.2
public class Bookmark

The Bookmark type exposes the following members.

Public propertyCode exampleBookmarkEnd
Gets the node that represents the end of the bookmark.
Public propertyCode exampleBookmarkStart
Gets the node that represents the start of the bookmark.
Public propertyCode exampleFirstColumn
Gets the zero-based index of the first column of the table column range associated with the bookmark.
Public propertyCode exampleIsColumn
Returns true if this bookmark is a table column bookmark.
Public propertyCode exampleLastColumn
Gets the zero-based index of the last column of the table column range associated with the bookmark.
Public propertyCode exampleName
Gets or sets the name of the bookmark.
Public propertyCode exampleText
Gets or sets the text enclosed in the bookmark.
Public methodEquals (Inherited from Object.)
Protected methodFinalize (Inherited from Object.)
Public methodGetHashCode (Inherited from Object.)
Public methodGetType (Inherited from Object.)
Protected methodMemberwiseClone (Inherited from Object.)
Public methodCode exampleRemove
Removes the bookmark from the document. Does not remove text inside the bookmark.
Public methodToString (Inherited from Object.)

Bookmark is a "facade" object that encapsulates two nodes BookmarkStart and BookmarkEnd in a document tree and allows to work with a bookmark as a single object.

Shows how to add bookmarks and update their contents.
public void CreateUpdateAndPrintBookmarks()
    // Create a document with 3 bookmarks: "MyBookmark 1", "MyBookmark 2", "MyBookmark 3"
    Document doc = CreateDocumentWithBookmarks();
    BookmarkCollection bookmarks = doc.Range.Bookmarks;

    // Check that we have 3 bookmarks
    Assert.AreEqual(3, bookmarks.Count);

    // Look at initial values of our bookmarks

    // Obtain bookmarks from a bookmark collection by index/name and update their values
    bookmarks[0].Name = "Updated name of " + bookmarks[0].Name;
    bookmarks["MyBookmark 2"].Text = "Updated text content of " + bookmarks[1].Name;
    // Remove the latest bookmark
    // The bookmarked text is not deleted

    bookmarks = doc.Range.Bookmarks;
    // Check that we have 2 bookmarks after the latest bookmark was deleted
    Assert.AreEqual(2, bookmarks.Count);

    // Look at updated values of our bookmarks

/// <summary>
/// Create a document with bookmarks using the start and end nodes.
/// </summary>
private static Document CreateDocumentWithBookmarks()
    DocumentBuilder builder = new DocumentBuilder();
    Document doc = builder.Document;

    // An empty document has just one empty paragraph by default
    Paragraph p = doc.FirstSection.Body.FirstParagraph;

    // Add several bookmarks to the document
    for (int i = 1; i <= 3; i++)
        string bookmarkName = "MyBookmark " + i;

        p.AppendChild(new Run(doc, "Text before bookmark."));

        p.AppendChild(new BookmarkStart(doc, bookmarkName));
        p.AppendChild(new Run(doc, "Text content of " + bookmarkName));
        p.AppendChild(new BookmarkEnd(doc, bookmarkName));

        p.AppendChild(new Run(doc, "Text after bookmark.\r\n"));

    return builder.Document;

/// <summary>
/// Use an iterator and a visitor to print info of every bookmark from within a document.
/// </summary>
private static void PrintAllBookmarkInfo(BookmarkCollection bookmarks)
    // Create a DocumentVisitor
    BookmarkInfoPrinter bookmarkVisitor = new BookmarkInfoPrinter();

    // Get the enumerator from the document's BookmarkCollection and iterate over the bookmarks
    using (IEnumerator<Bookmark> enumerator = bookmarks.GetEnumerator())
        while (enumerator.MoveNext())
            Bookmark currentBookmark = enumerator.Current;

            // Accept our DocumentVisitor it to print information about our bookmarks
            if (currentBookmark != null)

                // Prints a blank line

/// <summary>
/// Visitor that prints bookmark information to the console.
/// </summary>
public class BookmarkInfoPrinter : DocumentVisitor
    public override VisitorAction VisitBookmarkStart(BookmarkStart bookmarkStart)
        Console.WriteLine("BookmarkStart name: \"{0}\", Content: \"{1}\"", bookmarkStart.Name,
        return VisitorAction.Continue;

    public override VisitorAction VisitBookmarkEnd(BookmarkEnd bookmarkEnd)
        Console.WriteLine("BookmarkEnd name: \"{0}\"", bookmarkEnd.Name);
        return VisitorAction.Continue;
See Also