Bookmark Class

A bookmark in a document.

Namespace: DevExpress.Blazor.RichEdit

Assembly: DevExpress.Blazor.RichEdit.v21.2.dll

Declaration

public class Bookmark :
    DocumentElementBase

Remarks

A bookmark is an anchor in a document. Users can navigate to this anchor when they click a corresponding hyperlink.

Create a Bookmark

Call a Bookmarks.CreateAsync method to add a bookmark to a sub-document.

<DxRichEdit @ref="@richEdit" />

@code {
    DxRichEdit richEdit { get; set; }
    @* ... *@
    /* Surround the code that contains an asynchronous operation with a try-catch block to handle
    the OperationCanceledException. This exception is thrown when an asynchronous operation is cancelled. */
        try {
        @* ... *@
            // Create a bookmark
            var bookmarkTextSpan = await richEdit.DocumentAPI.AddTextAsync("Chapter 1");
            var bookmarkChapter1 = await richEdit.DocumentAPI.Bookmarks.CreateAsync(bookmarkTextSpan.Interval, "chapter1Bookmark");
            @* ... *@
        }
        catch (OperationCanceledException e) {
            Console.WriteLine($"{nameof(OperationCanceledException)} thrown with message: {e.Message}");
        }
}

Call a Hyperlinks.CreateAsync method to create a hyperlink that navigates to the bookmark.

// Create a hyperlink to the bookmark
var hlChapter1Span = await richEdit.DocumentAPI.AddTextAsync("To learn more, see the Chapter 1");
await richEdit.DocumentAPI.Hyperlinks.CreateAsync(hlChapter1Span.Interval, "", bookmarkChapter1.Name, "Go to Chapter 1");

Find a Bookmark

Use the following methods to find a bookmark:

  • GetAsync(String) - finds a bookmark by name

    // Find a bookmark by its name and get the bookmark's interval
    var bookmark = await richEdit.DocumentAPI.Bookmarks.GetAsync("chapter1Bookmark");
    if (bookmark != null) {
        var bookmarkInterval = bookmark.Interval;
    }
    
  • GetAsync(Int32) - finds a bookmark by index in the collection

    var firstBookmark = await richEdit.DocumentAPI.Bookmarks.GetAsync(0);
    
  • GetAllAsync() - gets the collection of the bookmarks

    var bookmarks = await richEdit.DocumentAPI.Bookmarks.GetAllAsync();
    

Remove a Bookmark

Use the following methods to remove a bookmark:

  • RemoveAsync(Bookmark) - removes the specified bookmark.

    // Remove the first bookmark
    var firstBookmark = await richEdit.DocumentAPI.Bookmarks.GetAsync(0);
    await richEdit.DocumentAPI.Bookmarks.RemoveAsync(firstBookmark);
    
  • RemoveAsync(Int32) - removes a bookmark with the specified index.

    // Remove all bookmarks from the document
    var bookmarks = await richEdit.DocumentAPI.Bookmarks.GetAllAsync();
    while (bookmarks.Count > 0) {
        await richEdit.DocumentAPI.Bookmarks.RemoveAsync(0);
        bookmarks = await richEdit.DocumentAPI.Bookmarks.GetAllAsync();
    }
    
  • RemoveAsync(String) - removes a bookmark with the specified name.

    // Remove a bookmark by a name
    await richEdit.DocumentAPI.Bookmarks.RemoveAsync("chapter1Bookmark");
    

Inheritance

See Also