Java Requires Chilkat v11.7.0+
Chilkat2 Java
Optimize a PDF Without Rewriting It (Incremental Update)
See more PDF Optimization Examples
Demonstrates the one combination of options that does not rewrite the file. With dropOldRevisions false and objectStreams "disable", the optimized objects are appended to the original bytes as an incremental update: every byte of the input is still there, byte for byte, followed by the changes and a new cross-reference section. That is what keeps an existing digital signature valid, and what lets a reader that has the original cached fetch only the tail. The file gets a little larger before it gets smaller, so this is for compatibility, not for size.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.FileAccess;
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();
json.updateBool("dropOldRevisions", false);
json.updateString("objectStreams", "disable");
JsonObject jsonOut = new JsonObject();
try {
pdf.optimize("optimize", json, jsonOut);
pdf.writeFile("qa_output/watersense_incremental.pdf");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
FileAccess fac = new FileAccess();
System.out.println("original: " + jsonOut.intOf("fileSize"));
System.out.println("written: " + fac.fileSize("qa_output/watersense_incremental.pdf"));
// Everything the optimizer changed is in the appended part; the original bytes are the start of the
// new file. Any other combination of options -- dropOldRevisions, or generated object streams --
// rewrites the document instead.
System.out.println("bytesSaved: " + jsonOut.intOf("bytesSaved"));
}
}