Skip to main content
A newer version of this page is available. .

Hyperlinks and Bookmarks

  • 6 minutes to read

The HyperlinkCollection contains all document hyperlinks. Use the SubDocument.Hyperlinks property to access a specific hyperlink by its index. Call the ReadOnlyHyperlinkCollection.Get method to retrieve the hyperlink related to the specified range.

Call the HyperlinkCollection.Create method to create a new Hyperlink.

The code snippet below finds a specific range in the document and converts it to a hyperlink with the specified URI and tooltip:

RichEditControl_Hyperlinks_Result

using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;

using (var wordProcessor = new RichEditDocumentServer())
{
  wordProcessor.LoadDocument("Documents//Document.docx");
  Document document = wordProcessor.Document;

  // Find a specific text string
  DocumentRange[] foundRanges =
          document.FindAll("DevExpress WinForms Rich Text Editor",
  SearchOptions.CaseSensitive);

  if (foundRanges.Length > 0)
  {
      // Convert the found range to a hyperlink
      Hyperlink hyperlink =  document.Hyperlinks.Create(foundRanges[0]);

      // Specify the URI and the tooltip
      hyperlink.NavigateUri =
           "https://www.devexpress.com/Products/NET/Controls/WinForms/Rich_Editor/";
      hyperlink.ToolTip = "WinForms Rich Text Editor";
  }
}

Tip

You can use the Create method to convert static URIs to hyperlinks. Refer to the following article for a code sample:

Read Tutorial: How to: Convert a Static URI to a Hyperlink

Use the HyperlinkOptions class properties to complete the following tasks:

The code snippet below shows how to specify these properties in code:

using XtraRichEdit.API.Native;

using (var wordProcessor = new RichEditDocumentServer())
{
    //...
    hyperlinkOptions = wordProcessor.Options.Hyperlinks;

    hyperlinkOptions.EnableUriCorrection = false;
    hyperlinkOptions.ModifierKeys = Keys.Shift;
    hyperlinkOptions.ShowToolTip = true;
}

Note

The HYPERLINK field represents hyperlinks in the RichEditDocumentServer, therefore the DocumentCapabilitiesOptions.Fields property affects the hyperlink loading process when importing the document. Set the RichEditControlCompatibility.LoadHyperlinkAsField property to false before calling the InitializeComponent method to correct this behavior and allow loading hyperlinks when loading fields are disabled.

Call the HyperlinkCollection.Remove to delete the hyperlink. The HyperlinkCollection.RemoveAt method deletes the hyperlink at the specified index in the collection.

The code sample below locates and removes all hyperlinks in the first section:

using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;

using (var wordProcessor = new RichEditDocumentServer())
{
    // Load a document
    wordProcessor.LoadDocument("Documents//Document.docx");
    Document document = wordProcessor.Document;

    // Obtain all hyperlinks in the first section
    var hyperlinks = document.Hyperlinks.Get(document.Sections[0].Range);
    if (hyperlinks != null)
    {
        // Remove all hyperlinks
        foreach (Hyperlink hyperlink in hyperlinks)
        {
            document.Hyperlinks.Remove(hyperlink);
        }
    }
}

Bookmarks

The BookmarkCollection contains all document bookmarks. Use the SubDocument.Bookmarks property to access a specific bookmark by its index. Call the ReadOnlyBookmarkCollection.Get method to retrieve the bookmark related to the specified range.

Create a Bookmark

The following code snippet creates a Bookmark and a hyperlink associated with this bookmark:

RichEditControl_Bookmarks_Gif

using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;

using (var wordProcessor = new RichEditDocumentServer())
{
    wordProcessor.LoadDocument("Documents//Document.docx");
    Document document = wordProcessor.Document;

    // Create a bookmark at the document start
    DocumentRange start = document.CreateRange(document.Range.Start, 1);
    document.Bookmarks.Create(start, "Top");

    // Insert the hyperlink anchored to the created bookmark
    DocumentRange[] foundRanges = document.FindAll("To the Top", SearchOptions.CaseSensitive);
    if (foundRanges.Length > 0)
    {
        Hyperlink hyperlink = document.Hyperlinks.Create(foundRanges[0]);
        hyperlink.Anchor = "Top";
    }
}

Note

If a new bookmark name duplicates the existing one, an InvalidOperationException exception is thrown with the message obtained from the XtraRichEditStringId.Msg_DuplicateBookmark resource string that reads “Bookmark with that name already exists in the document”. Use the BookmarkOptions.ConflictNameResolution property to process bookmarks with duplicate names.

Visualize the Bookmark

Use the following properties to highlight bookmarks:

With these properties specified, the bookmarks are displayed the following way:

XtraRichEdit_Bookmarks

using DevExpress.XtraRichEdit;
using System.Drawing;

using (var wordProcessor = new RichEditDocumentServer())
{
  //...
  BookmarkOptions bookmarkOptions = wordProcessor.Options.Bookmarks;
  bookmarkOptions.Visibility = RichEditBookmarkVisibility.Visible;
  bookmarkOptions.Color = Color.Sienna;
}

Export Bookmarks to the PDF format

You can specify what bookmarks can be displayed in the Bookmarks panel when you export the document to PDF format. Set the BookmarkOptions.DisplayBookmarksInPdfNavigationPane property to one of the DevExpress.XtraRichEdit.PdfBookmarkDisplayMode enumeration values, as shown below:

using DevExpress.XtraRichEdit;

using (var wordProcessor = new RichEditDocumentServer())
{
    //...
    BookmarkOptions bookmarkOptions = wordProcessor.Options.Bookmarks;
    bookmarkOptions.DisplayBookmarksInPdfNavigationPane =
         PdfBookmarkDisplayMode.TocBookmarks;
}

Use the BookmarkOptions.DisplayUnreferencedPdfBookmarks property to determine whether to show bookmarks without references (i.e., without hyperlinks anchored to these bookmarks) in the Bookmarks navigation pane. The DisplayBookmarksInPdfNavigationPane property set PdfBookmarkDisplayMode.None controls the unreferenced bookmarks when the DisplayUnreferencedPdfBookmarks property is set to true.

Remove a Bookmark

Utilize the BookmarkCollection.Remove method to delete the bookmark.

The code sample below shows how to locate and remove bookmarks in the 6th paragraph:

using DevExpress.XtraRichEdit.API.Native;

using (var wordProcessor = new RichEditDocumentServer())
{
  wordProcessor.LoadDocument("Documents//Document.docx");
  Document document = wordProcessor.Document;

  var bookmarks =
     document.Bookmarks.Get(document.Paragraphs[5].Range);
  if (bookmarks != null)
  {
      foreach (Bookmark bookmark in bookmarks)
      {
        document.Bookmarks.Remove(bookmark);
      }
  }
}