How to replace the content between two different existing bookmarks in a word document
Syncfusion Essential DocIO is a .NET Word library used to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies.
Using this library, you can replace the content between two different bookmarks in a word document using C# and VB.NET. Follow the steps below.
- Design your template Word document with the two different bookmarks having text in-between them using Microsoft Word.
- Insert a temporary bookmark which is used to defined the range of content.
- Access the bookmark and replace the content using BookmarksNavigator.
- Remove that temporary bookmark after replaced the content.
Steps to replace the content between two different bookmarks in word using C# and VB.NET:
- Create a new C# console application project.
- Install the Syncfusion.DocIO.WinForms NuGet package as a reference to your .NET Framework applications from NuGet.org.
- Include the following namespace in the Program.cs file.
C#
using Syncfusion.DocIO; using Syncfusion.DocIO.DLS;
VB
Imports Syncfusion.DocIO Imports Syncfusion.DocIO.DLS
- Use the following code to replace the content between two different bookmarks in a word document.
C#
//Load the template document. using (WordDocument document = new WordDocument(@"Template.docx", FormatType.Docx)) { //Replace the text between two different existing bookmarks. ReplaceBookmarkContent(document, "BM1", "BM2", "New text inserted"); //Save and close the Word document. document.Save("Output.docx", FormatType.Docx); } System.Diagnostics.Process.Start("Output.docx");
VB
'Load the template document. Using document As WordDocument = New WordDocument("Template.docx", FormatType.Docx) 'Replace the text between two different existing bookmarks. ReplaceBookmarkContent(document, "BM1", "BM2", "New text inserted") 'Save and close the Word document. document.Save("Output.docx", FormatType.Docx) End Using System.Diagnostics.Process.Start("Output.docx")
- Helper method to replace content.
C#
public void ReplaceBookmarkContent(WordDocument document, String bookmark1, String bookmark2, String replacementContent) { //Temp Bookmark. String tempBookmarkName = "tempBookmark"; #region Insert bookmark start after bookmark1. //Get the bookmark instance by using FindByName method of BookmarkCollection with bookmark name. Bookmark firstBookmark = document.Bookmarks.FindByName(bookmark1); //Access the bookmark end’s owner paragraph by using bookmark. WParagraph firstBookmarkOwnerPara = firstBookmark.BookmarkEnd.OwnerParagraph; //Get the index of bookmark end of bookmark1. int index = firstBookmarkOwnerPara.Items.IndexOf(firstBookmark.BookmarkEnd); //Create and add new bookmark start after bookmark1. BookmarkStart newBookmarkStart = new BookmarkStart(document, tempBookmarkName); firstBookmarkOwnerPara.ChildEntities.Insert(index + 1, newBookmarkStart); #endregion #region Insert bookmark end before bookmark2. //Get the bookmark instance by using FindByName method of BookmarkCollection with bookmark name. Bookmark secondBookmark = document.Bookmarks.FindByName(bookmark2); //Access the bookmark start’s owner paragraph by using bookmark. WParagraph secondBookmarkOwnerPara = secondBookmark.BookmarkStart.OwnerParagraph; //Get the index of bookmark start of bookmark2. index = secondBookmarkOwnerPara.Items.IndexOf(secondBookmark.BookmarkStart); //Create and add new bookmark end before bookmark2. BookmarkEnd newBookmarkEnd = new BookmarkEnd(document, tempBookmarkName); secondBookmarkOwnerPara.ChildEntities.Insert(index, newBookmarkEnd); #endregion #region Select bookmark content and replace. //Create the bookmark navigator instance to access the newly created bookmark. BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document); //Move the virtual cursor to the location of the temp bookmark. bookmarkNavigator.MoveToBookmark(tempBookmarkName); //Replace the bookmark content. bookmarkNavigator.ReplaceBookmarkContent(replacementContent, true); #endregion #region Remove that temporary bookmark. //Get the bookmark instance by using FindByName method of BookmarkCollection with bookmark name. Bookmark bookmark = document.Bookmarks.FindByName(tempBookmarkName); //Remove the temp bookmark named from Word document. document.Bookmarks.Remove(bookmark); #endregion }
VB
Public Shared Sub ReplaceBookmarkContent(ByVal document As WordDocument, ByVal bookmark1 As String, ByVal bookmark2 As String, ByVal replacementContent As String) Dim tempBookmarkName As String = "tempBookmark" #Region "Insert bookmark start after bookmark1" 'Get the bookmark instance by using FindByName method of BookmarkCollection with bookmark name. Dim firstBookmark As Bookmark = document.Bookmarks.FindByName(bookmark1) 'Access the bookmark end_s owner paragraph by using bookmark. Dim firstBookmarkOwnerPara As WParagraph = firstBookmark.BookmarkEnd.OwnerParagraph 'Get the index of bookmark end of bookmark1. Dim index As Integer = firstBookmarkOwnerPara.Items.IndexOf(firstBookmark.BookmarkEnd) 'Create and add new bookmark start after bookmark1. Dim newBookmarkStart As BookmarkStart = New BookmarkStart(document, tempBookmarkName) firstBookmarkOwnerPara.ChildEntities.Insert((index + 1), newBookmarkStart) #End Region #Region "Insert bookmark End before bookmark2" 'Get the bookmark instance by using FindByName method of BookmarkCollection with bookmark name. Dim secondBookmark As Bookmark = document.Bookmarks.FindByName(bookmark2) 'Access the bookmark start_s owner paragraph by using bookmark. Dim secondBookmarkOwnerPara As WParagraph = secondBookmark.BookmarkStart.OwnerParagraph 'Get the index of bookmark start of bookmark2. index = secondBookmarkOwnerPara.Items.IndexOf(secondBookmark.BookmarkStart) 'Create and add new bookmark end before bookmark2. Dim newBookmarkEnd As BookmarkEnd = New BookmarkEnd(document, tempBookmarkName) secondBookmarkOwnerPara.ChildEntities.Insert(index, newBookmarkEnd) #End Region #Region "Select bookmark content and replace" 'Create the bookmark navigator instance to access the newly created bookmark. Dim bookmarkNavigator As BookmarksNavigator = New BookmarksNavigator(document) 'Move the virtual cursor to the location of the temp bookmark. bookmarkNavigator.MoveToBookmark(tempBookmarkName) 'Replace the bookmark content. bookmarkNavigator.ReplaceBookmarkContent(replacementContent, True) #End Region #Region "Remove that temporary bookmark" Dim bookmark As Bookmark = document.Bookmarks.FindByName(tempBookmarkName) 'Remove the temp bookmark named from Word document. document.Bookmarks.Remove(bookmark) #End Region End Sub
A complete working example of how to replace the content between two different bookmarks in a word document using C# can be downloaded from ReplaceContentBetweenTwoDifferentBookmarks.zip
By executing the program, you will get the output Word document as follows.
Take a moment to peruse the documentation, where you can find basic Word document processing options along with the features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly, PDF and Image conversions with code examples.
Explore more about the rich set of Syncfusion Word Framework features.
See also:
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn how to generate and register a Syncfusion license key in your application to use the components without a trail message.