Insert Word Bookmark in C#, VB.NET

Word bookmark is convenient for users to go to specified location and modify contents of bookmark range in a document. Solution in this guide introduces how to insert bookmark in existing Word document in C# and VB.NET via Spire.Doc for .NET. In this example, the bookmark range is from the end of the fourth paragraph to the end of the fifth paragraph. The following screenshot presents effect after Work bookmark inserting.

Insert Word Bookmark

Spire.Doc for .NET, a professional .NET Word component, provides two methods to insert Word bookmark, Paragraph.AppendBookmarkStart and Paragraph.AppendBookmarkEnd with parameter string name. The former method is used to append start of the bookmark with specified name into paragraph, while the later to append end of bookmark. Download and install Spire.Doc for .NET and follow the code to insert Word bookmark.

[C#]
using System;
using Spire.Doc;
using Spire.Doc.Documents;

namespace WordBookmark
{
    class Bookmark
    {
        static void Main(string[] args)
        {
            //Load Document
            Document document = new Document();
            document.LoadFromFile(@"E:\Work\Document\.NET Framework.docx");

            //Insert Bookmark
            Section section = document.Sections[0];
            section.Paragraphs[3].AppendBookmarkStart(".NETFramework");
            section.Paragraphs[4].AppendBookmarkEnd(".NETFramework");

            //Save and Launch 
            document.SaveToFile("Bookmark.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Bookmark.docx");
        }
    }
}
[VB.NET]
Imports System
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace WordBookmark
    Friend Class Bookmark
        Shared Sub Main(ByVal args() As String)
            'Load Document
            Dim document As New Document()
            document.LoadFromFile("E:\Work\Document\.NET Framework.docx")

            'Insert Bookmark
            Dim section As Section = document.Sections(0)
            section.Paragraphs(3).AppendBookmarkStart(".NETFramework")
            section.Paragraphs(4).AppendBookmarkEnd(".NETFramework")

            'Save and Launch 
            document.SaveToFile("Bookmark.docx", FileFormat.Docx)
            System.Diagnostics.Process.Start("Bookmark.docx")
        End Sub
    End Class
End Namespace

Spire.Doc, an easy-to-use component to operate Word document, allows developers to fast generate, write, edit and save Word (Word 97-2003, Word 2007, Word 2010) in C# and VB.NET for .NET, Silverlight and WPF.