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

Unicode C++
#include <CkFileAccessW.h>
#include <CkJsonObjectW.h>
#include <CkPdfW.h>

void ChilkatSample(void)
    {
    bool 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

    CkFileAccessW fac;
    CkJsonObjectW json;
    CkJsonObjectW jsonOut;

    //  generate: pack the objects (the default).
    CkPdfW pdf1;
    success = pdf1.LoadFile(L"qa_data/pdf/grand_canyon_hiking_guide.pdf");
    if (success == false) {
        wprintf(L"%s\n",pdf1.lastErrorText());
        return;
    }

    json.UpdateString(L"objectStreams",L"generate");
    success = pdf1.Optimize(L"optimize",json,jsonOut);
    if (success == false) {
        wprintf(L"%s\n",pdf1.lastErrorText());
        return;
    }

    success = pdf1.WriteFile(L"qa_output/grand_canyon_objstm_generate.pdf");
    if (success == false) {
        wprintf(L"%s\n",pdf1.lastErrorText());
        return;
    }

    wprintf(L"generate: %d bytes, objects packed: %d\n",fac.FileSize(L"qa_output/grand_canyon_objstm_generate.pdf"),jsonOut.IntOf(L"actions.objectStreams.count"));

    //  disable: every object on its own, with a classic cross-reference table.
    CkPdfW pdf2;
    success = pdf2.LoadFile(L"qa_data/pdf/grand_canyon_hiking_guide.pdf");
    if (success == false) {
        wprintf(L"%s\n",pdf2.lastErrorText());
        return;
    }

    json.UpdateString(L"objectStreams",L"disable");
    success = pdf2.Optimize(L"optimize",json,jsonOut);
    if (success == false) {
        wprintf(L"%s\n",pdf2.lastErrorText());
        return;
    }

    success = pdf2.WriteFile(L"qa_output/grand_canyon_objstm_disable.pdf");
    if (success == false) {
        wprintf(L"%s\n",pdf2.lastErrorText());
        return;
    }

    wprintf(L"disable:  %d bytes\n",fac.FileSize(L"qa_output/grand_canyon_objstm_disable.pdf"));

    //  preserve: leave the file's own structure alone.  Use it when the output must stay readable by
    //  whatever reads the input today.
    CkPdfW pdf3;
    success = pdf3.LoadFile(L"qa_data/pdf/grand_canyon_hiking_guide.pdf");
    if (success == false) {
        wprintf(L"%s\n",pdf3.lastErrorText());
        return;
    }

    json.UpdateString(L"objectStreams",L"preserve");
    success = pdf3.Optimize(L"optimize",json,jsonOut);
    if (success == false) {
        wprintf(L"%s\n",pdf3.lastErrorText());
        return;
    }

    success = pdf3.WriteFile(L"qa_output/grand_canyon_objstm_preserve.pdf");
    if (success == false) {
        wprintf(L"%s\n",pdf3.lastErrorText());
        return;
    }

    wprintf(L"preserve: %d bytes\n",fac.FileSize(L"qa_output/grand_canyon_objstm_preserve.pdf"));
    }