Bookmarks can be used as placeholders in Word templates. By replacing the contents of bookmarks in the templates, developers can quickly generate a batch of Word documents with preset formatting and layout. In this article, I will explain how to replace bookmarks in Word documents in C# and VB.NET using a .NET Word API – Free Spire.Doc for .NET.
- Replace Bookmark with Text
- Replace Bookmark with Image
- Replace Bookmark with HTML
Installation
You can download the DLL file of the API from this website or install it via NuGet by selecting Tools > NuGet Package Manager > Package Manager Console and then execute the following command:
Install-Package FreeSpire.Doc
Using the Code
Free Spire.Doc provides a BookmarksNavigator class which enables developers to easily navigate to a specific bookmark in a Word document and replace the content of it. The following code examples show how to replace bookmarks with text, image and html respectively using C# and VB.NET.
Example 1. Replace Bookmark with Text
C#
using Spire.Doc;
using Spire.Doc.Documents;
using System.Collections.Generic;
namespace ReplaceBookmarkWithText
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document document = new Document();
//Load the template document
document.LoadFromFile("Template.docx");
//Create a Dictionary instance
Dictionary<string, string> values = new Dictionary<string, string>()
{
{"Name", "Michael" },
{"Email", "Michael@outlook.com"},
{"Address", "Canton, OH 44704"}
};
//Replace bookmark content with text
foreach (KeyValuePair<string, string> item in values)
{
//Create a BookmarksNavigator instance
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
//Locate the bookmark
bookmarkNavigator.MoveToBookmark(item.Key);
//Replace the bookmark content with text
bookmarkNavigator.ReplaceBookmarkContent(item.Value, true);
//Remove the bookmark
document.Bookmarks.Remove(bookmarkNavigator.CurrentBookmark);
}
//Save the result document
document.SaveToFile("ReplaceBookmarkWithText.docx", FileFormat.Docx2013);
}
}
}
VB.NET
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports System.Collections.Generic
Namespace ReplaceBookmarkWithText
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a Document instance
Dim document As Document = New Document()
'Load the template document
document.LoadFromFile("Template.docx")
'Create a Dictionary instance
Dim values As Dictionary(Of String, String) = New Dictionary(Of String, String)() From {
{"Name", "Michael"},
{"Email", "Michael@outlook.com"},
{"Address", "Canton, OH 44704"}
}
'Replace bookmark content with text
For Each item In values
'Create a BookmarksNavigator instance
Dim bookmarkNavigator As BookmarksNavigator = New BookmarksNavigator(document)
'Locate the bookmark
bookmarkNavigator.MoveToBookmark(item.Key)
'Replace the bookmark content with text
bookmarkNavigator.ReplaceBookmarkContent(item.Value, True)
'Remove the bookmark
document.Bookmarks.Remove(bookmarkNavigator.CurrentBookmark)
Next
'Save the result document
document.SaveToFile("ReplaceBookmarkWithText.docx", FileFormat.Docx2013)
End Sub
End Class
End Namespace
The input Word template:
The result document:
Example 2. Replace Bookmark with Image
C#
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
namespace ReplaceBookmarkWithImage
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document document = new Document();
//Load the template document
document.LoadFromFile("ReplaceBookmarkWithText.docx");
//Replace bookmark with image
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
//Locate the bookmark
bookmarkNavigator.MoveToBookmark("Photo");
//Add a paragraph
Paragraph para = document.Sections[0].AddParagraph();
//Add an image to the paragraph
Image image = Image.FromFile("photo.png");
para.AppendPicture(image);
//Insert the paragraph to the bookmark
bookmarkNavigator.InsertParagraph(para);
//Remove the bookmark
document.Bookmarks.Remove(bookmarkNavigator.CurrentBookmark);
//Save the result document
document.SaveToFile("ReplaceBookmarkWithImage.docx", FileFormat.Docx2013);
}
}
}
VB.NET
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace ReplaceBookmarkWithImage
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a Document instance
Dim document As Document = New Document()
'Load the template document
document.LoadFromFile("ReplaceBookmarkWithText.docx")
'Replace bookmark with image
Dim bookmarkNavigator As BookmarksNavigator = New BookmarksNavigator(document)
'Locate the bookmark
bookmarkNavigator.MoveToBookmark("Photo")
'Add a paragraph
Dim para As Paragraph = document.Sections(0).AddParagraph()
'Add an image to the paragraph
Dim image As Image = Image.FromFile("photo.png")
para.AppendPicture(image)
'Insert the paragraph to the bookmark
bookmarkNavigator.InsertParagraph(para)
'Remove the bookmark
document.Bookmarks.Remove(bookmarkNavigator.CurrentBookmark)
'Save the result document
document.SaveToFile("ReplaceBookmarkWithImage.docx", FileFormat.Docx2013)
End Sub
End Class
End Namespace
The result document:
Example 3. Replace Bookmark with HTML
C#
using Spire.Doc;
using Spire.Doc.Documents;
namespace ReplaceBookmarkWithHtml
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document document = new Document();
//Load the template document
document.LoadFromFile("ReplaceBookmarkWithImage.docx");
//Create a BookmarksNavigator instance
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
//Locate the bookmark
bookmarkNavigator.MoveToBookmark("Education");
//Add a paragraph
Paragraph para = document.Sections[0].AddParagraph();
//Add HTML string to the paragraph
para.AppendHTML("<p>University of South Florida, <font color=\"#ff0000\">September 2013 - June 2017</font></p>");
//Insert the paragraph to the bookmark
bookmarkNavigator.InsertParagraph(para);
//Remove the bookmark
document.Bookmarks.Remove(bookmarkNavigator.CurrentBookmark);
//Save the result document
document.SaveToFile("ReplaceBookmarkWithHtml.docx", FileFormat.Docx2013);
}
}
}
VB.NET
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace ReplaceBookmarkWithHtml
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a Document instance
Dim document As Document = New Document()
'Load the template document
document.LoadFromFile("ReplaceBookmarkWithImage.docx")
'Create a BookmarksNavigator instance
Dim bookmarkNavigator As BookmarksNavigator = New BookmarksNavigator(document)
'Locate the bookmark
bookmarkNavigator.MoveToBookmark("Education")
'Add a paragraph
Dim para As Paragraph = document.Sections(0).AddParagraph()
'Add HTML string to the paragraph
para.AppendHTML("<p>University of South Florida, <font color=""#ff0000"">September 2013 - June 2017</font></p>")
'Insert the paragraph to the bookmark
bookmarkNavigator.InsertParagraph(para)
'Remove the bookmark
document.Bookmarks.Remove(bookmarkNavigator.CurrentBookmark)
'Save the result document
document.SaveToFile("ReplaceBookmarkWithHtml.docx", FileFormat.Docx2013)
End Sub
End Class
End Namespace
The result document: