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

Shrink a PDF Losslessly

See more PDF Optimization Examples

Demonstrates the "optimize" operation with the default options, which is the lossless preset: nothing is resampled, recoloured or thrown away, so every page looks exactly as it did and all the text is still there. The changes are kept in memory until WriteFile (or WriteBd) is called. Note that the real saving is "fileSize" minus the size of the file that was written, not "bytesSaved": the rewrite also packs objects into object streams and writes a compact cross-reference stream, which belongs to no option.

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;
        }

    //  An empty JSON means the default options: every lossless optimization, object streams generated,
    //  old revisions dropped, deflate level 9.
    CkJsonObject json = new CkJsonObject();
    CkJsonObject jsonOut = new CkJsonObject();
    success = pdf.Optimize("optimize",json,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    //  Nothing has been written yet.  WriteFile (or WriteBd) produces the optimized PDF.
    success = pdf.WriteFile("qa_output/grand_canyon_optimized.pdf");
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    CkFileAccess fac = new CkFileAccess();
    int sizeBefore = jsonOut.IntOf("fileSize");
    int sizeAfter = fac.FileSize("qa_output/grand_canyon_optimized.pdf");

    Log.i(TAG, "before: " + String.valueOf(sizeBefore));
    Log.i(TAG, "after:  " + String.valueOf(sizeAfter));
    Log.i(TAG, "saved:  " + String.valueOf(sizeBefore - sizeAfter));

    //  What the options themselves account for.  This is smaller than the real saving above, because the
    //  rewrite's own gains (object streams, the cross-reference stream, the old sections left behind)
    //  belong to no single option.
    Log.i(TAG, "bytesSaved (options only): " + String.valueOf(jsonOut.IntOf("bytesSaved")));

  }

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