Use DocumentBuilder to inserting a Bookmark
within C# and VB.NET.


   Bookmarks work with hyperlinks to let you jump to a specific place in your document

   To insert a bookmark in a document, call DocumentBuilderStartBookmark, passing it the desired bookmark name. Insert the bookmark content using the DocumentBuilder methods. Call DocumentBuilderEndBookmark, passing it the same name that you used with DocumentBuilderStartBookmark.
Bookmark names need to begin with a letter. They can include both numbers and letters, but not spaces.

Complete code

using System;
using SautinSoft.Document;
using System.Text;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            InsertingBookmark();
        }
        /// <summary>
        /// How to insert a Bookmark in a document using DocumentBuilder.
        /// </summary>
        /// <remarks>
        /// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/documentbuilder-inserting-bookmark.php
        /// </remarks>

        static void InsertingBookmark()
        {
            DocumentCore dc = new DocumentCore();
            DocumentBuilder db = new DocumentBuilder(dc);

            string resultPath = @"result.docx";

            // Insert the formatted text into the document using DocumentBuilder.
            db.CharacterFormat.FontName = "Verdana";
            db.CharacterFormat.Size = 16;
            db.CharacterFormat.FontColor = Color.Orange;
            db.Writeln("This text is inserted by the DocumentBuilder.Write method with formatting.");

            // Marks the current position in the document as a 1st bookmark start.
            db.StartBookmark("Firstbookmark");
            db.CharacterFormat.Italic = true;
            db.CharacterFormat.Size = 12;
            db.CharacterFormat.FontColor = Color.Blue;
            db.Writeln("The text inside the bookmark 'Firstbookmark' is inserted by the DocumentBuilder.Writeln method.");
            
            // Marks the current position in the document as a 1st bookmark end.
            db.EndBookmark("Firstbookmark");

            // Insert text after the 1st bookmark.
            db.CharacterFormat.Italic = false;
            db.CharacterFormat.Size = 16;
            db.CharacterFormat.FontColor = Color.Orange;
            db.Writeln("DocumentBuilder.EndBookmark method with the same name points to the end of the bookmark.");

            // Marks the current position in the document as a 2nd bookmark start.
            db.StartBookmark("Secondbookmark");
            db.CharacterFormat.Italic = true;
            db.CharacterFormat.Size = 12;
            db.CharacterFormat.FontColor = Color.Blue;
            db.Writeln("Incorrectly spelled bookmarks or bookmarks with duplicate names will be ignored when saving the document.");

            // Marks the current position in the document as a 2nd bookmark end.
            db.EndBookmark("Secondbookmark");

            // Save our document into DOCX format.
            dc.Save(resultPath, new DocxSaveOptions());

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(resultPath) { UseShellExecute = true });
        }
    }
}

Download.

            Imports System
Imports SautinSoft.Document
Imports System.Text

Namespace Example
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			InsertingBookmark()
		End Sub
		''' <summary>
		''' How to insert a Bookmark in a document using DocumentBuilder.
		''' </summary>
		''' <remarks>
		''' Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/documentbuilder-inserting-bookmark.php
		''' </remarks>

		Private Shared Sub InsertingBookmark()
			Dim dc As New DocumentCore()
			Dim db As New DocumentBuilder(dc)

			Dim resultPath As String = "result.docx"

			' Insert the formatted text into the document using DocumentBuilder.
			db.CharacterFormat.FontName = "Verdana"
			db.CharacterFormat.Size = 16
			db.CharacterFormat.FontColor = Color.Orange
			db.Writeln("This text is inserted by the DocumentBuilder.Write method with formatting.")

			' Marks the current position in the document as a 1st bookmark start.
			db.StartBookmark("Firstbookmark")
			db.CharacterFormat.Italic = True
			db.CharacterFormat.Size = 12
			db.CharacterFormat.FontColor = Color.Blue
			db.Writeln("The text inside the bookmark 'Firstbookmark' is inserted by the DocumentBuilder.Writeln method.")

			' Marks the current position in the document as a 1st bookmark end.
			db.EndBookmark("Firstbookmark")

			' Insert text after the 1st bookmark.
			db.CharacterFormat.Italic = False
			db.CharacterFormat.Size = 16
			db.CharacterFormat.FontColor = Color.Orange
			db.Writeln("DocumentBuilder.EndBookmark method with the same name points to the end of the bookmark.")

			' Marks the current position in the document as a 2nd bookmark start.
			db.StartBookmark("Secondbookmark")
			db.CharacterFormat.Italic = True
			db.CharacterFormat.Size = 12
			db.CharacterFormat.FontColor = Color.Blue
			db.Writeln("Incorrectly spelled bookmarks or bookmarks with duplicate names will be ignored when saving the document.")

			' Marks the current position in the document as a 2nd bookmark end.
			db.EndBookmark("Secondbookmark")

			' Save our document into DOCX format.
			dc.Save(resultPath, New DocxSaveOptions())

			' Open the result for demonstration purposes.
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(resultPath) With {.UseShellExecute = True})
		End Sub
	End Class
End Namespace

Download.


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.