Android™ Requires Chilkat v11.7.0+
Android™
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 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 grand_canyon_hiking_guide.pdf
CkPdf pdf = new CkPdf();
success = pdf.LoadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf");
if (success == false) {
Log.i(TAG, 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) {
Log.i(TAG, pdf.lastErrorText());
return;
}
Log.i(TAG, "fileSize: " + String.valueOf(jsonOut.IntOf("fileSize")));
Log.i(TAG, "pdfVersion: " + jsonOut.stringOf("pdfVersion"));
Log.i(TAG, "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.
Log.i(TAG, "images: " + String.valueOf(jsonOut.IntOf("usage.images.bytes")) + " in " + String.valueOf(jsonOut.IntOf("usage.images.count")) + " objects");
Log.i(TAG, "fonts: " + String.valueOf(jsonOut.IntOf("usage.fonts.bytes")) + " in " + String.valueOf(jsonOut.IntOf("usage.fonts.count")) + " objects");
Log.i(TAG, "pageContent: " + String.valueOf(jsonOut.IntOf("usage.pageContent.bytes")));
Log.i(TAG, "formXObjects: " + String.valueOf(jsonOut.IntOf("usage.formXObjects.bytes")));
Log.i(TAG, "annotations: " + String.valueOf(jsonOut.IntOf("usage.annotations.bytes")));
Log.i(TAG, "structureTree: " + String.valueOf(jsonOut.IntOf("usage.structureTree.bytes")));
Log.i(TAG, "metadata: " + String.valueOf(jsonOut.IntOf("usage.metadata.bytes")));
Log.i(TAG, "embeddedFiles: " + String.valueOf(jsonOut.IntOf("usage.embeddedFiles.bytes")));
Log.i(TAG, "oldRevisions: " + String.valueOf(jsonOut.IntOf("usage.oldRevisions.bytes")));
Log.i(TAG, "overhead: " + String.valueOf(jsonOut.IntOf("usage.overhead.bytes")));
// What each option is expected to save. An option that would save nothing is left out.
Log.i(TAG, "estimated total: " + String.valueOf(jsonOut.IntOf("estimatedSavings.total")));
CkJsonObject est = new CkJsonObject();
success = jsonOut.ObjectOf2("estimatedSavings",est);
if (success == false) {
Log.i(TAG, jsonOut.lastErrorText());
return;
}
int i = 0;
int numEst = est.get_Size();
while (i < numEst) {
String optionName = est.nameAt(i);
Log.i(TAG, " " + optionName + ": " + String.valueOf(est.IntOf(optionName)));
i = i + 1;
}
}
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."
}
}