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

Choose Which PDF Optimizations to Apply

See more PDF Optimization Examples

Demonstrates turning individual optimizations off. Every option is on by default except the two reserved ones; setting one to false leaves that part of the file alone. Here the old revisions are kept (so the output is an incremental update that leaves the original bytes untouched) and the fonts are not subset, which is what a workflow that will later fill in form fields or replace text needs. The "actions" in the report name only the options that did something.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPdf
    Variant vJson
    Handle hoJson
    Variant vJsonOut
    Handle hoJsonOut
    Variant vActions
    Handle hoActions
    Variant vOneAction
    Handle hoOneAction
    Integer i
    Integer iNumActions
    String sActionName
    String sTemp1
    Integer iTemp1
    Integer iTemp2

    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

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End

    //  The options, all boolean, all true by default:
    //    compressUncompressed    compress streams stored with no filter
    //    recompressFlate         deflate again at the chosen level
    //    replaceLegacyFilters    re-encode LZW, RunLength, ASCIIHex and ASCII85 streams as Flate
    //    imagePredictors         apply a PNG predictor where it makes an image stream smaller
    //    dedupeStreams           merge streams whose contents are identical
    //    dedupeObjects           merge objects whose contents are identical
    //    removeUnreferenced      drop objects nothing refers to
    //    removeUnusedResources   drop resource entries no page uses
    //    removeUnusedLayers      drop optional-content groups nothing uses
    //    coalesceContent         join a page's content streams into one
    //    tidyContent             remove redundant operators and white space from page content
    //    subsetFonts             keep only the glyphs the document uses
    //    dropOldRevisions        drop superseded revisions (a full rewrite)
    //  
    //  mergeFontSubsets and compactType1Fonts are reserved for a later release and are reported in
    //  "skipped" when requested.  See the "Why an Optimization Was Skipped" example.

    Get ComUpdateBool Of hoJson "subsetFonts" False To iSuccess
    Get ComUpdateBool Of hoJson "dropOldRevisions" False To iSuccess

    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 "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_some_options.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComIntOf Of hoJsonOut "bytesSaved" To iTemp1
    Showln "bytesSaved: " iTemp1

    //  Only the options that actually did something appear under "actions", each with the number of
    //  objects it affected and the bytes it saved.
    Get Create (RefClass(cComChilkatJsonObject)) To hoActions
    If (Not(IsComObjectCreated(hoActions))) Begin
        Send CreateComObject of hoActions
    End
    Get pvComObject of hoActions to vActions
    Get ComObjectOf2 Of hoJsonOut "actions" vActions To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoJsonOut To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoOneAction
    If (Not(IsComObjectCreated(hoOneAction))) Begin
        Send CreateComObject of hoOneAction
    End
    Move 0 To i
    Get ComSize Of hoActions To iNumActions
    While (i < iNumActions)
        Get ComNameAt Of hoActions i To sActionName
        Get pvComObject of hoOneAction to vOneAction
        Get ComObjectOf2 Of hoActions sActionName vOneAction To iSuccess
        If (iSuccess = False) Begin
            Get ComLastErrorText Of hoActions To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        Get ComIntOf Of hoOneAction "bytesSaved" To iTemp1
        Get ComIntOf Of hoOneAction "count" To iTemp2
        Showln sActionName ": " iTemp1 " bytes, " iTemp2 " objects"
        Move (i + 1) To i
    Loop



End_Procedure