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

Where a PDF's Bytes Go: the Optimize Audit

See more PDF Optimization Examples

Demonstrates the "audit" operation, which measures the loaded PDF and changes nothing. The report says how large the file is, which PDF version it claims, whether it claims to be PDF/A, where its bytes go ("usage", as stored in the file, so compressed sizes), and how many bytes each lossless option would be expected to save ("estimatedSavings"). Use it to decide whether optimizing is worth the trouble, and which options matter for this file.

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

    CkPdf pdf = new CkPdf();
    success = pdf.LoadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf");
    if (success == false) {
        System.out.println(pdf.lastErrorText());
        return;
        }

    //  "audit" takes the same options as "optimize", so an empty JSON means the default options.
    CkJsonObject json = new CkJsonObject();
    CkJsonObject jsonOut = new CkJsonObject();
    success = pdf.Optimize("audit",json,jsonOut);
    if (success == false) {
        System.out.println(pdf.lastErrorText());
        return;
        }

    System.out.println("fileSize: " + jsonOut.IntOf("fileSize"));
    System.out.println("pdfVersion: " + jsonOut.stringOf("pdfVersion"));
    System.out.println("pdfa: " + jsonOut.stringOf("pdfa"));

    //  Where the bytes go.  Every category is present even when it is 0, and the sizes are the stored
    //  (compressed) sizes, so they add up to fileSize.  "overhead" is what is left: the cross-reference
    //  tables, the object headers and the white space between objects.
    System.out.println("images:        " + jsonOut.IntOf("usage.images.bytes") + " in " + jsonOut.IntOf("usage.images.count") + " objects");
    System.out.println("fonts:         " + jsonOut.IntOf("usage.fonts.bytes") + " in " + jsonOut.IntOf("usage.fonts.count") + " objects");
    System.out.println("pageContent:   " + jsonOut.IntOf("usage.pageContent.bytes"));
    System.out.println("formXObjects:  " + jsonOut.IntOf("usage.formXObjects.bytes"));
    System.out.println("annotations:   " + jsonOut.IntOf("usage.annotations.bytes"));
    System.out.println("structureTree: " + jsonOut.IntOf("usage.structureTree.bytes"));
    System.out.println("metadata:      " + jsonOut.IntOf("usage.metadata.bytes"));
    System.out.println("embeddedFiles: " + jsonOut.IntOf("usage.embeddedFiles.bytes"));
    System.out.println("oldRevisions:  " + jsonOut.IntOf("usage.oldRevisions.bytes"));
    System.out.println("overhead:      " + jsonOut.IntOf("usage.overhead.bytes"));

    //  What each option is expected to save.  An option that would save nothing is left out.
    System.out.println("estimated total: " + jsonOut.IntOf("estimatedSavings.total"));

    CkJsonObject est = new CkJsonObject();
    success = jsonOut.ObjectOf2("estimatedSavings",est);
    if (success == false) {
        System.out.println(jsonOut.lastErrorText());
        return;
        }

    int i = 0;
    int numEst = est.get_Size();
    while (i < numEst) {
        String optionName = est.nameAt(i);
        System.out.println("   " + optionName + ": " + est.IntOf(optionName));
        i = i+1;
        }
  }
}