Bookmarks in Microsoft Word are useful for quickly navigating to specific sections of a document. They allow you to create and name "markers" within your document that you can easily jump to. This can be particularly helpful when working with long or complex documents.
In this article, you will learn how to add and remove bookmarks in a Word document using C# and the Spire.Doc for .NET library.
- Add a Bookmark to a Paragraph in C#
- Add a Bookmark to Specific Text within a Paragraph in C#
- Remove Bookmarks from a Word Document in C#
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Doc
Add a Bookmark to a Paragraph in C#
Using Spire.Doc, you can create a bookmark for a paragraph by inserting a BookmarkStart object at the beginning of the paragraph and a BookmarkEnd object at the end of the paragraph. The space between the bookmark start and end points becomes a defined bookmark that can be referenced and accessed as needed.
The detailed steps to add a bookmark to a paragraph are as follows.
- Create a Document object.
- Load a Word document.
- Get a specific paragraph from a specified section.
- Create a BookmarkStart object using Paragraph.AppendBookmarkStart() method.
- Insert the BookmarkStart at the beginning of the selected paragraph.
- Append a BookmarkEnd object to the end of the paragraph using Paragraph.AppendBookmarkEnd() method.
- Save the document to a different Word file.
- C#
using Spire.Doc; using Spire.Doc.Documents; namespace AddBookmarkToParagraph { class Program { static void Main(string[] args) { // Create a Document object Document doc = new Document(); // Load a Word file doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx"); // Get a specified paragraph Paragraph paragraph = doc.Sections[0].Paragraphs[2]; // Create a bookmark start BookmarkStart start = paragraph.AppendBookmarkStart("myBookmark"); // Insert it at the beginning of the paragraph paragraph.Items.Insert(0, start); // Append a bookmark end at the end of the paragraph paragraph.AppendBookmarkEnd("myBookmark"); // Save the file doc.SaveToFile("AddBookmarkToParagraph.docx", FileFormat.Docx2019); // Dispose resources doc.Dispose(); } } }
Add a Bookmark to Specific Text within a Paragraph in C#
To add a bookmark to specific text, you need first to find the text and its position within the paragraph. Then, insert a BookmarkStart object before the text and a BookmarkEnd object after the text.
The following are the steps to add a bookmark to specific text within a paragraph using Spire.Doc.
- Create a Document object.
- Load a Word document.
- Find the desired text from the document, and get its position in its owner paragraph.
- Create a BookmarkStart object using Paragraph.AppendBookmarkStart() method.
- Insert the BookmarkStart before the selected text.
- Create a BookmarkEnd object using Paragraph.AppendBookmarkEnd() method.
- Insert the BookmarkEnd object behind the selected text.
- Save the document to a different Word file.
- C#
using Spire.Doc; using Spire.Doc.Documents; namespace AddBookmarkToText { class Program { static void Main(string[] args) { // Create a Document object Document doc = new Document(); // Load a Word file doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx"); // Specify the string to find string stringToFind = "Privacy Policy"; // Find the selected text from the document TextSelection[] finds = doc.FindAllString(stringToFind, false, true); TextSelection specificText = finds[1]; // Find the paragraph where the text is located Paragraph para = specificText.GetAsOneRange().OwnerParagraph; // Get the index of the text in the paragraph int index = para.ChildObjects.IndexOf(specificText.GetAsOneRange()); // Create a bookmark start BookmarkStart start = para.AppendBookmarkStart("myBookmark"); // Insert the bookmark start at the index position para.ChildObjects.Insert(index, start); // Create a bookmark end BookmarkEnd end = para.AppendBookmarkEnd("myBookmark"); // Insert the bookmark end at the end of the selected text para.ChildObjects.Insert(index + 2, end); // Save the document to another file doc.SaveToFile("AddBookmarkToText.docx", FileFormat.Docx2019); // Dispose resources doc.Dispose(); } } }
Remove Bookmarks from a Word Document in C#
To remove a specific bookmark or all bookmarks from a Word document, you can use the Bookmarks.Remove() method or the Bookmarks.Clear() method. The detailed steps are as follows.
- Create a Document object.
- Load a Word document.
- Get a specific bookmark from the document by its index using Document.Bookmarks[index] property.
- Remove the bookmark using Bookmarks.Remove() method.
- To remove all bookmarks at once, use Document.Bookmarks.Clear() method.
- Save the document to a different Word file.
- C#
using Spire.Doc; namespace RemoveBookmarks { class Program { static void Main(string[] args) { // Create a Document object Document doc = new Document(); // Load a Word file doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Bookmarks.docx"); // Get a specific bookmark by its index Bookmark bookmark = doc.Bookmarks[0]; // Remove the bookmark doc.Bookmarks.Remove(bookmark); // Remove all bookmarks at once // doc.Bookmarks.Clear(); // Save the document. doc.SaveToFile("RemoveBookmark.docx", FileFormat.Docx2019); // Dispose resources doc.Dispose(); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.