Syncfusion Feedback

Bookmarks in Word Documents Using the Syncfusion .NET Word Library

Bookmarks in a Word document serve as placeholders that can mark particular locations or content. The Syncfusion® .NET Word library enables users to add and remove bookmarks in C#. Users can programmatically extract, edit, and delete bookmarked content in C#, providing a versatile method to identify and locate content within a Word document without relying on Microsoft Word or interop dependencies.

Watch this video to learn how to handle bookmarks in Word documents using the Syncfusion .NET Word Library.

Watch the video

Manage bookmarks in Word documents using C#

Learn how to manage bookmarks in Word documents programmatically using C# with the Syncfusion .NET Word Library. This guide demonstrates adding bookmarks and replacing bookmark content to manage document placeholders effectively.

Step 1: Create a new project

Start by creating a new C# Console Application project.

Step 2: Install the NuGet package

Add the Syncfusion.DocIO.Net.Core package to your project from NuGet.org.

Step 3: Add required namespaces to work with bookmarks

Add the following namespaces to your Program.cs file:

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using System.IO;

Step 4: Open the template Word document

Open an existing Word document that contains the source bookmark content.

FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//Open an existing Word document.
WordDocument templateDocument = new WordDocument(fileStreamPath, FormatType.Automatic);

Step 5: Get bookmark content from template

Navigate to a specific bookmark in the template document and retrieve its content as a WordDocumentPart.

//Create the bookmark navigator instance to access the bookmark
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(templateDocument);
//Move the virtual cursor to the location before the end of the bookmark "Northwind"
bookmarkNavigator.MoveToBookmark("Northwind");
//Get the bookmark content as WordDocumentPart
WordDocumentPart wordDocumentPart = bookmarkNavigator.GetContent();

Step 6: Open the target Word document

Open the Word document where the bookmark content will be replaced.

//Load the Word document with bookmark NorthwindDB
FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Bookmarks.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
WordDocument document = new WordDocument(fileStream, FormatType.Docx);

Step 7: Replace bookmark content in target document

Navigate to the target bookmark and replaces its content with the previously retrieved WordDocumentPart.

Run

//Create the bookmark navigator instance to access the bookmark
bookmarkNavigator = new BookmarksNavigator(document);
//Move the virtual cursor to the location before the end of the bookmark "NorthwindDB"
bookmarkNavigator.MoveToBookmark("NorthwindDB");
//Replace the bookmark content with word body part
bookmarkNavigator.ReplaceContent(wordDocumentPart);
//Close the WordDocumentPart instance
wordDocumentPart.Close();

Step 8: Save the Word document

Save the Word document to a file stream in DOCX format.

//Create file stream
FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.docx"), FileMode.Create, FileAccess.ReadWrite);
//Save the Word document to file stream
document.Save(outputFileStream, FormatType.Docx);
// Close all streams and documents
outputFileStream.Close();
document.Close();
fileStream.Close();

NuGet installation

Nuget Installation image Syncfusion.DocIO.Net.Core Copy Icon image

Get started quickly by downloading the installer and checking license information on the Downloads page.

Syncfusion .NET Word Library Resources

Explore these resources for comprehensive guides, knowledge base articles, insightful blogs, and ebooks.