Link Annotations
The Telerik .NET MAUI PDF Viewer supports link annotations. This feature allows the user to tap any hyperlink that leads to an absolute URI, and the link will open in the browser. In addition, if the PDF contains links that point to bookmarks in the same document, the viewport scrolls to the destination specified in the link.
The PDF Viewer provides the LinkAnnotationTapped
event, which allows you to implementing custom logic related to the links in the PDF document:
-
LinkAnnotationTapped
—Occurs 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 LinkAnnotationTapped
event handler receives two parameters:
- The
sender
argument, which is of typeobject
, but can be cast to theRadPdfViewer
type. - A
LinkAnnotationTappedEventArgs
object, which provides the following properties:- The
LinkAnnotation
property—It contains the link tapped by the user. TheLinkAnnotation
also holds information about the requested action:-
UriAction
—Open a URI in the browsers. -
GoToAction
—Move the viewport to position in the same document.
-
- The
Handled
boolean property—A cancellation option for the opening of a page.
- The
Link Annotations Example
The example below demonstrates how to detect whether the link annotation leads to a URI or a concrete position in the document, and cancel the navigation in the first case.
You can implement additional logic for requesting a confirmation from the end user whether to navigate outside of the app in the case of a hyperlink.
1. Add the PDF Viewer definition with the event:
<telerik:RadPdfViewer x:Name="pdfViewer"
LinkAnnotationTapped="LinkTapped" />
2. Add the LinkAnnotationTapped
event handler:
private void LinkTapped(object sender, Telerik.Maui.Controls.PdfViewer.Annotations.LinkAnnotationTappedEventArgs e)
{
if (e.LinkAnnotation.Action is UriAction uriAction)
{
e.Handled = true;
Application.Current.MainPage.DisplayAlert("Confirm", "Are you sure you want to navigate", "Yes", "No").ContinueWith(t =>
{
bool shouldNavigateAway = t.Status == TaskStatus.RanToCompletion ? t.Result : false;
if (shouldNavigateAway)
{
Dispatcher.Dispatch(() =>
{
Launcher.OpenAsync(uriAction.Uri);
});
}
});
}
}
The following image shows the result from tapping a hyperlink on different platforms:
For a runnable example with the PDF Viewer Annotations, see the SDKBrowser Demo Application and go to PdfViewer > Features.