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

Remove PDF Objects Nothing Refers To

See more PDF Optimization Examples

Demonstrates removeUnreferenced. When a page, an image or a whole section is replaced in an editor, the objects it used often stay in the file with nothing pointing at them any more. They are not old revisions -- they are in the current revision, simply unreachable -- so only a reachability walk from the document catalog finds them. In a publication assembled over several editing passes this is frequently the largest single saving.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkPdfW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkPdfW pdf;
    HCkJsonObjectW json;
    HCkJsonObjectW jsonOut;

    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 watersense_accomplishments.pdf

    pdf = CkPdfW_Create();
    success = CkPdfW_LoadFile(pdf,L"qa_data/pdf/watersense_accomplishments.pdf");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        return;
    }

    json = CkJsonObjectW_Create();
    jsonOut = CkJsonObjectW_Create();
    success = CkPdfW_Optimize(pdf,L"audit",json,jsonOut);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        return;
    }

    wprintf(L"fileSize: %d\n",CkJsonObjectW_IntOf(jsonOut,L"fileSize"));
    wprintf(L"removeUnreferenced would save: %d\n",CkJsonObjectW_IntOf(jsonOut,L"estimatedSavings.removeUnreferenced"));

    //  Optimize with the defaults and see how many objects were actually dropped.
    success = CkPdfW_Optimize(pdf,L"optimize",json,jsonOut);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        return;
    }

    success = CkPdfW_WriteFile(pdf,L"qa_output/watersense_optimized.pdf");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        return;
    }

    wprintf(L"objects removed: %d\n",CkJsonObjectW_IntOf(jsonOut,L"actions.removeUnreferenced.count"));
    wprintf(L"bytes saved:     %d\n",CkJsonObjectW_IntOf(jsonOut,L"actions.removeUnreferenced.bytesSaved"));

    //  Nothing a page needs is ever removed: the walk starts at the document catalog and follows every
    //  reference, including the ones in annotations, the structure tree, named destinations and the
    //  optional-content configuration.
    wprintf(L"pages after: %d\n",CkPdfW_getNumPages(pdf));


    CkPdfW_Dispose(pdf);
    CkJsonObjectW_Dispose(json);
    CkJsonObjectW_Dispose(jsonOut);

    }