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

PDF Object Streams: generate, preserve or disable

See more PDF Optimization Examples

Demonstrates the objectStreams option. An object stream packs many small objects (dictionaries, not stream data) into one compressed stream, which is usually a worthwhile saving in a document with many pages or tags. "generate" (the default) packs them and writes a cross-reference stream; "preserve" keeps the file as it is -- packed if it already was, not packed if it was not; "disable" writes every object on its own with a classic cross-reference table, which is what a PDF 1.4 reader needs. Object streams require PDF 1.5 and are skipped for PDF/A-1.

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

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

    //  generate: pack the objects (the default).
    CkPdf pdf1 = new CkPdf();
    success = pdf1.LoadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf");
    if (success == false) {
        Log.i(TAG, pdf1.lastErrorText());
        return;
        }

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

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

    Log.i(TAG, "generate: " + String.valueOf(fac.FileSize("qa_output/grand_canyon_objstm_generate.pdf")) + " bytes, objects packed: " + String.valueOf(jsonOut.IntOf("actions.objectStreams.count")));

    //  disable: every object on its own, with a classic cross-reference table.
    CkPdf pdf2 = new CkPdf();
    success = pdf2.LoadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf");
    if (success == false) {
        Log.i(TAG, pdf2.lastErrorText());
        return;
        }

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

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

    Log.i(TAG, "disable:  " + String.valueOf(fac.FileSize("qa_output/grand_canyon_objstm_disable.pdf")) + " bytes");

    //  preserve: leave the file's own structure alone.  Use it when the output must stay readable by
    //  whatever reads the input today.
    CkPdf pdf3 = new CkPdf();
    success = pdf3.LoadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf");
    if (success == false) {
        Log.i(TAG, pdf3.lastErrorText());
        return;
        }

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

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

    Log.i(TAG, "preserve: " + String.valueOf(fac.FileSize("qa_output/grand_canyon_objstm_preserve.pdf")) + " bytes");

  }

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