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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPdf
    Variant vJson
    Handle hoJson
    Variant vJsonOut
    Handle hoJsonOut
    Handle hoFac
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

    //  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

    Get Create (RefClass(cComChilkatPdf)) To hoPdf
    If (Not(IsComObjectCreated(hoPdf))) Begin
        Send CreateComObject of hoPdf
    End
    Get ComLoadFile Of hoPdf "qa_data/pdf/grand_canyon_hiking_guide.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  How much of this file is superseded revisions?
    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
    If (Not(IsComObjectCreated(hoJsonOut))) Begin
        Send CreateComObject of hoJsonOut
    End
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComOptimize Of hoPdf "audit" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComIntOf Of hoJsonOut "fileSize" To iTemp1
    Showln "fileSize:     " iTemp1
    Get ComIntOf Of hoJsonOut "usage.oldRevisions.bytes" To iTemp1
    Showln "oldRevisions: " iTemp1

    //  Drop them, and nothing else, so that the saving is attributable.
    Get ComUpdateBool Of hoJson "compressUncompressed" False To iSuccess
    Get ComUpdateBool Of hoJson "recompressFlate" False To iSuccess
    Get ComUpdateBool Of hoJson "replaceLegacyFilters" False To iSuccess
    Get ComUpdateBool Of hoJson "imagePredictors" False To iSuccess
    Get ComUpdateBool Of hoJson "dedupeStreams" False To iSuccess
    Get ComUpdateBool Of hoJson "dedupeObjects" False To iSuccess
    Get ComUpdateBool Of hoJson "removeUnreferenced" False To iSuccess
    Get ComUpdateBool Of hoJson "removeUnusedResources" False To iSuccess
    Get ComUpdateBool Of hoJson "removeUnusedLayers" False To iSuccess
    Get ComUpdateBool Of hoJson "coalesceContent" False To iSuccess
    Get ComUpdateBool Of hoJson "tidyContent" False To iSuccess
    Get ComUpdateBool Of hoJson "subsetFonts" False To iSuccess
    Get ComUpdateBool Of hoJson "dropOldRevisions" True To iSuccess

    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComOptimize Of hoPdf "optimize" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComWriteFile Of hoPdf "qa_output/grand_canyon_one_revision.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  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.
    Get Create (RefClass(cComCkFileAccess)) To hoFac
    If (Not(IsComObjectCreated(hoFac))) Begin
        Send CreateComObject of hoFac
    End
    Get ComIntOf Of hoJsonOut "bytesSaved" To iTemp1
    Showln "bytesSaved (reported): " iTemp1
    Get ComFileSize Of hoFac "qa_output/grand_canyon_one_revision.pdf" To iTemp1
    Showln "written file:          " iTemp1


End_Procedure