# Hyperlink

The hyperlink functionality in RadRichTextBox is encapusalted in the [HyperlinkInfo](https://www.telerik.com/products/wpf/documentation/controls/radrichtextbox/features/document-elements-hyperlink#hyperlinkinfo), [HyperlinkRangeStart and HyperlinkRangeEnd](https://www.telerik.com/products/wpf/documentation/controls/radrichtextbox/features/document-elements-hyperlink#hyperlinkrangestart-and-hyperlinkrangeend) classes.

## HyperlinkInfo

The `HyperlinkInfo` class allows you to easily insert hyperlinks in the current selection of the document. The class exposes the following properties:

* `IsAnchor`—A boolean property determining if the hyperlink points to a URL or a bookmark;

* `NavigateUri`—The URI of the hyperlink;

* `Target`—Determines if the link should be opened in another window (when set to `HyperlinkTargets.Blank`) or in the same frame (`HyperlinkTargets.Self`).

* `ToolTip`—A tooltip text for the hyperlink.

**Inserting a hyperlink pointing to a web address**

```C#
HyperlinkInfo info = new HyperlinkInfo()
{
    NavigateUri = "https://www.telerik.com/products/wpf/overview.aspx",
    Target = HyperlinkTargets.Blank,
    IsAnchor = false,
    ToolTip = "Telerik UI for WPF website"
};
	this.radRichTextBox.InsertHyperlink(info, "RichTextBox demo");
```

To insert a hyperlink to a bookmark within the document, specify the bookmark's name as the `NavigateUri` and set the `IsAnchor` to `true`.

**Inserting a hyperlink pointing to a bookmark in the document**

```C#
	HyperlinkInfo info = new HyperlinkInfo()
    {
        NavigateUri = "bookmark1",
        Target = HyperlinkTargets.Self,
        IsAnchor = true
    };
	this.radRichTextBox.InsertHyperlink(info, "Link to Bookmark 1");
```

The `InsertHyperlink` method provides the following overloads:

* `public void InsertHyperlink(HyperlinkInfo hyperlinkInfo, IEnumerable&lt;Inline&gt; inlines)`—Inserts a hyperlink which spans several different adjacent inlines, e.g. part of the hyperlink text is Bold or there is an image inside the hyperlink.

* `public void InsertHyperlink(HyperlinkInfo hyperlinkInfo)`—Creates a hyperlink from the currently selected part of the document.

To remove hyperlink from the document, position the caret within the hyperlink span and call the `RemoveHyperlink`.

**Removing the hyperlink under the caret position**

```C#
	this.radRichTextBox.RemoveHyperlink();
```

## HyperlinkRangeStart and HyperlinkRangeEnd

The `HyperlinkRangeStart` and `HyperlinkRangeEnd` are Inline DocumentElements and can be added to the inlines of a document when building the contents of a RadDocument in code-behind. Using these classes give a bit more control over the creation and spanning of hyperlinks.

To add a hyperlink using this API, add the range start object and then pair it with the range end. The information about the hyperlink is stored in a `HyperlinkInfo` object assigned to the `HyperlinkRangeStart`. Then, the range objects are added to the `Paragraph`'s `Inlines` collection.

**Adding a hyperlink that spans on multiple document elements**

```C#
	// creating the hyperlink range	
	HyperlinkRangeStart hyperlinkStart = new HyperlinkRangeStart();
        HyperlinkRangeEnd hyperlinkEnd = new HyperlinkRangeEnd();
        hyperlinkEnd.PairWithStart(hyperlinkStart);
	HyperlinkInfo hyperlinkInfo = new HyperlinkInfo() { NavigateUri = "http://telerik.com", Target = HyperlinkTargets.Blank };
        hyperlinkStart.HyperlinkInfo = hyperlinkInfo;
	
	// document setup
	RadDocument document = new RadDocument();
        Section section = new Section();
        Paragraph paragraph = new Paragraph();
        Span spanBefore = new Span("Text before the image ");
        ImageInline image = new ImageInline(new Uri("/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/32/Picture.png", UriKind.Relative));
        image.Size = new Size(32, 32);
        Span spanAfter = new Span(" and some text after the image");

        // inserting the hyper link range and the content where the hyper links spans
        paragraph.Inlines.Add(hyperlinkStart);
	paragraph.Inlines.Add(spanBefore);
	paragraph.Inlines.Add(image);
	paragraph.Inlines.Add(spanAfter);
	section.Blocks.Add(paragraph);
	Paragraph anotherParagraph = new Paragraph();
        anotherParagraph.Inlines.Add(new Span("Another paragraph here and still in hyperlink"));
	anotherParagraph.Inlines.Add(hyperlinkEnd);
	
	section.Blocks.Add(anotherParagraph);
	document.Sections.Add(section);	
	this.radRichTextBox.Document = document;
```

> caption The document created with the hyperlink range setup

![Hyperlink spanning on an image](https://www.telerik.com/products/wpf/documentation/assets/c43089165ead23d701798d2b1a680f4e/radrichtextbox_features_layoutelements_hyperlinks_imageinhyperlink.png)

The `HyperlinkRangeStart` and `HyperlinkRangeEnd` objects are used also when accessing and modifying the hyperlinks from the document.

**Deleting all hyperlinks in a document and replace them with text**

```C#
	IEnumerable<HyperlinkRangeStart> links = this.radRichTextBox.Document.EnumerateChildrenOfType<HyperlinkRangeStart>();
	foreach (HyperlinkRangeStart link in links)
	{
	    this.radRichTextBox.Document.CaretPosition.MoveToInline(link.FirstLayoutBox as InlineLayoutBox, 0);
	    this.radRichTextBox.Document.Selection.SelectAnnotationRange(link);
	    this.radRichTextBox.Delete(false);
	    this.radRichTextBox.Insert("Removed hyperlink");
	}
```

## Changing the Hyperlink ToolTip

By default the hyperlink tooltip displays the navigation Uri along with the navigation mode (Ctrl+Click or Click only). To change format used to display this information, set the `HyperlinkToolTipFormatString` property of `RadRichTextBox`. This will set the format for all hyperlinks in the document.

**Change the default hyperlink tooltip format**

```C#
	<telerik:RadRichTextBox HyperlinkToolTipFormatString = "{}Navigate to '{0}' by pressing {1}" />
```

![Showing the custom tooltip format](https://www.telerik.com/products/wpf/documentation/assets/17bf2ff81e527530cd80acfadf5c43bf/radrichtextbox_features_layoutelements_hyperlinks_hyperlinktooltipformatstring.png)

The tooltip can be customized also per hyperlink. To do this, set the `ToolTip` property of the corresponding `HyperlinkInfo` object.

**Inserting a hyperlink with a tooltip**

```C#
HyperlinkInfo info = new HyperlinkInfo()
    {
        NavigateUri = "https://www.telerik.com/products/wpf/overview.aspx",
	Target = HyperlinkTargets.Blank,
	IsAnchor = false,
	ToolTip = "Telerik UI for WPF product page. Use Ctrl+Click to open the page."
};
this.radRichTextBox.InsertHyperlink(info, "UI for WPF");
```

The `HyperlinkToolTipShowing` event can be used to further customize the tooltip content or cancel displaying it.

```C#
 private void RadRichTextBox_HyperlinkToolTipShowing(object sender, Telerik.Windows.Documents.UI.HyperlinkToolTipShowingEventArgs e)
{
    HyperlinkRangeStart hyperlinkAnnotationRangeStart = e.HyperlinkAnnotationMarker;
    HyperlinkInfo hyperlinkInfo = hyperlinkAnnotationRangeStart.HyperlinkInfo;
    string originalToolTip = e.ToolTip;

    e.ToolTip = "New tooltip value";

    // set e.Cancel = true; to cancel the tooltip showing
}
```

## Hyperlink Navigation Trigger

The trigger that navigates to the corresponding Uri is controlled by the `HyperlinkNavigationMode` property of `RadRichTextBox`. The mode can be set to one of the following values:

* `CtrlClick`—Triggers the hyperlink when users hold the Ctrl key and click on the hyperlink.
* `Click`—Triggers the hyperlink when users click on the hyperlink.

**Change the default hyperlink navigation mode**

```C#
this.radRichTextBox.HyperlinkNavigationMode = Telerik.Windows.Documents.UI.HyperlinkNavigationMode.Click;
```

## Handling the Hyperlink Click Navigation

When a hyperlink is clicked, the `HyperlinkClicked` event of `RadRichTextBox` is raised. The sender of the event is the document element, which the user has clicked on, e.g. a `Span`, an `Image`, `InlineUIContainer`, etc. The event args provide the possibility to cancel or replace the navigation action. This is helpful when you need to validate the clicked hyperlink and prevent it from navigating to an unsecure address or from starting a local process.

Set `Handled` to `true` to cancel the default navigation. `HyperlinkClickedEventArgs` derives from `HyperlinkClickedEventArgsBase` and exposes the URL through `Url` (not `URL`), along with `IsTrustedUrl` and `Handled`. The `URL` to `Url` rename is a breaking API change; update code that reads `HyperlinkClickedEventArgs.URL`.

**Prompting that the clicked linked may be unsafe and allows it to cancel the navigation action**

```C#
private void RadRichTextBox_HyperlinkClicked(object sender, HyperlinkClickedEventArgs e)
{
    var link = e.URL;
    if (link.EndsWith("exe"))
    {
        e.Handled = true;
        MessageBoxResult Result = MessageBox.Show("You are about to open an executable file. Do you want to proceed", "Possible unsafe link", MessageBoxButton.YesNo, MessageBoxImage.Question);

        if (Result == MessageBoxResult.Yes)
        {
            Process.Start(new ProcessStartInfo()
            {
                FileName = link,
                UseShellExecute = true
            });
        }
    }
}
```

The validator trusts absolute HTTP, HTTPS, and mailto URLs by default when they pass its pattern and scheme checks. Set `IsTrustedUrl` to `true` in an event handler to allow the default launch of a URL the validator would otherwise reject.

## Handling Hyperlink Requests Globally

RichTextBox already validates external URLs before launch; the shared event does not introduce that validation. The existing document- and editor-specific `HyperlinkClicked` handlers run before `HyperlinkManager.HyperlinkClicked`. The shared event runs if those handlers do not set `Handled` to `true`. Its event arguments expose `Url`, `IsTrustedUrl`, and `Handled`; its `sender` identifies the originating component.

Set `Handled` to `true` in either event to cancel navigation. Use the shared event to handle hyperlink requests from multiple supported controls in one place. Because `HyperlinkManager.HyperlinkClicked` is static, unsubscribe when the handler owner has a shorter lifetime than the application. See [Handling hyperlink navigation in RadHyperlinkButton](https://www.telerik.com/products/wpf/documentation/controls/radbuttons/features/hyperlink-button) for a shared-event example.

## Customizing the Hyperlink Visual

By default, when the users insert a hyperlink in RadRichTextBox, it is styled with the built-in Hyperlink style. To change the default appearance of the hyperlinks in a document, modifying the [associated style](https://www.telerik.com/products/wpf/documentation/controls/radrichtextbox/features/styles/styles).

#### **Change the default hyperlink style**

```
StyleDefinition hyperlinkStyle = this.radRichTextBox.Document.StyleRepository[RadDocumentDefaultStyles.HyperlinkStyleName];
hyperlinkStyle.SpanStyle.FontSize = 20; 
hyperlinkStyle.SpanStyle.ForeColor = Colors.Green;
hyperlinkStyle.SpanStyle.UnderlineDecoration = UnderlineTypes.None;
```

> caption A hyperlink with customized font styles

![Custom style for hyperlink in RadRichTextBox](https://www.telerik.com/products/wpf/documentation/assets/a90f29f68058410882c13dc112c5b9dd/radrichtextbox_features_layoutelements_hyperlinks_customstyle.png)

## See Also

* [Elements Hierarchy](https://www.telerik.com/products/wpf/documentation/controls/radrichtextbox/document-elements/features-hierarchy)
* [RadDocument](https://www.telerik.com/products/wpf/documentation/controls/radrichtextbox/document-elements/features-raddocument)
* [Section](https://www.telerik.com/products/wpf/documentation/controls/radrichtextbox/document-elements/features-section)
* [Paragraph](https://www.telerik.com/products/wpf/documentation/controls/radrichtextbox/document-elements/features-paragraph)
* [Span](https://www.telerik.com/products/wpf/documentation/controls/radrichtextbox/document-elements/features-span)
* [InlineImage](https://www.telerik.com/products/wpf/documentation/controls/radrichtextbox/document-elements/features-inlineimage)
* [Telerik UI for WPF API Reference](https://docs.telerik.com/devtools/wpf/api/)
