How to get the location of an ink signature for digital signing?

Q: How to get the location of an ink signature for digital signing?

If you want to digitally sign a document that doesn't contain a SignatureFormField yet, you can add one at the location of an ink signature. To get the location of an ink signature you have three options.

Finding an Already Added Ink Signature

Copy
1
2
3
4
5
6
7
8
9
10
11
12
@Override
public void onDocumentLoaded(@NonNull PdfDocument document) {
    // This will return the first ink signature found in the document.
    document.getAnnotationProvider().getAllAnnotationsOfType(EnumSet.of(AnnotationType.INK))
        .cast(InkAnnotation.class)
        .filter(InkAnnotation::isSignature)
        .firstElement()
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(inkAnnotation -> {
            digitallySignAtLocation(inkAnnotation.getBoundingBox());
        });
}

Finding an Ink Signature That Was Just Added

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Override
public void onDocumentLoaded(@NonNull PdfDocument document) {
    super.onDocumentLoaded(document);
    document.getAnnotationProvider().addOnAnnotationUpdatedListener(new AnnotationProvider.OnAnnotationUpdatedListener() {
        @Override
        public void onAnnotationCreated(@NonNull Annotation annotation) {
            if (annotation instanceof InkAnnotation &&
                ((InkAnnotation) annotation).isSignature()) {
                // This will be called when the user adds a new ink signature.
                digitallySignAtLocation(annotation.getBoundingBox());
            }
        }

        @Override
        public void onAnnotationUpdated(@NonNull Annotation annotation) {

        }

        @Override
        public void onAnnotationRemoved(@NonNull Annotation annotation) {

        }
    });
}                                                            

Finding an Ink Signature That the User Selected

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@Override
public void onDocumentLoaded(@NonNull PdfDocument document) {
    super.onDocumentLoaded(document);
    getPdfFragment().addOnAnnotationSelectedListener(new AnnotationManager.OnAnnotationSelectedListener() {
        @Override
        public boolean onPrepareAnnotationSelection(@NonNull AnnotationSelectionController controller,
                                                    @NonNull Annotation annotation,
                                                    boolean annotationCreated) {
            // You can return false to prevent the default annotation selection to happen.
            return true;
        }

        @Override
        public void onAnnotationSelected(@NonNull Annotation annotation, boolean annotationCreated) {
            if (annotation instanceof InkAnnotation &&
                ((InkAnnotation) annotation).isSignature()) {
                // This will be called when the user selects an ink signature.
                digitallySignAtLocation(annotation.getBoundingBox());
            }
        }
    });
}

See here on how to create digital signatures.