Add Replace And Remove Bookmarks In Word Using C#

Introduction

A bookmark identifies a location or a selection of the text, which you name and identify for future reference. In Word, when we add a bookmark to a block of text, the text will be surrounded with square brackets. Now, we can locate the text by using the bookmark instead of scrolling through the document. In this article, I will demonstrate how to add, replace, and remove bookmarks programmatically in C#.

How to Add a Bookmark?

This library provides two methods, which are AppendBookmarkStart (string name) and AppendBookmarkEnd (string name), in the Paragraph class to help us add a bookmark to the specific paragraph. BookmarkStart represents the start point of a bookmark, and BookmarkEnd represents the end point of a bookmark. 

// Load Document
Document document = new Document();
document.LoadFromFile(@"test.docx");
// Add Bookmark
Section section = document.Sections[0];
section.Paragraphs[2].AppendBookmarkStart("bookmark");
section.Paragraphs[3].AppendBookmarkEnd("bookmark");
// Save the document
document.SaveToFile("Bookmark.docx", FileFormat.Docx);

adding a bookmark

How to Replace a Bookmark?

BookmarkNavigator is used for navigating to a bookmark in a Word document. We can retrieve, replace, and delete the contents of a specified bookmark by using BookmarkNavigator. With this library, it’s also possible to replace a bookmark with formatting by setting the saveFormatting argument as true in the ReplaceBookmarkContent (TextBodyPart bodyPart, bool isKeepSourceFirstParaFormat, bool saveFormatting) method.

// Load document
Document document = new Document();
document.LoadFromFile(@"Bookmark.docx");
// Add a section to the document and two paragraphs to the section
Section sec = document.AddSection();
sec.AddParagraph().AppendText("Welcome Back, ");
sec.AddParagraph().AppendText("Friend! ");
// Get the text body part of the two paragraphs
ParagraphBase firstReplacementParagraph = sec.Paragraphs[0].Items.FirstItem as ParagraphBase;
ParagraphBase lastReplacementParagraph = sec.Paragraphs[sec.Paragraphs.Count - 1].Items.LastItem as ParagraphBase;
TextBodySelection selection = new TextBodySelection(firstReplacementParagraph, lastReplacementParagraph);
TextBodyPart part = new TextBodyPart(selection);
// Go to “bookmark”, remove its content but save the formatting
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
bookmarkNavigator.MoveToBookmark("bookmark", true, true);
bookmarkNavigator.DeleteBookmarkContent(true);
// Replace the bookmark content with the text body part of the two paragraphs and keep formatting
bookmarkNavigator.ReplaceBookmarkContent(part, true, true);
// Remove the section and save the document
document.Sections.Remove(sec);
document.SaveToFile("ReplaceBookmark.docx");

Replacebookmark

How to Remove the bookmark?

Every Word document contains a collection of bookmarks, which are accessible through the Bookmarks property of Document class. We can find and remove specific bookmarks by using the Bookmarks property.

// Load Document
Document document = new Document();
document.LoadFromFile(@"Bookmark.docx");
// Find Bookmark
Bookmark bookmark = document.Bookmarks.FindByName("bookmark");
// Remove Bookmark
document.Bookmarks.Remove(bookmark);
// Save and Launch
document.SaveToFile("RemoveBookmark.docx", FileFormat.Docx);

Removing the bookmark


Similar Articles