# Annotations

RadPdfViewer supports link annotations, which means that if you open a PDF file that includes hyperlinks to absolute URIs, you can click them and have a window open, navigated to the respective address. In addition, if there are links pointing to bookmarks in the same document, the viewport will be scrolled to the destination specified in the link.

## Annotation and Hyperlink Click Events

* `AnnotationClicked` event of `RadPdfViewer`—This event is fired when you click on an annotation such as a hyperlink.  It comes in handy when you want to detect or even cancel the opening of a web page. The `AnnotationEventArgs` contains the `Annotation` and `Link` objects. The action of the clicked annotation can be determined by the `Action` property of the event args. Handling the event in the following manner will not only show the URI of each clicked link as the text of a MessageBox, but will also cancel the default navigation behavior.

  **Defining the AnnotationClicked event handler**

  ```C#
  private void viewer_AnnotationClicked(object sender, AnnotationEventArgs e)
  {
      Link l = e.Annotation as Link;
      if (l == null)
      {
          return;
      }
      UriAction a = l.Action as UriAction;
      if (a == null)
      {
          return;
      }
      MessageBox.Show(a.Uri.ToString());
      e.Handled = true;
  }
  ```

* `HyperlinkClicked` event of `RadPdfViewer`—This event is similar to `AnnotationClicked`, but it is raised only when you click on the **hyperlink** type annotations. It allows you to cancel the navigation to the associated URI or to modify the click action. The `HyperlinkClickedEventArgs` gives access to the URL, which can be manually checked if it is trusted. In case the navigation should be canceled, set the `Handled` property of the event args to `true` or the `IsTrustedUrl` property to `false`.

  **Using the HyperlinkClicked event to ask the customer if wants to open the hyperlink**

  ```C#
  private void RadPdfViewer_HyperlinkClicked(object sender, Telerik.Windows.Documents.Fixed.HyperlinkClickedEventArgs e)
  {
      if (e.Url.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 = e.Url,
                  UseShellExecute = true
              });
          }
      }
  }
  ```

## Handling Hyperlink Requests Globally

`RadPdfViewer` validates external URLs before launching them. The shared `HyperlinkManager.HyperlinkClicked` event adds a global handling point alongside the viewer-specific `HyperlinkClicked` event exposed through `FixedDocumentViewerBase` and `RadBookFixedDocumentViewer`.

The viewer-specific event runs first. If its handler leaves `Handled` as `false`, the shared event runs next. Set `Handled` to `true` to cancel the default action, or set `IsTrustedUrl` to `true` to allow the default launch for a URL the validator would otherwise reject. The shared event arguments expose `Url`, `IsTrustedUrl`, and `Handled`. The `sender` identifies the originating viewer instance.

Absolute HTTP, HTTPS, and mailto URLs can be trusted by default when they pass the validator's pattern and scheme checks. Unsubscribe from the static `HyperlinkManager.HyperlinkClicked` event 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 code example.

## Accessing the Annotations Collection

The `Annotations` collection property of `RadFixedDocument` provides access to all annotations in the document.

**Accessing all annotations in the document**

```C#
private IEnumerable<Link> GetAllLinks(RadFixedDocument document)
{
    foreach (Annotation a in document.Annotations)
    {
        Link l = a as Link;
        if (l != null)
        {
            yield return l;
        }
    }
}
```

## Working with Bookmarks

The bookmarks in terms of “docx bookmarks” are not explicitly saved in PDF files. They are persisted only if there are link annotations assigned to them. The following example shows how to retrieve all bookmark destinations that have links anchored to them.

**Accessing annotations containing bookmarks**

```C#
private IEnumerable<Destination> GetAllBookmarks(RadFixedDocument document)
{
    foreach (Annotation a in document.Annotations)
    {
        Link l = a as Link;
        if (l != null && l.Destination != null)
        {
            yield return l.Destination;
        }
    }
}
```

The following example shows how to manually navigate to a bookmark. This idea can be used to implement external auto-[scrolling](https://www.telerik.com/products/wpf/documentation/controls/radpdfviewer/scrolling) logic (like a table of content UI).

**Using the GoToDestination method**

```C#
private void GoToDestination(Destination destination)
{
    this.pdfViewer.GoToDestination(destination);
}
```

It is not necessary for the `Destination` object to be existing. You can create one and manually adjust the scroll position.

**Creating a Destination object manually**

```C#
Destination destination = new Location() { Page = this.pdfViewer.Document.Pages[2], Left = 0, Top = 0, Zoom = 1 };
this.pdfViewer.GoToDestination(destination);
```

The code above will scroll the document to bring the top-left corner of the third page (indexing starts at 0) into view. The same scenario can be implemented using the `GoToPage(int pageNumber)` when pageNumber = 2, but when using `Destination` objects you have more control in terms of the horizontal and vertical offsets.

## See Also

* [RadFixedDocument](https://www.telerik.com/products/wpf/documentation/controls/radpdfviewer/document-model/radfixeddocument)
* [Telerik UI for WPF API Reference](https://docs.telerik.com/devtools/wpf/api/)
