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

Remove a PDF's Old Revisions

See more PDF Optimization Examples

Demonstrates dropOldRevisions. A PDF that has been edited and saved several times carries every earlier revision: the superseded objects are still in the file, reachable through the older cross-reference sections. The audit reports them as "oldRevisions", and dropping them is often the single largest saving in a real document. Dropping them requires a full rewrite, which is why it invalidates existing digital signatures.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkPdfW.h>
#include <CkJsonObjectW.h>
#include <CkFileAccessW.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

    CkPdfW pdf;
    success = pdf.LoadFile(L"qa_data/pdf/grand_canyon_hiking_guide.pdf");
    if (success == false) {
        wprintf(L"%s\n",pdf.lastErrorText());
        return;
    }

    //  How much of this file is superseded revisions?
    CkJsonObjectW json;
    CkJsonObjectW jsonOut;
    success = pdf.Optimize(L"audit",json,jsonOut);
    if (success == false) {
        wprintf(L"%s\n",pdf.lastErrorText());
        return;
    }

    wprintf(L"fileSize:     %d\n",jsonOut.IntOf(L"fileSize"));
    wprintf(L"oldRevisions: %d\n",jsonOut.IntOf(L"usage.oldRevisions.bytes"));

    //  Drop them, and nothing else, so that the saving is attributable.
    json.UpdateBool(L"compressUncompressed",false);
    json.UpdateBool(L"recompressFlate",false);
    json.UpdateBool(L"replaceLegacyFilters",false);
    json.UpdateBool(L"imagePredictors",false);
    json.UpdateBool(L"dedupeStreams",false);
    json.UpdateBool(L"dedupeObjects",false);
    json.UpdateBool(L"removeUnreferenced",false);
    json.UpdateBool(L"removeUnusedResources",false);
    json.UpdateBool(L"removeUnusedLayers",false);
    json.UpdateBool(L"coalesceContent",false);
    json.UpdateBool(L"tidyContent",false);
    json.UpdateBool(L"subsetFonts",false);
    json.UpdateBool(L"dropOldRevisions",true);

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

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

    //  The saving is an estimate: what the old revisions occupied is known exactly, but the writer
    //  decides the final layout.  Measure the written file for the real number.
    CkFileAccessW fac;
    wprintf(L"bytesSaved (reported): %d\n",jsonOut.IntOf(L"bytesSaved"));
    wprintf(L"written file:          %d\n",fac.FileSize(L"qa_output/grand_canyon_one_revision.pdf"));
    }