C#/VB.NET: Insert a Bookmark in Word

When dealing with a lengthy Word document, inserting bookmarks with different names is a convenient way to mark places in the document. Once the bookmarks are inserted, you can quickly jump to the specified places without scrolling through page after page. In this article, you will learn how to insert a bookmark into an existing Word document using Spire.Doc for .NET.

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 DLLs files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Insert a Bookmark into an Existing Word Document

Spire.Doc for .NET provides the Paragraph.AppendBookmarkStart(string name) and Paragraph.AppendBookmarkEnd(string name) methods to insert a bookmark with specified name into the specified paragraphs in a Word document. The detailed steps are as follows.

  • Create a Document instance.
  • Load a sample Word document using Document.LoadFromFile() method.
  • Get the first section of the Word Document using Document.Sections[] property.
  • Get a specified paragraph of the section using Section.Paragraphs[] property.
  • Append the start of the bookmark with specified name to the specified paragraph using Paragraph.AppendBookmarkStart(string name) method.
  • Append the end of the bookmark with specified name to the specified paragraph using Paragraph.AppendBookmarkEnd(string name) method.
  • Save the document to another file using Document. SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;

namespace WordBookmark
{
    class Bookmark
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document document = new Document();

            //Load a sample Word document
            document.LoadFromFile(@"C:\Users\Administrator\Desktop\test.docx");

            //Get the first section 
            Section section = document.Sections[0];

            //Insert a bookmark with specified name into the specified paragraphs
            section.Paragraphs[9].AppendBookmarkStart("SecurityTerm");
            section.Paragraphs[11].AppendBookmarkEnd("SecurityTerm");

            //Save the document to file
            document.SaveToFile("Bookmark.docx", FileFormat.Docx);
        }
    }
}

C#/VB.NET: Insert a Bookmark in Word

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.