Modify Word Bookmarks in C# and VB.NET

Bookmarks are used to define placeholders in the document where data should be imported.

GemBox.Document provides Bookmark.GetContent(System.Boolean) method to manipulate the content of a bookmark. Not only text, but any content can be set in / replace a bookmark as shown in the following sample.

If importation process has to be repeated, a better option would be to use Mail Merge - a process of merging or importing data from a data source to a template document.

Screenshot

Modify Bookmarks Screenshot

See the full code below, use RUN EXAMPLE to execute.

Upload your file (Drag files here)

1using System;
2using System.IO;
3using System.Text;
4using GemBox.Document;
5
6class Sample
7{
8    [STAThread]
9    static void Main(string[] args)
10    {
11        // If using Professional version, put your serial key below.
12        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
13
14        DocumentModel document = DocumentModel.Load("BookmarksTemplate.docx");
15
16        string pathToResources = "Resources";
17
18        document.Bookmarks["CompanyName"].GetContent(false).LoadText("ACME Corp");
19        document.Bookmarks["CompanyAddress"].GetContent(false).LoadText("240 Old Country Road, Springfield, IL");
20        document.Bookmarks["Country"].GetContent(false).LoadText("USA");
21        document.Bookmarks["ContactPerson"].GetContent(false).LoadText("Joe Smith");
22
23        document.Bookmarks["Text"].GetContent(false).LoadText(
24            "GemBox.Document is a .NET component that enables developers to read, write, convert and print document files (DOCX, DOC, PDF, HTML, XPS, RTF and TXT) from .NET applications in a simple and efficient way.",
25            new CharacterFormat() { Size = 14 });
26
27        Picture picture = new Picture(document, Path.Combine(pathToResources, "Acme.png"));
28        document.Bookmarks["Logo"].GetContent(false).Set(picture.Content);
29
30        document.Save("Modify Bookmarks.docx");
31    }
32}
1Imports System
2Imports System.IO
3Imports System.Text
4Imports GemBox.Document
5
6Module Samples
7
8    Sub Main()
9
10        ' If using Professional version, put your serial key below.
11        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
12
13        Dim document As DocumentModel = DocumentModel.Load("BookmarksTemplate.docx")
14
15        Dim pathToResources As String = "Resources"
16
17        document.Bookmarks("CompanyName").GetContent(False).LoadText("ACME Corp")
18        document.Bookmarks("CompanyAddress").GetContent(False).LoadText("240 Old Country Road, Springfield, IL")
19        document.Bookmarks("Country").GetContent(False).LoadText("USA")
20        document.Bookmarks("ContactPerson").GetContent(False).LoadText("Joe Smith")
21
22        document.Bookmarks("Text").GetContent(False).LoadText(
23            "GemBox.Document is a .NET component that enables developers to read, write, convert and print document files (DOCX, DOC, PDF, HTML, XPS, RTF and TXT) from .NET applications in a simple and efficient way.",
24            New CharacterFormat() With {.Size = 14})
25
26        Dim picture = New Picture(document, Path.Combine(pathToResources, "Acme.png"))
27        document.Bookmarks("Logo").GetContent(False).Set(picture.Content)
28
29        document.Save("Modify Bookmarks.docx")
30
31    End Sub
32
33End Module

Check next sample or find out more about GemBox.Document and GemBox Software.