Insert Bookmark to Fix Location Quickly in Word with C#/VB.NET

When talking about bookmark, we think of e-bookmark which saves website to be convenient for visiting next time. But, Word Bookmark is quietly different with e-bookmark. It is used to fix location to lead readers to get wanted contents quickly. In this post, I want to introduce a method to insert bookmark in Word with C#/VB.NET.

In a Word document, we can add bookmark for paragraph, text and image. For example, users can set bookmark to start with paragraph beginning and end with paragraph ending for get location of one special paragraph. Also, bookmark is very useful when there are several fields in your document. You can set bookmark for these fields to fill them quickly. 

In this example, I will insert a bookmark in my document. This bookmark starts from the end of paragraph 2 and end with the end of paragraph 3.

This method is based on a .NET Word component, Spire.Doc for .NET.  So if you want to use the following code, please download and install it on your system. Then, add its dll file as reference in your project.

Detailed Steps Shown as Following:           

  1. Load document which I want to insert bookmark.
  2. Get sections in this document and fix location you want to start with bookmark. That is the second paragraph of section.
  3. Use paragraph.BookmarkStart() method to define bookmark beginning location. There is a parameter passed to this method, string bookmark name.
  4. Fix location you want to end with bookmark. That is the third paragraph of section.
  5. Use paragraph.BookmarkEnd() method to define bookmark ending location. Also, string bookmark name should be passed to this method.
  6. Save and launch file.

Please note that: if you want to add bookmark for paragraph in existed document, the bookmark will be fixed at the end of paragraphs by default.

Main Coding:

C#

using System;
using Spire.Doc;
using Spire.Doc.Documents;

namespace WordBookmark
{
    class Bookmark
    {
        static void Main(string[] args)
        {
            //Create word document
            Document document = new Document();
            document.LoadFromFile(@”D:\work\Documents\Blues Introduction.docx”);

            Section section = document.Sections[0];
            Paragraph paragraph = section.Paragraphs[1];
            paragraph.AppendBookmarkStart(“blues”);
            Paragraph paragraph2 = section.Paragraphs[2];
            paragraph2.AppendBookmarkEnd(“blues”);

            //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)

            ‘Create word document
            Dim document As New Document()
            document.LoadFromFile(“D:\work\Documents\Blues Introduction.docx”)

            Dim section As Section = document.Sections(0)
            Dim paragraph As Paragraph = section.Paragraphs(1)
            paragraph.AppendBookmarkStart(“blues”)
            Dim paragraph2 As Paragraph = section.Paragraphs(2)
            paragraph2.AppendBookmarkEnd(“blues”)

            ‘Save and Launch
            document.SaveToFile(“Bookmark.docx”, FileFormat.Docx)
            System.Diagnostics.Process.Start(“Bookmark.docx”)

        End Sub
    End Class
End Namespace

Result Shown as Following:

 Download Spire.Doc for .NET Here

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s