Foxit PDF SDK for Linux Foxit PDF SDK for Mac Foxit PDF SDK for Windows

How to Apply Signatures in Foxit PDF SDK (Java)

The PDF Signature module can be used to create and sign with digital signatures for PDF documents. This protects the security of documents and prevents 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 a signature, delete an existing digital signature, 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 signature by default without needing to register a custom callback.

Example:

How to sign the PDF document with a signature

import com.foxit.sdk.pdf.*;
...
String filter = "Adobe.PPKLite";
String sub_filter = "adbe.pkcs7.detached";
PDFPage pdf_page = pdf_doc.getPage(0);
// Add a new signature to first page.
com.foxit.sdk.pdf.Signature new_signature = AddSiganture(pdf_page, sub_filter);
// Set filter and subfilter for the new signature.
new_signature.setFilter(filter);
new_signature.setSubFilter(sub_filter);
boolean is_signed = new_signature.isSigned();
int sig_state = new_signature.getState();
String signed_pdf_path = output_directory + "signed_newsignature.pdf";
String cert_file_path = input_path + "foxit_all.pfx";
String cert_file_password = "123456";
// Cert file path will be passed back to application through callback function SignatureCallback::Sign().
// In this demo, the cert file path will be used for signing in callback function SignatureCallback::Sign().
new_signature.startSign(cert_file_path, cert_file_password.getBytes(), e_DigestSHA1, signed_pdf_path, null, null);
...

How to implement signature callback function of signing

import com.foxit.sdk.pdf.*;
...
// Implementation of SignatureCallback
class SignatureCallbackImpl extends SignatureCallback {
private String sub_filter_;
private DigestContext digest_context_ = null;
byte[] arrall = null;
SignatureCallbackImpl(String subfilter) {
sub_filter_ = subfilter;
}
@Override
public boolean startCalcDigest(FileReaderCallback var1, int[] var2,
com.foxit.sdk.pdf.Signature var3, Object var4) {
digest_context_ = new DigestContext(var1, var2, var2.length);
return true;
}
@Override
public int continueCalcDigest(Object var1, PauseCallback var2) {
return com.foxit.sdk.common.Progressive.e_Finished;
}
@Override
public byte[] getDigest(Object var1) {
return arrall;
}
@Override
public byte[] sign(byte[] digest, String cert_path, byte[] cert_password, int digest_algorithm, java.lang.Object client_data){
String encryptStr = null;
try {
try {
FileReaderCallback filehandler = digest_context_.file_read_callback_;
{
long size = filehandler.getSize();
byte[] arr1 = new byte[digest_context_.byte_range_array_[1]];
filehandler.readBlock(arr1,
digest_context_.byte_range_array_[0],
digest_context_.byte_range_array_[1]);
byte[] arr2 = new byte[digest_context_.byte_range_array_[3]];
filehandler.readBlock(arr2,
digest_context_.byte_range_array_[2],
digest_context_.byte_range_array_[3]);
size = 0;
arrall = new byte[(int) digest_context_.byte_range_array_[1]
+ (int) digest_context_.byte_range_array_[3]];
System.arraycopy(arr1, 0, arrall, 0, arr1.length);
System.arraycopy(arr2, 0, arrall, arr1.length, arr2.length);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
encryptStr = CertUtil.SignMsg(arrall, signature.input_path
+ "foxit_all.pfx", "123456");
return encryptStr.getBytes();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
public int verifySigState(byte[] var1, byte[] var2, Object var3) {
byte[] arrall_verify = null;
boolean verify_state = false;
try {
verify_state = CertUtil.VerifyMsg(new String(var2).toLowerCase(), arrall,
signature.input_path + "foxit.cer");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return verify_state ? com.foxit.sdk.pdf.Signature.e_StateVerifyNoChange : com.foxit.sdk.pdf.Signature.e_StateVerifyChange ;
}
@Override
public boolean isNeedPadData() {return false;}
}
...

Updated on April 18, 2019

Was this article helpful?
Thanks for your feedback. If you have a comment on how to improve the article, you can write it here: