Android™ Requires Chilkat v11.7.0+
Android™
Encrypt and Sign a PDF, and Sign an Encrypted PDF
See more PDF Protection Examples
Demonstrates signing together with encryption. A pending encrypt is applied by SignPdf before it signs, so one call produces a PDF that is both encrypted and signed. A PDF that is already encrypted can be signed too, and stays encrypted. When it was opened with only the user password its permissions decide: adding a signature field needs both the modify and the annotate permission. Loading the PDF with its owner password lifts that.Chilkat Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean success = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The PDF used in this example is available at two_page_memo.pdf
// The signing certificate, with its private key.
CkCert cert = new CkCert();
success = cert.LoadPfxFile("qa_data/pfx/myPdfSigningCert.pfx","secret");
if (success == false) {
Log.i(TAG, cert.lastErrorText());
return;
}
CkPdf pdf = new CkPdf();
success = pdf.LoadFile("qa_data/pdf/two_page_memo.pdf");
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
// Encrypt with AES-256 and do not permit modifying.
pdf.put_UserPassword("user-secret");
pdf.put_OwnerPassword("owner-secret");
CkJsonObject json = new CkJsonObject();
json.UpdateBool("permissions.modify",false);
CkJsonObject jsonOut = new CkJsonObject();
success = pdf.Protection("encrypt",json,jsonOut);
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
// The encryption is still pending. SignPdf applies it first and signs the encrypted result.
success = pdf.SetSigningCert(cert);
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
CkJsonObject sigOpts = new CkJsonObject();
sigOpts.UpdateBool("invisibleSignature",true);
sigOpts.UpdateInt("signingCertificateV2",1);
sigOpts.UpdateInt("signingTime",1);
success = pdf.SignPdf(sigOpts,"qa_output/memo_encrypted_signed.pdf");
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
// The result is encrypted and signed.
CkPdf check = new CkPdf();
check.put_UserPassword("user-secret");
success = check.LoadFile("qa_output/memo_encrypted_signed.pdf");
if (success == false) {
Log.i(TAG, check.lastErrorText());
return;
}
CkJsonObject empty = new CkJsonObject();
CkJsonObject info = new CkJsonObject();
success = check.Protection("getInfo",empty,info);
if (success == false) {
Log.i(TAG, check.lastErrorText());
return;
}
Log.i(TAG, "algorithm: " + info.stringOf("loaded.algorithm"));
Log.i(TAG, "signatures: " + String.valueOf(check.get_NumSignatures()));
CkJsonObject sigInfo = new CkJsonObject();
boolean verified = check.VerifySignature(0,sigInfo);
Log.i(TAG, "signature verifies: " + String.valueOf(verified));
// Signing an encrypted PDF. This one was opened with only the user password, and its
// permissions do not allow modifying, so another signature is refused.
success = check.SetSigningCert(cert);
if (success == false) {
Log.i(TAG, check.lastErrorText());
return;
}
success = check.SignPdf(sigOpts,"qa_output/memo_signed_twice.pdf");
if (success == false) {
Log.i(TAG, "A second signature was refused: the permissions do not allow it.");
}
// Loaded with the owner password, the same PDF can be signed. It stays encrypted, and the
// first signature stays valid because the new one is added as an incremental update.
CkPdf owner = new CkPdf();
owner.put_UserPassword("user-secret");
owner.put_OwnerPassword("owner-secret");
success = owner.LoadFile("qa_output/memo_encrypted_signed.pdf");
if (success == false) {
Log.i(TAG, owner.lastErrorText());
return;
}
success = owner.SetSigningCert(cert);
if (success == false) {
Log.i(TAG, owner.lastErrorText());
return;
}
success = owner.SignPdf(sigOpts,"qa_output/memo_signed_twice.pdf");
if (success == false) {
Log.i(TAG, owner.lastErrorText());
return;
}
CkPdf twice = new CkPdf();
twice.put_UserPassword("user-secret");
success = twice.LoadFile("qa_output/memo_signed_twice.pdf");
if (success == false) {
Log.i(TAG, twice.lastErrorText());
return;
}
Log.i(TAG, "signatures after signing with owner rights: " + String.valueOf(twice.get_NumSignatures()));
verified = twice.VerifySignature(0,sigInfo);
Log.i(TAG, "first signature still verifies: " + String.valueOf(verified));
verified = twice.VerifySignature(1,sigInfo);
Log.i(TAG, "second signature verifies: " + String.valueOf(verified));
// Sample output:
// algorithm: aes-256
// signatures: 1
// signature verifies: True
// A second signature was refused: the permissions do not allow it.
// signatures after signing with owner rights: 2
// first signature still verifies: True
// second signature verifies: True
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}