How to Insert Word Bookmark in WPF

In Word, bookmark is used for positioning, it enables readers to return to the read or modified location quickly. Thus, it would be necessary for us to insert a bookmark when we are editing or reading a long word document, so that next time we can get back to the specific location in a short period.

This article will demonstrate how to insert word bookmark in WPF by using Spire.Doc for WPF.

At first, please download Spire.Doc and install it correctly, then add Spire.Doc.Wpf.dll and Spire.License.dll from the installation folder as reference.

This is the effective screenshot after inserting bookmark:

How to Insert Word Bookmark in WPF

Now, follow the Detail steps below:

Use namespace:

using System.Windows;
using Spire.Doc;

Step 1: Initialize a new instance of the Document class and load the word document from file.

Document document = new Document();
document.LoadFromFile("Stories.docx");

Step 2: Get its first section and insert bookmark starts from the end of the second paragraph and ends to the third paragraph.

Section section = document.Sections[0];
section.Paragraphs[1].AppendBookmarkStart("bookmark");
section.Paragraphs[2].AppendBookmarkEnd("bookmark");

Step 3: Save and launch the file.

document.SaveToFile("Bookmark.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("Bookmark.docx");

Full codes:

using Spire.Doc;
using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Document document = new Document();
            document.LoadFromFile("Stories.docx");

            Section section = document.Sections[0];
            section.Paragraphs[1].AppendBookmarkStart("bookmark");
            section.Paragraphs[2].AppendBookmarkEnd("bookmark");

            document.SaveToFile("Bookmark.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Bookmark.docx");
        }



    }
}