How to retrieve all signatures in a signed document
This guide outlines how to retrieve signatures in a signed document.
Finding all digital signatures
If you want to retrieve all the digital signatures in document, you can retrieve all the signed form fields in the following way:
override fun onDocumentLoaded(document: PdfDocument) { // This will return all signed signature form fields in the document. val signatureFormFields = document.documentSignatureInfo.signatureFormFields
for (signatureFormField in signatureFormFields) { val signatureInfo = signatureFormField.signatureInfo // Process the signature information. ... }}@Overridepublic void onDocumentLoaded(@NonNull PdfDocument document) { // This will return all signed signature form fields in the document. List<SignatureFormField> signatureFormFields = document.getDocumentSignatureInfo().getSignatureFormFields();
for (SignatureFormField signatureFormField : signatureFormFields) { DigitalSignatureInfo signatureInfo = signatureFormField.getSignatureInfo(); // Process the signature information. ... }}See here for information on how to create digital signatures.
Finding all form fields with overlapping ink signatures
If you want to find signature form fields that aren’t digitally signed but that contain an ink or stamp signature, use the following snippet:
lifecycleScope.launch { // This will return all form elements in the document. val formElements = document.formProvider.getFormElements() // Filter all signature form elements and cast them. .filterIsInstance<SignatureFormElement>() // This will create a list of all form elements with a signature on them. .filter { signatureFormElement -> signatureFormElement.getOverlappingSignature() != null } // A list of all form elements with ink or stamp signatures on them, or an empty list if there are none.}// `getFormElements()` and `getOverlappingSignature()` are suspend functions. `runBlocking()` waits// for them to finish, so only call this on a worker thread.try { // This will return all form elements in the document. List<? extends FormElement> allFormElements = BuildersKt.runBlocking( EmptyCoroutineContext.INSTANCE, (scope, continuation) -> document.getFormProvider().getFormElements(continuation));
List<SignatureFormElement> formElements = new ArrayList<>(); for (FormElement formElement : allFormElements) { // Filter all signature form elements and cast them. if (!(formElement instanceof SignatureFormElement)) continue; SignatureFormElement signatureFormElement = (SignatureFormElement) formElement;
// This will create a list of all form elements with a signature on them. Annotation signature = BuildersKt.runBlocking( EmptyCoroutineContext.INSTANCE, (scope, continuation) -> signatureFormElement.getOverlappingSignature(continuation)); if (signature != null) { formElements.add(signatureFormElement); } } // A list of all form elements with ink or stamp signatures on them, or an empty list if there are none.} catch (InterruptedException e) { Thread.currentThread().interrupt();}To call suspend functions from Java without blocking a thread, see calling asynchronous APIs from Java.