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.

using GemBox.Document;
using GemBox.Document.Tables;
class Program
{
static void Main()
{
// If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
var document = DocumentModel.Load("%#BookmarksTemplate.docx%");
// 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 the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY")
Dim document = DocumentModel.Load("%#BookmarksTemplate.docx%")
' 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.