Sample code for 30+ languages & platforms
Android™ 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 Android™ Downloads

Android™
// 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 grand_canyon_hiking_guide.pdf

    CkPdf pdf = new CkPdf();
    success = pdf.LoadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf");
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    //  How much of this file is superseded revisions?
    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, "oldRevisions: " + String.valueOf(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);

    success = pdf.Optimize("optimize",json,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    success = pdf.WriteFile("qa_output/grand_canyon_one_revision.pdf");
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        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.
    CkFileAccess fac = new CkFileAccess();
    Log.i(TAG, "bytesSaved (reported): " + String.valueOf(jsonOut.IntOf("bytesSaved")));
    Log.i(TAG, "written file:          " + String.valueOf(fac.FileSize("qa_output/grand_canyon_one_revision.pdf")));

  }

  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."
  }
}