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

Xojo Plugin
Dim success As Boolean
success = False

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

Dim fac As New Chilkat.FileAccess
Dim json As New Chilkat.JsonObject
Dim jsonOut As New Chilkat.JsonObject

Dim pdf1 As New Chilkat.Pdf
success = pdf1.LoadFile("qa_data/pdf/watersense_accomplishments.pdf")
If (success = False) Then
    System.DebugLog(pdf1.LastErrorText)
    Return
End If

success = json.UpdateInt("compressionLevel",1)
success = pdf1.Optimize("optimize",json,jsonOut)
If (success = False) Then
    System.DebugLog(pdf1.LastErrorText)
    Return
End If

success = pdf1.WriteFile("qa_output/watersense_level1.pdf")
If (success = False) Then
    System.DebugLog(pdf1.LastErrorText)
    Return
End If

System.DebugLog("level 1: " + Str(fac.FileSize("qa_output/watersense_level1.pdf")))

Dim pdf9 As New Chilkat.Pdf
success = pdf9.LoadFile("qa_data/pdf/watersense_accomplishments.pdf")
If (success = False) Then
    System.DebugLog(pdf9.LastErrorText)
    Return
End If

success = json.UpdateInt("compressionLevel",9)
success = pdf9.Optimize("optimize",json,jsonOut)
If (success = False) Then
    System.DebugLog(pdf9.LastErrorText)
    Return
End If

success = pdf9.WriteFile("qa_output/watersense_level9.pdf")
If (success = False) Then
    System.DebugLog(pdf9.LastErrorText)
    Return
End If

System.DebugLog("level 9: " + Str(fac.FileSize("qa_output/watersense_level9.pdf")))

//  A level outside 1..9 fails, and LastErrorText says why.
Dim pdfBad As New Chilkat.Pdf
success = pdfBad.LoadFile("qa_data/pdf/watersense_accomplishments.pdf")
If (success = False) Then
    System.DebugLog(pdfBad.LastErrorText)
    Return
End If

success = json.UpdateInt("compressionLevel",12)
success = pdfBad.Optimize("optimize",json,jsonOut)
If (success = False) Then
    System.DebugLog("compressionLevel 12 was refused, as it should be.")
End If