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

Deflate Compression Level When Optimizing a PDF

See more PDF Optimization Examples

Demonstrates compressionLevel, which is the deflate level (1 to 9, 9 by default) used for every stream the optimizer re-encodes. Level 9 is the smallest and the slowest; level 1 is the quickest. The difference is usually a percent or two, so level 9 is the right default for a file that will be downloaded many times, and a lower level is worth considering only when a server optimizes PDFs on the fly. A value outside 1 to 9 is an error.

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 watersense_accomplishments.pdf

    CkFileAccess fac = new CkFileAccess();
    CkJsonObject json = new CkJsonObject();
    CkJsonObject jsonOut = new CkJsonObject();

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

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

    success = pdf1.WriteFile("qa_output/watersense_level1.pdf");
    if (success == false) {
        Log.i(TAG, pdf1.lastErrorText());
        return;
        }

    Log.i(TAG, "level 1: " + String.valueOf(fac.FileSize("qa_output/watersense_level1.pdf")));

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

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

    success = pdf9.WriteFile("qa_output/watersense_level9.pdf");
    if (success == false) {
        Log.i(TAG, pdf9.lastErrorText());
        return;
        }

    Log.i(TAG, "level 9: " + String.valueOf(fac.FileSize("qa_output/watersense_level9.pdf")));

    //  A level outside 1..9 fails, and LastErrorText says why.
    CkPdf pdfBad = new CkPdf();
    success = pdfBad.LoadFile("qa_data/pdf/watersense_accomplishments.pdf");
    if (success == false) {
        Log.i(TAG, pdfBad.lastErrorText());
        return;
        }

    json.UpdateInt("compressionLevel",12);
    success = pdfBad.Optimize("optimize",json,jsonOut);
    if (success == false) {
        Log.i(TAG, "compressionLevel 12 was refused, as it should be.");
        }


  }

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