Java Requires Chilkat v11.7.0+
Chilkat2 Java
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 Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Pdf;
public class ChilkatExample {
public static void main(String[] args) {
// 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
Pdf pdf = new Pdf();
try {
pdf.loadFile("qa_data/pdf/watersense_accomplishments.pdf");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
JsonObject json = new JsonObject();
JsonObject jsonOut = new JsonObject();
try {
pdf.optimize("audit", json, jsonOut);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("fileSize: " + jsonOut.intOf("fileSize"));
System.out.println("removeUnreferenced would save: " + jsonOut.intOf("estimatedSavings.removeUnreferenced"));
// Optimize with the defaults and see how many objects were actually dropped.
try {
pdf.optimize("optimize", json, jsonOut);
pdf.writeFile("qa_output/watersense_optimized.pdf");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("objects removed: " + jsonOut.intOf("actions.removeUnreferenced.count"));
System.out.println("bytes saved: " + 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.
System.out.println("pages after: " + pdf.getNumPages());
}
}