You must be familiar with the term “Bookmark” which marks a feature that you can get to easily when required. While working with large Word documents, you may often add bookmarks to certain text/pages that you would like to be included in the list of places that you would want to access quickly if required. Microsoft Word lets you work with bookmarks in documents by adding, updating, and deleting bookmarks. As a .NET developer, however, you would like to have the functionality of working with bookmarks from within your C#/VB.NET application.

In this blog, we’ll show how to add bookmarks to a Word document in C# using the free and open-source .NET API, NPOI.

How to Insert Bookmark in Word document using Microsoft Word?

Before we can investigate developing a C# application for adding Bookmark to a Word DOCX file, let us first have a look at how the same is done using Microsoft Word. Microsoft Word lets you create bookmarks pointing towards a specific location in your Word document. You can, then, go to any of these bookmarks and can also delete them if not required.

Steps to Add Bookmark in Word Document using Microsoft Word

To add a bookmark in Word document from within Microsoft Word, you can use the following steps.

  1. Open Microsoft Word and start with a blank document.
  2. Select text, a picture, or a place in your document where you want to insert a bookmark.
  3. From the Insert menu, click Bookmark. This will open the Bookmark menu for adding a bookmark.
  4. Under Bookmark name, type a name and click Add.

You can repeat the above steps to add multiple bookmarks to different locations in your document.

How to Go to a Bookmark?

To go to a specific bookmark, type Ctrl+G to open the Go To tab in the Find and Replace box. Under Go to what, click Bookmark. Enter or select the bookmark name, and then click Go To.

How to Insert Bookmark in Word Document using C#?

Now that we have seen how to insert a Bookmark in a Word document using Microsoft Word, we can move towards doing the same using NPOI API for .NET in our C# application. If you haven’t already installed NPOI API, you can go through our comprehensive guide for NPOI API installation in your .NET project.

Step-by-Step Guide to Adding Bookmark in Word Document using C#

At this point, your .NET project should be ready to write code for inserting a bookmark in a Word document using NPOI API in C#. The following steps can be used for this purpose.

  1. Create an instance of XWPFDocument
  2. Define an instance of CT_Bookmark class
  3. Add the bookmark to the paragraph
  4. Save the file to disc
//Creat document
XWPFDocument doc = new XWPFDocument();
CT_P para = doc.Document.body.AddNewP();
//Bookmark start
CT_Bookmark bookmark = new CT_Bookmark();
bookmark.name = "P1";
para.Items.Add(bookmark);
int idCount = para.Items.Count;
bookmark.id = idCount.ToString();
para.ItemsElementName.Add(ParagraphItemsChoiceType.bookmarkStart);
para.AddNewR().AddNewT().Value = "FileFormat.com - How to add bookmarks in Word Document.";
//Bookmark end
bookmark = new CT_Bookmark();
bookmark.id = idCount.ToString();
para.Items.Add(bookmark);
para.ItemsElementName.Add(ParagraphItemsChoiceType.bookmarkEnd);
//Save the file
using (FileStream file = File.Create("Bookmark.docx"))
{
    doc.Write(file);
}

Conclusion

In this article, we showed how to use NPOI API in C# programming language to insert a bookmark in a Word document. NPOI API is an open-source API and free to use for working with Microsoft Word documents. You can have a look at other NPOI examples for working with document files in C#. For more examples on working with NPOI , stay tuned.