Bookmarks and Hyperlinks

A Bookmark is an object used to record a location in a Word document. It's defined with the BookmarkStart and BookmarkEnd pair of elements.

Hyperlink is a document element used to jump to a Bookmark in the same document or to an external resource. It consists of two parts, an Address and some Display content.

The following example shows how you can insert a bookmark into a document and a hyperlink that links to that bookmark.

Word document with Bookmark and Hyperlink elements
Screenshot of Word file with bookmarks and hyperlinks
using GemBox.Document;

class Program
    static void Main()
        // If using Professional version, put your serial key below.

        var document = new DocumentModel();

        var bookmarkName = "TopOfDocument";

            new Section(document,
                new Paragraph(document,
                    new BookmarkStart(document, bookmarkName),
                    new Run(document, "This is a 'TopOfDocument' bookmark."),
                    new BookmarkEnd(document, bookmarkName)),
                new Paragraph(document,
                    new Run(document, "The following is a link to "),
                    new Hyperlink(document, "", "GemBox.Document Overview"),
                    new Run(document, " page.")),
                 new Paragraph(document,
                    new SpecialCharacter(document, SpecialCharacterType.PageBreak),
                    new Run(document, "This is a document's second page."),
                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
                    new Hyperlink(document, bookmarkName, "Return to 'TopOfDocument'.") { IsBookmarkLink = true })));

        document.Save("Bookmarks and Hyperlinks.%OutputFileType%");
Imports GemBox.Document

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.

        Dim document As New DocumentModel()

        Dim bookmarkName = "TopOfDocument"

            New Section(document,
                New Paragraph(document,
                    New BookmarkStart(document, bookmarkName),
                    New Run(document, "This is a 'TopOfDocument' bookmark."),
                    New BookmarkEnd(document, bookmarkName)),
                New Paragraph(document,
                    New Run(document, "The following is a link to "),
                    New Hyperlink(document, "", "GemBox.Document Overview"),
                    New Run(document, " page.")),
                 New Paragraph(document,
                    New SpecialCharacter(document, SpecialCharacterType.PageBreak),
                    New Run(document, "This is a document's second page."),
                    New SpecialCharacter(document, SpecialCharacterType.LineBreak),
                    New Hyperlink(document, bookmarkName, "Return to 'TopOfDocument'.") With {.IsBookmarkLink = True})))

        document.Save("Bookmarks and Hyperlinks.%OutputFileType%")

    End Sub
End Module

You can use the DocumentModel.Bookmarks collection to iterate through all bookmarks in the document, or to remove any bookmark from the document. You can also use it to retrieve a single Bookmark by specifying the bookmark's name.

Bookmarks are often used as placeholders where some data should be imported. You can check our Modify Bookmarks example to learn more about how to use bookmarks this way.

When converting a Word document to PDF file, you can specify if the document's bookmarks should be exported as PDF outlines using the PdfSaveOptions.BookmarksCreateOptions property.

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.

Like it?

Download Buy

If you want to try the GemBox.Document yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.