How to verify signatures with predicate functions
Leave feedback
On this page
GroupDocs.Signature supports predicate functions (java.util.function.Predicate<BaseSignature>) in the verification process. The Signature class provides two verify overloads that take a predicate:
Both methods verify the document with the given VerifyOptions first, then apply the predicate to the signatures that passed verification (the getSucceeded() list of the VerificationResult) and return the ones that satisfy it. A null predicate returns all verified signatures.
Here are the steps to verify Barcode signature within the document with GroupDocs.Signature:
Create new instance of Signature class and pass source document path as a constructor parameter.
Define a predicate with the criteria for the verified signatures, such as checking that the signature type is Barcode and that the SignatureId equals “123”.
Call the verify method and pass the options and the predicate to it.
Process the returned list of verified signatures if needed.
This example shows how to verify Barcode signature and filter the result with a predicate.
Signaturesignature=newSignature("sample.pdf");try{// verify Barcode signatures with the text "12345678"
BarcodeVerifyOptionsoptions=newBarcodeVerifyOptions("12345678");// keep only verified Barcode signatures with a specific SignatureId
List<BaseSignature>signatures=signature.verify(options,signatureItem->signatureItem.getSignatureType()==SignatureType.Barcode&&"123".equals(signatureItem.getSignatureId()));// Optionally, process the verified signatures here.
System.out.println("Verified "+signatures.size()+" Barcode signature(s) with SignatureId 123");}finally{signature.dispose();}
Verify several signature types with one predicate
Pass a list of verification options to verify several signature types at once. This example keeps the verified signatures on the first three pages and, for digital signatures, only the valid ones.
Signaturesignature=newSignature("sample_signed_digital.pdf");try{TextVerifyOptionstextVerifyOptions=newTextVerifyOptions();textVerifyOptions.setText("Approved");List<VerifyOptions>verifyOptionsList=newArrayList<VerifyOptions>();verifyOptionsList.add(newDigitalVerifyOptions());verifyOptionsList.add(textVerifyOptions);// keep verified signatures on the first 3 pages; digital signatures must also be valid
List<BaseSignature>filteredSignatures=signature.verify(verifyOptionsList,sig->sig.getPageNumber()>=1&&sig.getPageNumber()<=3&&(!(siginstanceofDigitalSignature)||((DigitalSignature)sig).isValid()));System.out.println("Verified "+filteredSignatures.size()+" signatures on pages 1-3");for(BaseSignaturesig:filteredSignatures){System.out.println(" "+sig.getSignatureType()+" signature on page "+sig.getPageNumber());}}finally{signature.dispose();}
More resources
GitHub Examples
You may easily run the code above and see the feature in action in our GitHub examples: