# # Signature Service API

## # Overview

The `SignatureService` class is designed for customizing the PDF signature validation logic. By configuring a custom signature verification callback, developers can forward signature validation requests to backend services or integrate third-party verification mechanisms.

## # Main Methods

### # setVerifyHandler(handler: VerifySignatureHandler)

- Functionality: Set the signature verification callback function. When the PDF viewer needs to validate a signature, it will invoke this `handler`.
- Parameters:
  - `handler`: A function of type `VerifySignatureHandler`, which handles signature verification.
- Return Value: None
- Version: Supported since version 11.0.0

#### # Description of `handler`

- `handler` is an asynchronous function, typically defined as follows

      async (signatureField, plainContent, signedData, hasDataOutOfScope) => { ... }

- Within the `handler`, developers can access information related to the signature field (e.g., `filter`, `subfilter`, `signer`) and forward the original content and signature data to backend services for validation.

## # Signature Verification Process Description

- The signature verification process is triggered in the following scenarios:

  1. Triggered by JavaScript Action: For example, when the document is opened (PDFDoc open action) or when the mouse button is released over the annotation area (ANNOT_MOUSE_BUTTON_RELEASED), signature verification is performed automatically.

  2. Triggered by User Action: When the user clicks on a signed signature field, the signature verification process is initiated.

  3. Calling the `PDFSignature.verify` Method: When using the `verify` method in `PDFSignature`, if the `verifyHandler` option is not specified, the `handler` set via `SignatureService.setVerifyHandler` will be used by default.

- Note：

  - The `PDFDoc.verifySignature` method does not use the `handler` set in `SignatureService` by default; you need to specify it explicitly.

## # Example

    const service = pdfViewer.getSignatureService();
    service.setVerifyHandler(async (signatureField, plainContent, signedData, hasDataOutOfScope) => {
        const filter = await signatureField.getFilter();
        const subfilter = await signatureField.getSubfilter();
        const signer = await signatureField.getSigner();

        const formdata = new FormData();
        formdata.append("filter", filter);
        formdata.append("subfilter", subfilter);
        formdata.append("signer", signer);
        formdata.append("plainContent", new Blob([plainContent]), "plainContent");
        formdata.append("signedData", new Blob([signedData]), "signedData");

        const response = await fetch('https://<server>:<port>/<path/to/>verify', {
            method: 'POST',
            body: formdata
        });
        return parseInt(await response.text());
    });

## # Notes

- Must Set a Verification Callback: Before initiating the signature verification process, make sure to set the `handler` using `SignatureService.setVerifyHandler`; otherwise, the verification process will throw an exception.

- Handler Return Value: The `handler` should return a numeric value representing the verification result (refer to the Form Signature Fields documentation for related information).

- Asynchronous Support: The `handler` supports asynchronous operations, making it convenient for integration with backend services.

- Scope of Applicability: The `handler` set in `SignatureService` is only effective for JavaScript Action verification processes and when `PDFSignature.verify` does not specify a `handler`. It does not apply to `PDFDoc.verifySignature`. :