Android™ Requires Chilkat v11.7.0+
Android™
Remove PDF Objects Nothing Refers To
See more PDF Optimization Examples
Demonstrates removeUnreferenced. When a page, an image or a whole section is replaced in an editor, the objects it used often stay in the file with nothing pointing at them any more. They are not old revisions -- they are in the current revision, simply unreachable -- so only a reachability walk from the document catalog finds them. In a publication assembled over several editing passes this is frequently the largest single saving.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;
}
Log.i(TAG, "fileSize: " + String.valueOf(jsonOut.IntOf("fileSize")));
Log.i(TAG, "removeUnreferenced would save: " + String.valueOf(jsonOut.IntOf("estimatedSavings.removeUnreferenced")));
// Optimize with the defaults and see how many objects were actually dropped.
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, "objects removed: " + String.valueOf(jsonOut.IntOf("actions.removeUnreferenced.count")));
Log.i(TAG, "bytes saved: " + String.valueOf(jsonOut.IntOf("actions.removeUnreferenced.bytesSaved")));
// Nothing a page needs is ever removed: the walk starts at the document catalog and follows every
// reference, including the ones in annotations, the structure tree, named destinations and the
// optional-content configuration.
Log.i(TAG, "pages after: " + String.valueOf(pdf.get_NumPages()));
}
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."
}
}