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#, using a .NET Word library.

Implementation

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

  1. //Load Document  
  2. Document document = new Document();  
  3. document.LoadFromFile(@"test.docx");  
  4.   
  5. //Add Bookmark  
  6. Section section = document.Sections[0];  
  7. section.Paragraphs[2].AppendBookmarkStart("bookmark");  
  8. section.Paragraphs[3].AppendBookmarkEnd("bookmark");  
  9.   
  10. //Save the document   
  11. document.SaveToFile("Bookmark.docx", FileFormat.Docx);   

C#

Replacing 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 ReplaceBookmarkContent (TextBodyPart bodyPart, bool isKeepSourceFirstParaFormat, bool saveFormatting) method. 

  1. //Load document  
  2. Document document = new Document();  
  3. document.LoadFromFile(@"Bookmark.docx");  
  4.   
  5. //Add a section to the document and two paragraphs to the section  
  6. Section sec = document.AddSection();  
  7. sec.AddParagraph().AppendText("Welcome Back, ");  
  8. sec.AddParagraph().AppendText("Friend! ");  
  9.   
  10. //Get the text body part of the two paragraphs  
  11. ParagraphBase firstReplacementParagraph = sec.Paragraphs[0].Items.FirstItem as ParagraphBase;  
  12. ParagraphBase lastReplacementParagraph = sec.Paragraphs[sec.Paragraphs.Count - 1].Items.LastItem as ParagraphBase;  
  13. TextBodySelection selection = new TextBodySelection(firstReplacementParagraph, lastReplacementParagraph);  
  14. TextBodyPart part = new TextBodyPart(selection);  
  15.   
  16. //Go to “bookmark”, remove its content but save the formatting  
  17. BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);  
  18. bookmarkNavigator.MoveToBookmark("bookmark"truetrue);  
  19. bookmarkNavigator.DeleteBookmarkContent(true);  
  20.   
  21. //Replace the bookmark content with the text body part of the two paragraphs and keep formatting  
  22. bookmarkNavigator.ReplaceBookmarkContent(part, truetrue);  
  23.   
  24. //Remove the section and save the document  
  25. document.Sections.Remove(sec);  
  26. document.SaveToFile("ReplaceBookmark.docx");   

C#

Removing 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 bookmark by using the Bookmarks property. 

  1. //Load Document  
  2. Document document = new Document();  
  3. document.LoadFromFile(@"Bookmark.docx");  
  4. Bookmark bookmark = document.Bookmarks.FindByName("bookmark");  
  5. //Remove Bookmark  
  6. document.Bookmarks.Remove(bookmark);  
  7. //Save and Launch  
  8. document.SaveToFile("RemoveBookmark.docx", FileFormat.Docx);   

C#