Modify Word Bookmarks in C# and VB.NET

Bookmark is used to mark a location in a Word document which can be identified and retrieved by knowing its Bookmark.Name. As such they're often used as named placeholders where some data should be imported.

With the Bookmark.GetContent method you can retrieve the bookmark's content as a ContentRange object which you can use to delete or replace that content.

Note, if your require modifying or replacing the bookmark's content in a repeated way, like inserting a dynamic number of table rows, than a better option would be to use Mail Merge, a powerful and customizable process of merging or importing data from a data source to a template document.

The following example shows how you can insert some data into the bookmarks by replacing their content with text, table and image.

Word document with data inserted in bookmarks
Screenshot of Word file with modified bookmarks
Upload your file (Drag file here)
using GemBox.Document;
using GemBox.Document.Tables;

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

        var document = DocumentModel.Load("%InputFileName%");

        // Replace bookmark's content with plain text.
        document.Bookmarks["Company"].GetContent(false).LoadText("Acme Corporation");
        
        // Replace bookmark's content with HTML text.
        document.Bookmarks["Address"].GetContent(false).LoadText(
            "<span style='font: italic 8pt Calibri; color: red;'>240 Old Country Road, Springfield, IL</span>",
            LoadOptions.HtmlDefault);

        // Insert hyperlink into a bookmark.
        var hyperlink = new Hyperlink(document, "mailto:joe.doe@acme.co", "joe.doe@acme.co");
        document.Bookmarks["Contact"].GetContent(false).Set(hyperlink.Content);

        // Insert image into a bookmark.
        var picture = new Picture(document, "%#Acme.png%");
        document.Bookmarks["Logo"].GetContent(false).Set(picture.Content);

        // Insert text and table into a bookmark.
        ContentRange itemsRange = document.Bookmarks["Items"].GetContent(false);
        itemsRange = itemsRange.LoadText("Storage:");
        var table = new Table(document, 6, 3, (r, c) => new TableCell(document, new Paragraph(document, $"Item {r}-{c}")));
        itemsRange.End.InsertRange(table.Content);

        document.Save("Modified Bookmarks.%OutputFileType%");
    }
}
Imports GemBox.Document
Imports GemBox.Document.Tables

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim document = DocumentModel.Load("%InputFileName%")

        ' Replace bookmark's content with plain text.
        document.Bookmarks("Company").GetContent(False).LoadText("Acme Corporation")

        ' Replace bookmark's content with HTML text.
        document.Bookmarks("Address").GetContent(False).LoadText(
            "<span style='font: italic 8pt Calibri; color: red;'>240 Old Country Road, Springfield, IL</span>",
            LoadOptions.HtmlDefault)

        ' Insert hyperlink into a bookmark.
        Dim hyperlink As New Hyperlink(document, "mailto:joe.doe@acme.co", "joe.doe@acme.co")
        document.Bookmarks("Contact").GetContent(False).Set(hyperlink.Content)

        ' Insert image into a bookmark.
        Dim picture As New Picture(document, "%#Acme.png%")
        document.Bookmarks("Logo").GetContent(False).Set(picture.Content)

        ' Insert text and table into a bookmark.
        Dim itemsRange As ContentRange = document.Bookmarks("Items").GetContent(False)
        itemsRange = itemsRange.LoadText("Storage:")
        Dim table As New Table(document, 6, 3, Function(r, c) New TableCell(document, New Paragraph(document, $"Item {r}-{c}")))
        itemsRange.End.InsertRange(table.Content)

        document.Save("Modified Bookmarks.%OutputFileType%")

    End Sub
End Module

Besides modifying the bookmark's content, you can also retrieve the bookmark's text using the ContentRange.ToString method, or retrieve other elements in the bookmark, like tables and images, using the ContentRange.GetChildElements method.

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.