Link Annotations
PSPDFKit for Android supports common types of link annotations, including page, web, and email links. Tapping on such a link will automatically invoke the saved action (see PDF Actions), as well as the correct event, such as changing the page or opening a URL.
Custom Link Handling
You can add custom URI handling to your application using the DocumentListener#onPageClick
method (see the Document Listeners guide for more information). The following code snippet can be used to detect any custom URI, like custom://my.custom.uri/with/1/parameter
, or a standard web URL, like https://www.pspdfkit.com
:
override fun onPageClick(document: PdfDocument, pageIndex: Int, event: MotionEvent?, pagePosition: PointF?, clickedAnnotation: Annotation?): Boolean { var handled = false; // If no annotation was clicked, `clickedAnnotation` may be null. // Otherwise, check if the clicked annotation is a link annotation. if (clickedAnnotation is LinkAnnotation && clickedAnnotation.action is UriAction) { // Extract the URI of the annotation action. val uri : Uri = Uri.parse((clickedAnnotation.action as UriAction).uri) // You can now check if you would like to handle the URI yourself, e.g. by using // a `UriMatcher` or some custom logic. if (isCustomUri(uri)) { Toast.makeText(this, "We have detected a custom Uri!", Toast.LENGTH_SHORT).show(); handled = true; } } return handled; }
@Override public boolean onPageClick(@NonNull PdfDocument document, int pageIndex, @Nullable MotionEvent event, @Nullable PointF pagePosition, @Nullable Annotation clickedAnnotation) { boolean handled = false; // If no annotation was clicked, `clickedAnnotation` may be null. // Otherwise, check if the clicked annotation is a link annotation. if (clickedAnnotation != null && clickedAnnotation instanceof LinkAnnotation) { LinkAnnotation linkAnnotation = (LinkAnnotation) clickedAnnotation; // Every link annotation may have an action defined. final Action action = linkAnnotation.getAction(); // Check if an action exists and if it is a `UriAction`. if (action != null && action instanceof UriAction) { // Extract the URI of the annotation action. Uri uri = Uri.parse(((UriAction) action).getUri()); // You can now check if you would like to handle the URI yourself, e.g. by using // a `UriMatcher` or some custom logic. if (isCustomUri(uri)) { Toast.makeText(this, "We have detected a custom Uri!", Toast.LENGTH_SHORT).show(); handled = true; } } } return handled; }
ℹ️ Note: Return true
in onPageClick
if you have handled the annotation click yourself. This will consume the event and keep PSPDFKit from invoking the default handler.