Sample code for 30+ languages & platforms
Java 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 Java Downloads

Java
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    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) {
        System.out.println(pdf.lastErrorText());
        return;
        }

    System.out.println("signatures: " + 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) {
        System.out.println(pdf.lastErrorText());
        return;
        }

    System.out.println("audit ok, estimated savings: " + jsonOut.IntOf("estimatedSavings.total"));

    //  Optimizing is refused while PreserveSignatures is true.
    success = pdf.Optimize("optimize",json,jsonOut);
    if (success == false) {
        System.out.println("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) {
        System.out.println(pdf.lastErrorText());
        return;
        }

    success = pdf.WriteFile("qa_output/invoice_optimized.pdf");
    if (success == false) {
        System.out.println(pdf.lastErrorText());
        return;
        }

    System.out.println("optimized, bytesSaved: " + 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) {
        System.out.println(pdf2.lastErrorText());
        return;
        }

    System.out.println("signatures after: " + pdf2.get_NumSignatures());

    CkJsonObject sigInfo = new CkJsonObject();
    boolean verified = pdf2.VerifySignature(0,sigInfo);
    System.out.println("verifies after optimizing: " + verified);
  }
}