AutoIt Requires Chilkat v11.7.0+
AutoIt
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 AutoIt Downloads
Local $bSuccess = 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
$oFac = ObjCreate("Chilkat.FileAccess")
$oJson = ObjCreate("Chilkat.JsonObject")
$oJsonOut = ObjCreate("Chilkat.JsonObject")
$oPdf1 = ObjCreate("Chilkat.Pdf")
$bSuccess = $oPdf1.LoadFile("qa_data/pdf/watersense_accomplishments.pdf")
If ($bSuccess = False) Then
ConsoleWrite($oPdf1.LastErrorText & @CRLF)
Exit
EndIf
$oJson.UpdateInt("compressionLevel",1)
$bSuccess = $oPdf1.Optimize("optimize",$oJson,$oJsonOut)
If ($bSuccess = False) Then
ConsoleWrite($oPdf1.LastErrorText & @CRLF)
Exit
EndIf
$bSuccess = $oPdf1.WriteFile("qa_output/watersense_level1.pdf")
If ($bSuccess = False) Then
ConsoleWrite($oPdf1.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("level 1: " & $oFac.FileSize("qa_output/watersense_level1.pdf") & @CRLF)
$oPdf9 = ObjCreate("Chilkat.Pdf")
$bSuccess = $oPdf9.LoadFile("qa_data/pdf/watersense_accomplishments.pdf")
If ($bSuccess = False) Then
ConsoleWrite($oPdf9.LastErrorText & @CRLF)
Exit
EndIf
$oJson.UpdateInt("compressionLevel",9)
$bSuccess = $oPdf9.Optimize("optimize",$oJson,$oJsonOut)
If ($bSuccess = False) Then
ConsoleWrite($oPdf9.LastErrorText & @CRLF)
Exit
EndIf
$bSuccess = $oPdf9.WriteFile("qa_output/watersense_level9.pdf")
If ($bSuccess = False) Then
ConsoleWrite($oPdf9.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("level 9: " & $oFac.FileSize("qa_output/watersense_level9.pdf") & @CRLF)
; A level outside 1..9 fails, and LastErrorText says why.
$oPdfBad = ObjCreate("Chilkat.Pdf")
$bSuccess = $oPdfBad.LoadFile("qa_data/pdf/watersense_accomplishments.pdf")
If ($bSuccess = False) Then
ConsoleWrite($oPdfBad.LastErrorText & @CRLF)
Exit
EndIf
$oJson.UpdateInt("compressionLevel",12)
$bSuccess = $oPdfBad.Optimize("optimize",$oJson,$oJsonOut)
If ($bSuccess = False) Then
ConsoleWrite("compressionLevel 12 was refused, as it should be." & @CRLF)
EndIf