Link Annotations

PSPDFKit supports all common types of link annotations. (Page Links, Web/Email links). Tapping such a link will automatically invoke the saved action and invoke the correct task, 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 listener 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.

Copy
MyCustomActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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;
}
Copy
MyCustomActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@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 will keep PSPDFKit from invoking the default handler.