Bookmarks

Bookmarks are a powerful tool for marking parts of the document, which can be later retrieved and reviewed. They can be added to the document both programmatically and via the UI. You can also have hyperlinks pointing to the annotations in the document. This is very convenient, as it provides the means for easier navigation in the document and enables features like table of contents.

Adding Bookmarks via the UI

Bookmarks can be inserted in the document and removed through the ManageBookmarksDialog, which is opened on pressing the Bookmark button in the Insert Tab of the default RadRichTextBoxRibbonUI.

Silverlight RadRichTextBox Bookmarks Window

As all pop-ups that RadRichTextBox uses, the ManageBookmarksDialog can be completely replaced by another, user-defined dialog implementing an interface.

Using Bookmarks Programmatically

The document elements that encapsulate the bookmarks functionality are BookmarkRangeStart and BookMarkRangeEnd, which are placed at the start and the end of the bookmark respectively. Some of the useful methods that RadRichTextBox and RadDocument expose are:

  • this.editor.InsertBookmark(string bookmarkName): Inserts a Bookmark with the name specified. If there is selection in the document, the BookmarkRangeStart will be inserted just before the first selected element and the BookmarkRangeEnd will be added at the end of the first selection range.

  • this.editor.Document.GoToBookmark( bookmarkName)
    this.editor.Document.GoToBookmark(BookmarkRangeStart bookmarkStart) : Both methods move the caret to the bookmark specified. As bookmarks with the same name cannot be inserted in the same document, the name of the bookmark can be used as an identifier.

  • this.editor.Document.Selection.SelectAnnotationRange(AnnotationRangeStartannotationStart) : Selects the annotation passed as parameter. Particularly useful, as most methods of RadRichTextBox and RadDocument operate on the selection. For example, if you invoke this.editor.Delete(false), the text of the Bookmark along with the Bookmark itself will be erased.

  • this.editor.DeleteBookmark(string bookmarkName)
    this.editor.DeleteBookmark(BookmarkRangeStart bookmarkRangeStart) : These two methods remove the bookmark. The text in the document between the BookmarkRangeStart and BookmarkRangeEnd is not deleted.

  • this.editor.Document.GetAllBookmarks(): Returns an IEnumerable containing all BookmarkRangeStarts.

  • this.editor.Document.EnumerateChildrenOfType() : Returns all Bookmarks in the document. This method can be used on document elements other than RadDocument, in case you want to detect all bookmarks in a limited part of the document, e.g. a Paragraph or a Table.

You can also add Bookmarks in a document you are creating manually. As both BookmarkRangeStart and BookMarkRangeEnd inherit from Inline, they can be added to the Inlines property of a Paragraph, just like any other Inline. You can also have document positions go to the start or end of the Bookmark and perform non-standard operations.

For example, you can keep a Dictionary mapping each bookmark name to another string and substitute a bookmark with the corresponding text using the following method:

private void ReplaceContentOfBookmark(string bookmarkName) 
{ 
    BookmarkRangeStart bookmark = this.radRichTextBox.Document.GetBookmarkByName(bookmarkName); 
    this.radRichTextBox.Document.Selection.SelectAnnotationRange(bookmark); 
    this.radRichTextBox.Delete(false); 
    this.radRichTextBox.Insert(bookmarksToContent[bookmarkName]); 
} 

If you want to preserve the bookmarks in the document and only change the text between the BookmarkRangeStart and BookmarkRangeEnd document elements, you can do so like this:

private void ChangeAllBookmarks(RadRichTextBox radRichTextBox) 
{ 
    BookmarkRangeStart[] bookmarks = this.radRichTextBox.Document.GetAllBookmarks().ToArray<BookmarkRangeStart>(); 
    DocumentPosition start = new DocumentPosition(radRichTextBox.Document); 
    DocumentPosition end = new DocumentPosition(radRichTextBox.Document); 
    foreach (BookmarkRangeStart item in bookmarks) 
    { 
        radRichTextBox.Document.GoToBookmark(item); 
 
        start.MoveToInline(item.FirstLayoutBox as InlineLayoutBox, 0); 
        end.MoveToInline(item.End.FirstLayoutBox as InlineLayoutBox, 0); 
        start.MoveToNextInline(); 
        radRichTextBox.Document.Selection.SetSelectionStart(start); 
        radRichTextBox.Document.Selection.AddSelectionEnd(end); 
 
        radRichTextBox.Delete(false); 
 
        radRichTextBox.Insert(bookmarksToContent[item.Name]); 
    } 
} 
In this article