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

PureBasic
IncludeFile "CkPdf.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkFileAccess.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  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

    fac.i = CkFileAccess::ckCreate()
    If fac.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    jsonOut.i = CkJsonObject::ckCreate()
    If jsonOut.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    pdf1.i = CkPdf::ckCreate()
    If pdf1.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPdf::ckLoadFile(pdf1,"qa_data/pdf/watersense_accomplishments.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf1)
        CkFileAccess::ckDispose(fac)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkPdf::ckDispose(pdf1)
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateInt(json,"compressionLevel",1)
    success = CkPdf::ckOptimize(pdf1,"optimize",json,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf1)
        CkFileAccess::ckDispose(fac)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkPdf::ckDispose(pdf1)
        ProcedureReturn
    EndIf

    success = CkPdf::ckWriteFile(pdf1,"qa_output/watersense_level1.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf1)
        CkFileAccess::ckDispose(fac)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkPdf::ckDispose(pdf1)
        ProcedureReturn
    EndIf

    Debug "level 1: " + Str(CkFileAccess::ckFileSize(fac,"qa_output/watersense_level1.pdf"))

    pdf9.i = CkPdf::ckCreate()
    If pdf9.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPdf::ckLoadFile(pdf9,"qa_data/pdf/watersense_accomplishments.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf9)
        CkFileAccess::ckDispose(fac)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkPdf::ckDispose(pdf1)
        CkPdf::ckDispose(pdf9)
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateInt(json,"compressionLevel",9)
    success = CkPdf::ckOptimize(pdf9,"optimize",json,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf9)
        CkFileAccess::ckDispose(fac)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkPdf::ckDispose(pdf1)
        CkPdf::ckDispose(pdf9)
        ProcedureReturn
    EndIf

    success = CkPdf::ckWriteFile(pdf9,"qa_output/watersense_level9.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf9)
        CkFileAccess::ckDispose(fac)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkPdf::ckDispose(pdf1)
        CkPdf::ckDispose(pdf9)
        ProcedureReturn
    EndIf

    Debug "level 9: " + Str(CkFileAccess::ckFileSize(fac,"qa_output/watersense_level9.pdf"))

    ;  A level outside 1..9 fails, and LastErrorText says why.
    pdfBad.i = CkPdf::ckCreate()
    If pdfBad.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPdf::ckLoadFile(pdfBad,"qa_data/pdf/watersense_accomplishments.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdfBad)
        CkFileAccess::ckDispose(fac)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkPdf::ckDispose(pdf1)
        CkPdf::ckDispose(pdf9)
        CkPdf::ckDispose(pdfBad)
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateInt(json,"compressionLevel",12)
    success = CkPdf::ckOptimize(pdfBad,"optimize",json,jsonOut)
    If success = 0
        Debug "compressionLevel 12 was refused, as it should be."
    EndIf



    CkFileAccess::ckDispose(fac)
    CkJsonObject::ckDispose(json)
    CkJsonObject::ckDispose(jsonOut)
    CkPdf::ckDispose(pdf1)
    CkPdf::ckDispose(pdf9)
    CkPdf::ckDispose(pdfBad)


    ProcedureReturn
EndProcedure