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 ofRadPdfViewer
—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. TheAnnotationEventArgs
contains theAnnotation
andLink
objects. The action of the clicked annotation can be determined by theAction
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
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 ofRadPdfViewer
—This event is similar toAnnotationClicked
, 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. TheHyperlinkClickedEventArgs
gives access to the URL, which can be manually checked if it is trusted. In case the navigation should be canceled, set theHandled
property of the event args totrue
or theIsTrustedUrl
property tofalse
.Using the HyperlinkClicked event to ask the customer if wants to open the hyperlink
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 }); } } }
Accessing the Annotations Collection
The Annotations
collection property of RadFixedDocument
provides access to all annotations in the document.
Accessing all annotations in the document
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
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;
}
}
}
Using the GoToDestination method
private void GoToDestination(Destination destination)
{
this.pdfViewer.GoToDestination(destination);
}
Destination
object to be existing. You can create one and manually adjust the scroll position.
Creating a Destination object manually
Destination destination = new Location() { Page = this.pdfViewer.Document.Pages[2], Left = 0, Top = 0, Zoom = 1 };
this.pdfViewer.GoToDestination(destination);
GoToPage(int pageNumber)
when pageNumber = 2, but when using Destination
objects you have more control in terms of the horizontal and vertical offsets.