Sample code for 30+ languages & platforms
Android™ Requires Chilkat v11.7.0+

Encrypt a PDF for a Certificate Instead of a Password

See more PDF Protection Examples

Demonstrates certificate security, where a PDF is encrypted for one or more recipients and each opens it with their own certificate and private key. No password is involved. Only the public certificate of a recipient is needed to encrypt. Each recipient has its own permissions, and the changeSecurity permission gives a recipient owner rights. PreLoadCheck tells an application that a file is certificate-encrypted and which certificates can open it. A PDF that cannot be opened, because no matching certificate and private key were provided, fails to load.

Chilkat Android™ Downloads

Android™
// 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 recipient's certificate.  Only the public certificate is needed to encrypt.
    CkCert recipient = new CkCert();
    success = recipient.LoadFromFile("qa_data/certs/recipient.cer");
    if (success == false) {
        Log.i(TAG, recipient.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;
        }

    //  Call AddEncryptCert once for each recipient.
    success = pdf.AddEncryptCert(recipient);
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    //  recipientPermissions[0] belongs to the first certificate added.  changeSecurity gives that
    //  recipient owner rights, so the file can be managed later.
    CkJsonObject json = new CkJsonObject();
    json.UpdateString("method","certificate");
    json.UpdateBool("recipientPermissions[0].changeSecurity",true);

    CkJsonObject jsonOut = new CkJsonObject();
    success = pdf.Protection("encrypt",json,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    Log.i(TAG, "method: " + jsonOut.stringOf("pending.method"));
    Log.i(TAG, "algorithm: " + jsonOut.stringOf("pending.algorithm"));
    Log.i(TAG, "recipients: " + String.valueOf(jsonOut.SizeOfArray("pending.recipients")));

    success = pdf.WriteFile("qa_output/memo_for_recipient.pdf");
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    //  PreLoadCheck says how the file is protected, without loading it.
    CkPdf reader = new CkPdf();
    CkJsonObject info = new CkJsonObject();
    success = reader.PreLoadCheck("qa_output/memo_for_recipient.pdf",info);
    if (success == false) {
        Log.i(TAG, reader.lastErrorText());
        return;
        }

    Log.i(TAG, "saved file, method: " + info.stringOf("method"));
    Log.i(TAG, "saved file, recipients: " + String.valueOf(info.SizeOfArray("recipients")));

    //  The recipient opens it with the certificate and its private key, here from a .pfx file.
    //  (On Windows, a certificate that is installed in the Windows certificate store with its private
    //  key is found automatically when the PDF is loaded, and SetDecryptCert is not needed for it.)
    CkCert myCert = new CkCert();
    success = myCert.LoadPfxFile("qa_data/pfx/recipient.pfx","pfx-password");
    if (success == false) {
        Log.i(TAG, myCert.lastErrorText());
        return;
        }

    success = reader.SetDecryptCert(myCert);
    if (success == false) {
        Log.i(TAG, reader.lastErrorText());
        return;
        }

    success = reader.LoadFile("qa_output/memo_for_recipient.pdf");
    if (success == false) {
        Log.i(TAG, reader.lastErrorText());
        return;
        }

    CkJsonObject empty = new CkJsonObject();
    CkJsonObject loadedInfo = new CkJsonObject();
    success = reader.Protection("getInfo",empty,loadedInfo);
    if (success == false) {
        Log.i(TAG, reader.lastErrorText());
        return;
        }

    Log.i(TAG, "opened with: " + loadedInfo.stringOf("loaded.openedWith"));
    Log.i(TAG, "pages: " + String.valueOf(reader.get_NumPages()));

    //  Sample output:

    //  method: certificate
    //  algorithm: aes-256
    //  recipients: 1
    //  saved file, method: certificate
    //  saved file, recipients: 1
    //  opened with: certificate
    //  pages: 2

  }

  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."
  }
}