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:
- Load document which I want to insert bookmark.
- Get sections in this document and fix location you want to start with bookmark. That is the second paragraph of section.
- Use paragraph.BookmarkStart() method to define bookmark beginning location. There is a parameter passed to this method, string bookmark name.
- Fix location you want to end with bookmark. That is the third paragraph of section.
- Use paragraph.BookmarkEnd() method to define bookmark ending location. Also, string bookmark name should be passed to this method.
- 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.
static void Main(string args)
//Create word document
Document document = new Document();
Section section = document.Sections;
Paragraph paragraph = section.Paragraphs;
Paragraph paragraph2 = section.Paragraphs;
//Save and Launch
Friend Class Bookmark
Shared Sub Main(ByVal args() As String)
‘Create word document
Dim document As New Document()
Dim section As Section = document.Sections(0)
Dim paragraph As Paragraph = section.Paragraphs(1)
Dim paragraph2 As Paragraph = section.Paragraphs(2)
‘Save and Launch
Result Shown as Following: