Annotations
RadPdfViewer supports link annotations. External URI annotations open only when the destination passes both link-format and URI-scheme validation, or an application handler explicitly approves it. The default allowed schemes are http, https, and mailto. Links pointing to destinations in the same PDF document scroll the viewport internally.
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
private void radPdfViewer1_AnnotationClicked(object sender, Telerik.Documents.Fixed.Model.Annotations.EventArgs.AnnotationEventArgs e)
{
Telerik.Documents.Fixed.Model.Annotations.Link l = e.Annotation as Telerik.Documents.Fixed.Model.Annotations.Link;
if (l == null)
{
return;
}
UriAction a = l.Actions[0] as UriAction;
if (a == null)
{
return;
}
System.Windows.MessageBox.Show(a.Uri.ToString());
e.Handled = true;
}
RadPdfViewer.HyperlinkClickedis raised for URI annotation navigation. It usesTelerik.WinControls.Hyperlinks.HyperlinkClickedEventArgs, which exposesURL,IsTrustedUrl, andHandled. SetIsTrustedUrltotrueonly after applying your own destination validation. SetHandledtotrueto replace or suppress navigation.
If the local event leaves the request unhandled, HyperlinkHelper.HyperlinkClicked runs next. An unhandled request whose final IsTrustedUrl is false displays the localized invalid-link message. Setting Handled to true suppresses both the launch and the message. FTP, file/UNC, OneNote, and custom-protocol destinations are not automatically approved. See shared hyperlink validation for the default policy and handling links across controls for a global approval example.
The following example prompts the user before continuing navigation:
HyperlinkClicked Event Handler
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:
Get Annotation Links
private IEnumerable<Telerik.Documents.Fixed.Model.Annotations.Link> GetAllLinks(Telerik.Documents.Fixed.Model.RadFixedDocument document)
{
foreach (Telerik.Documents.Fixed.Model.Annotations.Annotation a in document.Annotations)
{
Telerik.Documents.Fixed.Model.Annotations.Link l = a as Telerik.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
private IEnumerable<Telerik.Documents.Fixed.Model.Navigation.Destination> GetAllBookmarks(Telerik.Documents.Fixed.Model.RadFixedDocument document)
{
foreach (Telerik.Documents.Fixed.Model.Annotations.Annotation a in document.Annotations)
{
Telerik.Documents.Fixed.Model.Annotations.Link l = a as Telerik.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:
Navigate to Destination
private void GoToDestination(Telerik.Documents.Fixed.Model.Navigation.Destination destination)
{
this.radPdfViewer1.PdfViewerElement.GoToDestination(destination);
}