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

Xbase++
LOCAL nSuccess
LOCAL oFac
LOCAL oJson
LOCAL oJsonOut
LOCAL oPdf1
LOCAL oPdf9
LOCAL oPdfBad

nSuccess := 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

oFac := CreateObject("Chilkat.FileAccess")
oJson := CreateObject("Chilkat.JsonObject")
oJsonOut := CreateObject("Chilkat.JsonObject")

oPdf1 := CreateObject("Chilkat.Pdf")
nSuccess := oPdf1:LoadFile("qa_data/pdf/watersense_accomplishments.pdf")
IF (nSuccess == 0)
    ? oPdf1:LastErrorText
    oFac:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    oPdf1:destroy()
    RETURN
ENDIF

oJson:UpdateInt("compressionLevel", 1)
nSuccess := oPdf1:Optimize("optimize", oJson, oJsonOut)
IF (nSuccess == 0)
    ? oPdf1:LastErrorText
    oFac:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    oPdf1:destroy()
    RETURN
ENDIF

nSuccess := oPdf1:WriteFile("qa_output/watersense_level1.pdf")
IF (nSuccess == 0)
    ? oPdf1:LastErrorText
    oFac:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    oPdf1:destroy()
    RETURN
ENDIF

? "level 1: " + Str(oFac:FileSize("qa_output/watersense_level1.pdf"))

oPdf9 := CreateObject("Chilkat.Pdf")
nSuccess := oPdf9:LoadFile("qa_data/pdf/watersense_accomplishments.pdf")
IF (nSuccess == 0)
    ? oPdf9:LastErrorText
    oFac:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    oPdf1:destroy()
    oPdf9:destroy()
    RETURN
ENDIF

oJson:UpdateInt("compressionLevel", 9)
nSuccess := oPdf9:Optimize("optimize", oJson, oJsonOut)
IF (nSuccess == 0)
    ? oPdf9:LastErrorText
    oFac:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    oPdf1:destroy()
    oPdf9:destroy()
    RETURN
ENDIF

nSuccess := oPdf9:WriteFile("qa_output/watersense_level9.pdf")
IF (nSuccess == 0)
    ? oPdf9:LastErrorText
    oFac:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    oPdf1:destroy()
    oPdf9:destroy()
    RETURN
ENDIF

? "level 9: " + Str(oFac:FileSize("qa_output/watersense_level9.pdf"))

//  A level outside 1..9 fails, and LastErrorText says why.
oPdfBad := CreateObject("Chilkat.Pdf")
nSuccess := oPdfBad:LoadFile("qa_data/pdf/watersense_accomplishments.pdf")
IF (nSuccess == 0)
    ? oPdfBad:LastErrorText
    oFac:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    oPdf1:destroy()
    oPdf9:destroy()
    oPdfBad:destroy()
    RETURN
ENDIF

oJson:UpdateInt("compressionLevel", 12)
nSuccess := oPdfBad:Optimize("optimize", oJson, oJsonOut)
IF (nSuccess == 0)
    ? "compressionLevel 12 was refused, as it should be."
ENDIF

oFac:destroy()
oJson:destroy()
oJsonOut:destroy()
oPdf1:destroy()
oPdf9:destroy()
oPdfBad:destroy()