Bookmarks and Hyperlinks

Bookmark is an object used to record a location in a Word document. It's defined with 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 a Display content.

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

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

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        var document = new DocumentModel();

        var bookmarkName = "TopOfDocument";

        document.Sections.Add(
            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, "https://www.gemboxsoftware.com/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.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim document As New DocumentModel()

        Dim bookmarkName = "TopOfDocument"

        document.Sections.Add(
            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, "https://www.gemboxsoftware.com/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 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 in such a way.

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

Check next example or download examples from GitHub.