1. Home
  2. Foxit PDF SDK for Android
  3. How to Apply Signatures with Foxit PDF SDK for Android

How to Apply Signatures with Foxit PDF SDK for Android

A PDF Signature can be used to create and sign digital signatures on PDF documents, which protects the security of documents’ contents and avoids it from being tampered with maliciously. It can let the receiver ensure that the document is released by the signer and the contents of the document are complete and unchanged. Foxit PDF SDK provides APIs to create a digital signature, verify the validity of the signature, delete existing digital signatures, get and set properties of a digital signature, display a signature and customize the appearance of the signature form fields.

Note: Foxit PDF SDK provides default Signature callbacks which supports the following two types of signature filter and subfilter:

  1. filter: Adobe.PPKLite – subfilter: adbe.pkcs7.detached
  2. filter: Adobe.PPKLite – subfilter: adbe.pkcs7.sha1

If you use one of the above signature filters and subfilters, you can sign a PDF document and verify the validity of the signature by default without needing to register a custom callback.

Example:

How to sign a PDF document and verify the signature

import com.foxit.sdk.PDFException;
import com.foxit.sdk.PDFViewCtrl;
import com.foxit.sdk.common.Constants;
import com.foxit.sdk.common.Progressive;
import com.foxit.sdk.common.fxcrt.RectF;
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.sdk.pdf.PDFPage;
import com.foxit.sdk.pdf.Signature;
...
// Sample code demonstrate signing and verifying of PDF signature.
public void addNewSignatureAndSign(PDFPage page, RectF rect) {
 try {
 // Add a new signature on the specified page rect.
 Signature signature = page.addSignature(rect);
 // Set the appearance flags, if the specified flag is on, then the associated key will be displayed on the signature appearance.
 signature.setAppearanceFlags(Signature.e_APFlagLabel | Signature.e_APFlagDN | Signature.e_APFlagText
 | Signature.e_APFlagLocation | Signature.e_APFlagReason | Signature.e_APFlagSigner);
 // Set signer.
 signature.setKeyValue(Signature.e_KeyNameSigner, "Foxit");
 // Set location.
 signature.setKeyValue(Signature.e_KeyNameLocation, "AnyWhere");
 // Set reason.
 signature.setKeyValue(Signature.e_KeyNameReason, "ANyReason");
 // Set contact info.
 signature.setKeyValue(Signature.e_KeyNameContactInfo, "AnyInfo");
 // Set domain name.
 signature.setKeyValue(Signature.e_KeyNameDN, "AnyDN");
 // Set description.
 signature.setKeyValue(Signature.e_KeyNameText, "AnyContent");
 // Filter "Adobe.PPKLite" is supported by default.
 signature.setFilter("Adobe.PPKLite");
 // SubFilter "adbe.pkcs7.sha1" or "adbe.pkcs7.detached" are supported by default.
 signature.setSubFilter("adbe.pkcs7.detached");
 // The input PKCS#12 format certificate, which contains the public and private keys.
 String certPath = "/somewhere/cert.pfx";
 // Password for that certificate.
 byte[] certPassword = "123".getBytes();
 String signedPDFPath = "/somewhere/signed.pdf";
 // Start to sign the signature, if everything goes well, the signed PDF will be saved to the path specified by "save_path".
 Progressive progressive = signature.startSign(certPath, certPassword, Signature.e_DigestSHA1, signedPDFPath, null, null);
 if (progressive != null) {
 int state = Progressive.e_ToBeContinued;
 while (state == Progressive.e_ToBeContinued) {
 state = progressive.resume();
 }
 if (state != Progressive.e_Finished) return;
 }
 // Get the signatures from the signed PDF document, then verify them all.
 PDFDoc pdfDoc = new PDFDoc(signedPDFPath);
 int err = pdfDoc.load(null);
 if (err != Constants.e_ErrSuccess) return;
 int count = pdfDoc.getSignatureCount();
 for (int i = 0; i < count; i++) {
 Signature sign = pdfDoc.getSignature(i);
 if (sign != null && !sign.isEmpty()) {
 Progressive progressive_1 = sign.startVerify(null, null);
 if (progressive_1 != null) {
 int state = Progressive.e_ToBeContinued;
 while (state == Progressive.e_ToBeContinued) {
 state = progressive_1.resume();
 }
 if (state != Progressive.e_Finished) continue;
 }
 int verifiedState = sign.getState();
 if ((verifiedState & sign.e_StateVerifyValid) == sign.e_StateVerifyValid) {
 Log.d("Signature", "addNewSignatureAndSign: Signature" + i + "is valid.");
 }
 }
 }
 } catch (PDFException e) {
 e.printStackTrace();
 }
}
Updated on October 23, 2019

Was this article helpful?

Related Articles