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

Deflate Compression Level When Optimizing a PDF

See more PDF Optimization Examples

Demonstrates compressionLevel, which is the deflate level (1 to 9, 9 by default) used for every stream the optimizer re-encodes. Level 9 is the smallest and the slowest; level 1 is the quickest. The difference is usually a percent or two, so level 9 is the right default for a file that will be downloaded many times, and a lower level is worth considering only when a server optimizes PDFs on the fly. A value outside 1 to 9 is an error.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoFac
    Variant vJson
    Handle hoJson
    Variant vJsonOut
    Handle hoJsonOut
    Handle hoPdf1
    Handle hoPdf9
    Handle hoPdfBad
    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 watersense_accomplishments.pdf

    Get Create (RefClass(cComCkFileAccess)) To hoFac
    If (Not(IsComObjectCreated(hoFac))) Begin
        Send CreateComObject of hoFac
    End
    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 Create (RefClass(cComChilkatPdf)) To hoPdf1
    If (Not(IsComObjectCreated(hoPdf1))) Begin
        Send CreateComObject of hoPdf1
    End
    Get ComLoadFile Of hoPdf1 "qa_data/pdf/watersense_accomplishments.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf1 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

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

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

    Get ComFileSize Of hoFac "qa_output/watersense_level1.pdf" To iTemp1
    Showln "level 1: " iTemp1

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

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

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

    Get ComFileSize Of hoFac "qa_output/watersense_level9.pdf" To iTemp1
    Showln "level 9: " iTemp1

    //  A level outside 1..9 fails, and LastErrorText says why.
    Get Create (RefClass(cComChilkatPdf)) To hoPdfBad
    If (Not(IsComObjectCreated(hoPdfBad))) Begin
        Send CreateComObject of hoPdfBad
    End
    Get ComLoadFile Of hoPdfBad "qa_data/pdf/watersense_accomplishments.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdfBad To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComUpdateInt Of hoJson "compressionLevel" 12 To iSuccess
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComOptimize Of hoPdfBad "optimize" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Showln "compressionLevel 12 was refused, as it should be."
    End



End_Procedure