This example demonstrates how to add bookmark to your document.
A bookmark is a thin marker used to keep the reader's place in a book and to enable the reader to return to it with ease.
A bookmark identifies a specific word, section, or location in your document that you name and identify for future reference. For example, you might create a bookmark to identify text that you want to revise at a later time.
We can bookmark with below
- Plain Bookmark
- Hidden Bookmark
- Nested Bookmark
We have versions of this representation in words ,using dropdown you can make a choice within the following
- Word Doc
- Word Docx
- Word ML
- RTF
- MS Text format
- E-book format
- DLS based xml file format
- HTML
After clicking a generate button will launch a file with above choosen version of word with sample bookmarks.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using OfficeComponent.Word;
namespace OfficeComponent.Samples
{
class DocumentBookmarkExample : WordExampleBase
{
public DocumentBookmarkExample(string commonDataPath, string outputDir)
: base(commonDataPath, outputDir)
{
}
public DocumentBookmarkExample(string commonDataPath, string outputDir, string xmlFile)
: base(commonDataPath, outputDir, xmlFile)
{
}
public override string Execute()
{
// Create a new instance of PdfDocument class.
WordDocument document = new WordDocument();
// Adding a section to the document.
Section section = document.AddSection();
// Adding a new paragraph to the section.
Paragraph paragraph = section.AddParagraph();
// Writing text
paragraph.AppendText("This document demonstrates OfficeComponent Word's Bookmark functionality.").CharacterFormat.FontSize = 14f;
// Adding paragraph to the section.
section.AddParagraph();
paragraph = section.AddParagraph();
paragraph.AppendText("1. Inserting Bookmark Text").CharacterFormat.FontSize = 12f;
// Adding paragraph to the section.
section.AddParagraph();
paragraph = section.AddParagraph();
// BookmarkStart.
paragraph.AppendBookmarkStart("Bookmark");
// Write bookmark
paragraph.AppendText("Bookmark Text");
paragraph.AppendComment("This is a simple bookmark");
// BookmarkEnd.
paragraph.AppendBookmarkEnd("Bookmark");
// Adding paragraph to the section.
section.AddParagraph();
paragraph = section.AddParagraph();
// Indicating hidden bookmark text start.
paragraph.AppendBookmarkStart("_HiddenText");
// Writing bookmark text
paragraph.AppendText("2. Hidden Bookmark Text").CharacterFormat.Font = new Font("Comic Sans MS", 10);
// Indicating hidden bookmark text end.
paragraph.AppendBookmarkEnd("_HiddenText");
paragraph.AppendComment("This is hidden bookmark");
section.AddParagraph();
paragraph = section.AddParagraph();
paragraph.AppendText("3. Nested Bookmarks").CharacterFormat.FontSize = 12f;
// Writing nested bookmarks
section.AddParagraph();
paragraph = section.AddParagraph();
paragraph.AppendBookmarkStart("Main");
paragraph.AppendText(" Main data ");
paragraph.AppendBookmarkStart("Nested");
paragraph.AppendText(" Nested data ");
paragraph.AppendComment("This is a nested bookmark");
paragraph.AppendBookmarkStart("NestedNested");
paragraph.AppendText(" Nested Nested ");
paragraph.AppendBookmarkEnd("NestedNested");
paragraph.AppendText(" data Nested ");
paragraph.AppendBookmarkEnd("Nested");
paragraph.AppendText(" Data Main ");
paragraph.AppendBookmarkEnd("Main");
string fileName = Path.Combine(OutputDir, this.GetType().Name + "_" + Guid.NewGuid().ToString() + GetExtension(SaveAsFormat));
// Save the document.
document.Save(fileName, SaveAsFormat);
// Close the document.
document.Close();
return fileName;
}
}
}
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Text
Imports OfficeComponent.Word
Namespace OfficeComponent.Samples
Friend Class DocumentBookmarkExample
Inherits WordExampleBase
Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
MyBase.New(commonDataPath, outputDir)
End Sub
Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
MyBase.New(commonDataPath, outputDir, xmlFile)
End Sub
Public Overrides Function Execute() As String
' Create a new instance of PdfDocument class.
Dim document As New WordDocument()
' Adding a section to the document.
Dim section As Section = document.AddSection()
' Adding a new paragraph to the section.
Dim paragraph As Paragraph = section.AddParagraph()
' Writing text
paragraph.AppendText("This document demonstrates OfficeComponent Word's Bookmark functionality.").CharacterFormat.FontSize = 14F
' Adding paragraph to the section.
section.AddParagraph()
paragraph = section.AddParagraph()
paragraph.AppendText("1. Inserting Bookmark Text").CharacterFormat.FontSize = 12F
' Adding paragraph to the section.
section.AddParagraph()
paragraph = section.AddParagraph()
' BookmarkStart.
paragraph.AppendBookmarkStart("Bookmark")
' Write bookmark
paragraph.AppendText("Bookmark Text")
paragraph.AppendComment("This is a simple bookmark")
' BookmarkEnd.
paragraph.AppendBookmarkEnd("Bookmark")
' Adding paragraph to the section.
section.AddParagraph()
paragraph = section.AddParagraph()
' Indicating hidden bookmark text start.
paragraph.AppendBookmarkStart("_HiddenText")
' Writing bookmark text
paragraph.AppendText("2. Hidden Bookmark Text").CharacterFormat.Font = New Font("Comic Sans MS", 10)
' Indicating hidden bookmark text end.
paragraph.AppendBookmarkEnd("_HiddenText")
paragraph.AppendComment("This is hidden bookmark")
section.AddParagraph()
paragraph = section.AddParagraph()
paragraph.AppendText("3. Nested Bookmarks").CharacterFormat.FontSize = 12F
' Writing nested bookmarks
section.AddParagraph()
paragraph = section.AddParagraph()
paragraph.AppendBookmarkStart("Main")
paragraph.AppendText(" Main data ")
paragraph.AppendBookmarkStart("Nested")
paragraph.AppendText(" Nested data ")
paragraph.AppendComment("This is a nested bookmark")
paragraph.AppendBookmarkStart("NestedNested")
paragraph.AppendText(" Nested Nested ")
paragraph.AppendBookmarkEnd("NestedNested")
paragraph.AppendText(" data Nested ")
paragraph.AppendBookmarkEnd("Nested")
paragraph.AppendText(" Data Main ")
paragraph.AppendBookmarkEnd("Main")
Dim fileName As String = Path.Combine(OutputDir, Me.GetType().Name & "_" & Guid.NewGuid().ToString() & GetExtension(SaveAsFormat))
' Save the document.
document.Save(fileName, SaveAsFormat)
' Close the document.
document.Close()
Return fileName
End Function
End Class
End Namespace