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

If you want to digitally sign a document that doesn’t yet contain a SignatureFormField, 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

@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

@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

@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 from happening.
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 for information on how to create digital signatures.