Link Annotations
The PdfViewer provides read-only support for link annotations. This means that if you open a PDF file that includes hyperlinks to absolute URIs and click them, a window navigates to the respective address.
In addition, you can include links to bookmarks in the same document and, when clicked, the viewport will scroll to the respective destination within the document.
Link annotations are part of the Telerik RadPdfProcessing library which is used for the document model of the PdfViewer.
A clicked link annotation
Getting Link Annotations
To get the link annotations in the document, use the Annotation
collection property of the RadFixedDocument
.
Get the link annotations
IEnumerable<Link> annotations = this.pdfViewer.Document.Annotations.OfType<Link>();
// The address of the link annotation can be access through its Action property.
Link link = annotations.ElementAt(0);
UriAction action = (UriAction)link.Action;
Uri address = action.Uri;
Destination destination = action.Destination;
Action
property. The Uri
property of the action tells what is the external address that the annotation click will open. The Destination
points to a bookmark within the loaded document.
Customizing Link Click Action
To replace or cancel the action that happens on link click, use the AnnotationClick
or HyperlinkClick
events of RadPdfViewer
. These events are useful in order to further improve the security or the behavior of the links in the document.
Cancel the link click navigation
private void PdfViewer_AnnotationClicked(object sender, Telerik.Windows.Documents.Fixed.Model.Annotations.EventArgs.AnnotationEventArgs e)
{
// Cancel the go-to action of the link.
e.Handled = true;
RadFixedPage page = e.Page;
Link link = (Link)e.Annotation;
// Here you can implement any custom logic that will happen on annotation click.
}
Using the HyperlinkClicked event to ask the customer if wants to open the hyperlink
private void RadPdfViewer_HyperlinkClicked(object sender, HyperlinkClickedEventArgs e)
{
if (e.Url.EndsWith("exe"))
{
e.Handled = true;
ContentDialog dialog = new ContentDialog();
dialog.XamlRoot = owner.XamlRoot;
dialog.Title = "Possible unsafe link";
dialog.Content = "You are about to open an executable file. Do you want to proceed? - " + e.Url;
dialog.PrimaryButtonText = "Allow";
dialog.SecondaryButtonText = "Block";
ContentDialogResult result = dialog.ShowAsync();
if (result == ContentDialogResult.Primary)
{
//navigate to the e.Url link manually
}
}
}
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
.