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.

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 example.

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)

Download a sample file

using System;
using System.IO;
using System.Text;
using GemBox.Document;

class Sample
{
    [STAThread]
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        DocumentModel document = DocumentModel.Load("BookmarksTemplate.docx");

        string pathToResources = "Resources";

        document.Bookmarks["CompanyName"].GetContent(false).LoadText("ACME Corp");
        document.Bookmarks["CompanyAddress"].GetContent(false).LoadText("240 Old Country Road, Springfield, IL");
        document.Bookmarks["Country"].GetContent(false).LoadText("USA");
        document.Bookmarks["ContactPerson"].GetContent(false).LoadText("Joe Smith");

        document.Bookmarks["Text"].GetContent(false).LoadText(
            "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.",
            new CharacterFormat() { Size = 14 });

        Picture picture = new Picture(document, Path.Combine(pathToResources, "Acme.png"));
        document.Bookmarks["Logo"].GetContent(false).Set(picture.Content);

        document.Save("Modify Bookmarks.docx");
    }
}
Imports System
Imports System.IO
Imports System.Text
Imports GemBox.Document

Module Samples

    Sub Main()

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

        Dim document As DocumentModel = DocumentModel.Load("BookmarksTemplate.docx")

        Dim pathToResources As String = "Resources"

        document.Bookmarks("CompanyName").GetContent(False).LoadText("ACME Corp")
        document.Bookmarks("CompanyAddress").GetContent(False).LoadText("240 Old Country Road, Springfield, IL")
        document.Bookmarks("Country").GetContent(False).LoadText("USA")
        document.Bookmarks("ContactPerson").GetContent(False).LoadText("Joe Smith")

        document.Bookmarks("Text").GetContent(False).LoadText(
            "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.",
            New CharacterFormat() With {.Size = 14})

        Dim picture = New Picture(document, Path.Combine(pathToResources, "Acme.png"))
        document.Bookmarks("Logo").GetContent(False).Set(picture.Content)

        document.Save("Modify Bookmarks.docx")

    End Sub

End Module

Check next sample.