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

Shrink a PDF Losslessly

See more PDF Optimization Examples

Demonstrates the "optimize" operation with the default options, which is the lossless preset: nothing is resampled, recoloured or thrown away, so every page looks exactly as it did and all the text is still there. The changes are kept in memory until WriteFile (or WriteBd) is called. Note that the real saving is "fileSize" minus the size of the file that was written, not "bytesSaved": the rewrite also packs objects into object streams and writes a compact cross-reference stream, which belongs to no option.

Chilkat C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkPdf pdf;
    HCkJsonObject json;
    HCkJsonObject jsonOut;
    HCkFileAccess fac;
    int sizeBefore;
    int sizeAfter;

    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

    pdf = CkPdf_Create();
    success = CkPdf_LoadFile(pdf,"qa_data/pdf/grand_canyon_hiking_guide.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkPdf_Dispose(pdf);
        return;
    }

    //  An empty JSON means the default options: every lossless optimization, object streams generated,
    //  old revisions dropped, deflate level 9.
    json = CkJsonObject_Create();
    jsonOut = CkJsonObject_Create();
    success = CkPdf_Optimize(pdf,"optimize",json,jsonOut);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        return;
    }

    //  Nothing has been written yet.  WriteFile (or WriteBd) produces the optimized PDF.
    success = CkPdf_WriteFile(pdf,"qa_output/grand_canyon_optimized.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        return;
    }

    fac = CkFileAccess_Create();
    sizeBefore = CkJsonObject_IntOf(jsonOut,"fileSize");
    sizeAfter = CkFileAccess_FileSize(fac,"qa_output/grand_canyon_optimized.pdf");

    printf("before: %d\n",sizeBefore);
    printf("after:  %d\n",sizeAfter);
    printf("saved:  %d\n",sizeBefore - sizeAfter);

    //  What the options themselves account for.  This is smaller than the real saving above, because the
    //  rewrite's own gains (object streams, the cross-reference stream, the old sections left behind)
    //  belong to no single option.
    printf("bytesSaved (options only): %d\n",CkJsonObject_IntOf(jsonOut,"bytesSaved"));


    CkPdf_Dispose(pdf);
    CkJsonObject_Dispose(json);
    CkJsonObject_Dispose(jsonOut);
    CkFileAccess_Dispose(fac);

    }