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

Optimizing a Signed PDF (PreserveSignatures)

See more PDF Optimization Examples

Demonstrates what happens when a signed PDF is optimized. Optimizing rewrites the document, and a rewrite breaks every existing digital signature, so with PreserveSignatures true (the default) Optimize refuses and LastErrorText says why. Setting PreserveSignatures to false allows it, and the signature that was in the file no longer verifies afterwards. "audit" is always allowed, because it changes nothing.

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 invoice_signed.pdf

    CkPdf pdf = new CkPdf();
    success = pdf.LoadFile("qa_data/pdf/invoice_signed.pdf");
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    Log.i(TAG, "signatures: " + String.valueOf(pdf.get_NumSignatures()));

    CkJsonObject json = new CkJsonObject();
    CkJsonObject jsonOut = new CkJsonObject();

    //  An audit changes nothing, so it is allowed on a signed PDF.
    success = pdf.Optimize("audit",json,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    Log.i(TAG, "audit ok, estimated savings: " + String.valueOf(jsonOut.IntOf("estimatedSavings.total")));

    //  Optimizing is refused while PreserveSignatures is true.
    success = pdf.Optimize("optimize",json,jsonOut);
    if (success == false) {
        Log.i(TAG, "Refused while PreserveSignatures is true.");
        }

    //  Allow the rewrite.  Do this only when the signatures no longer matter -- an invoice being
    //  archived, say, where the signed original is kept elsewhere.
    pdf.put_PreserveSignatures(false);
    success = pdf.Optimize("optimize",json,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

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

    Log.i(TAG, "optimized, bytesSaved: " + String.valueOf(jsonOut.IntOf("bytesSaved")));

    //  The signature is still in the file, but it no longer verifies: the bytes it covered have changed.
    CkPdf pdf2 = new CkPdf();
    success = pdf2.LoadFile("qa_output/invoice_optimized.pdf");
    if (success == false) {
        Log.i(TAG, pdf2.lastErrorText());
        return;
        }

    Log.i(TAG, "signatures after: " + String.valueOf(pdf2.get_NumSignatures()));

    CkJsonObject sigInfo = new CkJsonObject();
    boolean verified = pdf2.VerifySignature(0,sigInfo);
    Log.i(TAG, "verifies after optimizing: " + String.valueOf(verified));

  }

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