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

C
#include <C_CkFileAccess.h>
#include <C_CkJsonObject.h>
#include <C_CkPdf.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkFileAccess fac;
    HCkJsonObject json;
    HCkJsonObject jsonOut;
    HCkPdf pdf1;
    HCkPdf pdf2;
    HCkPdf pdf3;

    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

    fac = CkFileAccess_Create();
    json = CkJsonObject_Create();
    jsonOut = CkJsonObject_Create();

    //  generate: pack the objects (the default).
    pdf1 = CkPdf_Create();
    success = CkPdf_LoadFile(pdf1,"qa_data/pdf/grand_canyon_hiking_guide.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf1));
        CkFileAccess_Dispose(fac);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkPdf_Dispose(pdf1);
        return;
    }

    CkJsonObject_UpdateString(json,"objectStreams","generate");
    success = CkPdf_Optimize(pdf1,"optimize",json,jsonOut);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf1));
        CkFileAccess_Dispose(fac);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkPdf_Dispose(pdf1);
        return;
    }

    success = CkPdf_WriteFile(pdf1,"qa_output/grand_canyon_objstm_generate.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf1));
        CkFileAccess_Dispose(fac);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkPdf_Dispose(pdf1);
        return;
    }

    printf("generate: %d bytes, objects packed: %d\n",CkFileAccess_FileSize(fac,"qa_output/grand_canyon_objstm_generate.pdf"),CkJsonObject_IntOf(jsonOut,"actions.objectStreams.count"));

    //  disable: every object on its own, with a classic cross-reference table.
    pdf2 = CkPdf_Create();
    success = CkPdf_LoadFile(pdf2,"qa_data/pdf/grand_canyon_hiking_guide.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf2));
        CkFileAccess_Dispose(fac);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkPdf_Dispose(pdf1);
        CkPdf_Dispose(pdf2);
        return;
    }

    CkJsonObject_UpdateString(json,"objectStreams","disable");
    success = CkPdf_Optimize(pdf2,"optimize",json,jsonOut);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf2));
        CkFileAccess_Dispose(fac);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkPdf_Dispose(pdf1);
        CkPdf_Dispose(pdf2);
        return;
    }

    success = CkPdf_WriteFile(pdf2,"qa_output/grand_canyon_objstm_disable.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf2));
        CkFileAccess_Dispose(fac);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkPdf_Dispose(pdf1);
        CkPdf_Dispose(pdf2);
        return;
    }

    printf("disable:  %d bytes\n",CkFileAccess_FileSize(fac,"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.
    pdf3 = CkPdf_Create();
    success = CkPdf_LoadFile(pdf3,"qa_data/pdf/grand_canyon_hiking_guide.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf3));
        CkFileAccess_Dispose(fac);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkPdf_Dispose(pdf1);
        CkPdf_Dispose(pdf2);
        CkPdf_Dispose(pdf3);
        return;
    }

    CkJsonObject_UpdateString(json,"objectStreams","preserve");
    success = CkPdf_Optimize(pdf3,"optimize",json,jsonOut);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf3));
        CkFileAccess_Dispose(fac);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkPdf_Dispose(pdf1);
        CkPdf_Dispose(pdf2);
        CkPdf_Dispose(pdf3);
        return;
    }

    success = CkPdf_WriteFile(pdf3,"qa_output/grand_canyon_objstm_preserve.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf3));
        CkFileAccess_Dispose(fac);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkPdf_Dispose(pdf1);
        CkPdf_Dispose(pdf2);
        CkPdf_Dispose(pdf3);
        return;
    }

    printf("preserve: %d bytes\n",CkFileAccess_FileSize(fac,"qa_output/grand_canyon_objstm_preserve.pdf"));


    CkFileAccess_Dispose(fac);
    CkJsonObject_Dispose(json);
    CkJsonObject_Dispose(jsonOut);
    CkPdf_Dispose(pdf1);
    CkPdf_Dispose(pdf2);
    CkPdf_Dispose(pdf3);

    }