Sample code for 30+ languages & platforms
Java Requires Chilkat v11.7.0+

Remove a PDF's Old Revisions

See more PDF Optimization Examples

Demonstrates dropOldRevisions. A PDF that has been edited and saved several times carries every earlier revision: the superseded objects are still in the file, reachable through the older cross-reference sections. The audit reports them as "oldRevisions", and dropping them is often the single largest saving in a real document. Dropping them requires a full rewrite, which is why it invalidates existing digital signatures.

Chilkat Java Downloads

Chilkat2 Java
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 grand_canyon_hiking_guide.pdf

        Pdf pdf = new Pdf();
        try {
            pdf.loadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // How much of this file is superseded revisions?
        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("oldRevisions: " + jsonOut.intOf("usage.oldRevisions.bytes"));

        // Drop them, and nothing else, so that the saving is attributable.
        json.updateBool("compressUncompressed", false);
        json.updateBool("recompressFlate", false);
        json.updateBool("replaceLegacyFilters", false);
        json.updateBool("imagePredictors", false);
        json.updateBool("dedupeStreams", false);
        json.updateBool("dedupeObjects", false);
        json.updateBool("removeUnreferenced", false);
        json.updateBool("removeUnusedResources", false);
        json.updateBool("removeUnusedLayers", false);
        json.updateBool("coalesceContent", false);
        json.updateBool("tidyContent", false);
        json.updateBool("subsetFonts", false);
        json.updateBool("dropOldRevisions", true);

        try {
            pdf.optimize("optimize", json, jsonOut);

            pdf.writeFile("qa_output/grand_canyon_one_revision.pdf");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // The saving is an estimate: what the old revisions occupied is known exactly, but the writer
        // decides the final layout.  Measure the written file for the real number.
        FileAccess fac = new FileAccess();
        System.out.println("bytesSaved (reported): " + jsonOut.intOf("bytesSaved"));
        System.out.println("written file:          " + fac.fileSize("qa_output/grand_canyon_one_revision.pdf"));
    }
}