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

        FileAccess fac = new FileAccess();
        JsonObject json = new JsonObject();
        JsonObject jsonOut = new JsonObject();

        // generate: pack the objects (the default).
        Pdf pdf1 = new Pdf();
        try {
            pdf1.loadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf");

            json.updateString("objectStreams", "generate");
            pdf1.optimize("optimize", json, jsonOut);

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

        System.out.println("generate: " + fac.fileSize("qa_output/grand_canyon_objstm_generate.pdf") + " bytes, objects packed: " + jsonOut.intOf("actions.objectStreams.count"));

        // disable: every object on its own, with a classic cross-reference table.
        Pdf pdf2 = new Pdf();
        try {
            pdf2.loadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf");

            json.updateString("objectStreams", "disable");
            pdf2.optimize("optimize", json, jsonOut);

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

        System.out.println("disable:  " + 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.
        Pdf pdf3 = new Pdf();
        try {
            pdf3.loadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf");

            json.updateString("objectStreams", "preserve");
            pdf3.optimize("optimize", json, jsonOut);

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

        System.out.println("preserve: " + fac.fileSize("qa_output/grand_canyon_objstm_preserve.pdf") + " bytes");
    }
}