How to verify signatures with predicate functions

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:

public final List<BaseSignature> verify(VerifyOptions verifyOptions, Predicate<BaseSignature> predicate) throws Exception
public final List<BaseSignature> verify(List<VerifyOptions> verifyOptionsList, Predicate<BaseSignature> predicate) throws Exception

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.
  • Instantiate the BarcodeVerifyOptions object according to your requirements.
  • 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.

Signature signature = new Signature("sample.pdf");
try
{
    // verify Barcode signatures with the text "12345678"
    BarcodeVerifyOptions options = new BarcodeVerifyOptions("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.

Signature signature = new Signature("sample_signed_digital.pdf");
try
{
    TextVerifyOptions textVerifyOptions = new TextVerifyOptions();
    textVerifyOptions.setText("Approved");

    List<VerifyOptions> verifyOptionsList = new ArrayList<VerifyOptions>();
    verifyOptionsList.add(new DigitalVerifyOptions());
    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 &&
        (!(sig instanceof DigitalSignature) || ((DigitalSignature) sig).isValid()));

    System.out.println("Verified " + filteredSignatures.size() + " signatures on pages 1-3");
    for (BaseSignature sig : 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:

Free Online Apps

Along with the full-featured Java library, we provide simple but powerful free online apps.

To sign PDF, Word, Excel, PowerPoint, and other documents you can use the online apps from the GroupDocs.Signature App Product Family.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.