Android™ Requires Chilkat v11.7.0+
Android™
Audit a PDF First, Optimize Only If It Is Worth It
See more PDF Optimization Examples
Demonstrates deciding whether to rewrite a PDF at all. "audit" costs a fraction of a second and changes nothing, so a batch job can audit every file and optimize only those where enough would be saved. Here the threshold is a tenth of the file; a file already as small as it can be is left alone.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 watersense_accomplishments.pdf
CkPdf pdf = new CkPdf();
success = pdf.LoadFile("qa_data/pdf/watersense_accomplishments.pdf");
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
CkJsonObject json = new CkJsonObject();
CkJsonObject jsonOut = new CkJsonObject();
success = pdf.Optimize("audit",json,jsonOut);
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
int fileSize = jsonOut.IntOf("fileSize");
int estimated = jsonOut.IntOf("estimatedSavings.total");
Log.i(TAG, "fileSize: " + String.valueOf(fileSize));
Log.i(TAG, "estimated: " + String.valueOf(estimated));
// Only bother when at least a tenth of the file would go.
if (estimated > (fileSize / 10)) {
Log.i(TAG, "Worth optimizing.");
success = pdf.Optimize("optimize",json,jsonOut);
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
success = pdf.WriteFile("qa_output/watersense_optimized.pdf");
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
Log.i(TAG, "bytesSaved: " + String.valueOf(jsonOut.IntOf("bytesSaved")));
}
else {
Log.i(TAG, "Already small enough; left as it is.");
}
}
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."
}
}