Bookmarks and Hyperlinks

A Bookmark is an object used to record a location in a Word document. You can define a bookmark programmatically with the BookmarkStart and BookmarkEnd pair of elements.

A 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 GemBox.DocumentGemBox.Document component allows you to add both Bookmarks and Hyperlinks to your documents programmatically in C# and VB.NET

The following example shows how you can insert a bookmark to 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.
        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 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.

Use Bookmarks as placeholders where you can import data. 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.

See also


Next steps

GemBox.Document is a .NET component that enables you to read, write, edit, convert, and print document files from your .NET applications using one simple API. How about testing it today?

Download Buy