New to Telerik UI for WinFormsStart a free 30-day trial

Annotations

Updated over 6 months ago

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, and navigate to the respective address. In addition, if there are links pointing to bookmarks in the same document, the view port will be scrolled to the destination specified in the link.

The current API includes the following members, which allow customization of the default behavior or implementation of custom logic:

  • 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 as property and the Link itself has information of its Action, i.e. if it is a UriAction. 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 behavior.

AnnotationClicked Event Handler

C#
        
private void radPdfViewer1_AnnotationClicked(object sender, Telerik.Windows.Documents.Fixed.Model.Annotations.EventArgs.AnnotationEventArgs e)
{
    Telerik.Windows.Documents.Fixed.Model.Annotations.Link l = e.Annotation as Telerik.Windows.Documents.Fixed.Model.Annotations.Link;
    if (l == null)
    {
        return;
    }
    Telerik.Windows.Documents.Fixed.Model.Actions.UriAction a = l.Action as Telerik.Windows.Documents.Fixed.Model.Actions.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. With the 2024 Q3 (2024.3.924), the default navigation behavior of the hyperlinks is to automatically open only valid and trusted addresses. If needed, the navigation can be canceled by either setting the Handled property of the event args to true or the IsTrustedUrl property to false. Below is an example of using this event to prompt that the clicked hyperlink might be unsafe and provide the opportunity to cancel the navigation process upon receiving the end user confirmation:

HyperlinkClicked Event Handler

C#
private void RadPdfViewer1_HyperlinkClicked(object sender, Telerik.WinControls.Hyperlinks.HyperlinkClickedEventArgs e)
{
    var link = e.URL;
    if (link.EndsWith("exe"))
    {
        e.Handled = true; MessageBoxResult Result = System.Windows.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
            });
        }
    }
}
  • Annotations property of RadFixedDocument – A collection which returns all annotations in the document. For example, you can retrieve all links using the following code:
C#
        
private IEnumerable<Telerik.Windows.Documents.Fixed.Model.Annotations.Link> GetAllLinks(Telerik.Windows.Documents.Fixed.Model.RadFixedDocument document)
{
    foreach (Telerik.Windows.Documents.Fixed.Model.Annotations.Annotation a in document.Annotations)
    {
        Telerik.Windows.Documents.Fixed.Model.Annotations.Link l = a as Telerik.Windows.Documents.Fixed.Model.Annotations.Link;
        if (l != null)
        {
            yield return l;
        }
    }
}

The bookmarks in terms of “docx bookmarks” are not explicitly saved in PDF files. They are persisted only if there are Link annotations to them, so you can use the snippet below to retrieve all destinations that have links to them:

Get Annotation Bookmarks

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

In this way, creating some UI containing all bookmarks would be possible. Then, you could implement the same action as the one being executed when a hyperlink is clicked, i.e. scroll the document to the specific place in the document where the destination of the link is placed. The following code can be used for this purpose – navigating to a specific destination:

C#
        
private void GoToDestination(Telerik.Windows.Documents.Fixed.Model.Navigation.Destination destination)
{
    this.radPdfViewer1.PdfViewerElement.GoToDestination(destination);
}

See Also

In this article
See Also
Not finding the help you need?
Contact Support