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 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 implementing custom logic:
- AnnotationClicked event of RadPdfViewer: This event is fired when you click on an annotation such as a hyperlink. It comes handy when you want to detect or even cancel the opening of a web page. The AnnotationEventArgs contain 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.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;
}
Private Sub radPdfViewer1_AnnotationClicked(sender As Object, e As Telerik.Windows.Documents.Fixed.Model.Annotations.EventArgs.AnnotationEventArgs)
Dim l As Telerik.Windows.Documents.Fixed.Model.Annotations.Link = TryCast(e.Annotation, Telerik.Windows.Documents.Fixed.Model.Annotations.Link)
If l Is Nothing Then
Return
End If
Dim a As Telerik.Windows.Documents.Fixed.Model.Actions.UriAction = TryCast(l.Action, Telerik.Windows.Documents.Fixed.Model.Actions.UriAction)
If a Is Nothing Then
Return
End If
MessageBox.Show(a.Uri.ToString())
e.Handled = True
End Sub
- 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.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;
}
}
}
Private Iterator Function GetAllLinks(document As Telerik.Windows.Documents.Fixed.Model.RadFixedDocument) As IEnumerable(Of Telerik.Windows.Documents.Fixed.Model.Annotations.Link)
For Each a As Telerik.Windows.Documents.Fixed.Model.Annotations.Annotation In document.Annotations
Dim l As Telerik.Windows.Documents.Fixed.Model.Annotations.Link = TryCast(a, Telerik.Windows.Documents.Fixed.Model.Annotations.Link)
If l IsNot Nothing Then
Yield l
End If
Next
End Function
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.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;
}
}
}
Private Iterator Function GetAllBookmarks(document As Telerik.Windows.Documents.Fixed.Model.RadFixedDocument) As IEnumerable(Of Telerik.Windows.Documents.Fixed.Model.Navigation.Destination)
For Each a As Telerik.Windows.Documents.Fixed.Model.Annotations.Annotation In document.Annotations
Dim l As Telerik.Windows.Documents.Fixed.Model.Annotations.Link = TryCast(a, Telerik.Windows.Documents.Fixed.Model.Annotations.Link)
If l IsNot Nothing AndAlso l.Destination IsNot Nothing Then
Yield l.Destination
End If
Next
End Function
In this way it would be possible to create some UI that contains all bookmarks. 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.Windows.Documents.Fixed.Model.Navigation.Destination destination)
{
this.radPdfViewer1.PdfViewerElement.GoToDestination(destination);
}
Private Sub GoToDestination(destination As Telerik.Windows.Documents.Fixed.Model.Navigation.Destination)
Me.RadPdfViewer1.PdfViewerElement.GoToDestination(destination)
End Sub