VBScript Requires Chilkat v11.7.0+
VBScript
Audit a PDF First, Optimize Only If It Is Worth It
See more PDF Optimization Examples
Demonstrates deciding whether to rewrite a PDF at all. "audit" costs a fraction of a second and changes nothing, so a batch job can audit every file and optimize only those where enough would be saved. Here the threshold is a tenth of the file; a file already as small as it can be is left alone.Chilkat VBScript Downloads
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)
success = 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
set pdf = CreateObject("Chilkat.Pdf")
success = pdf.LoadFile("qa_data/pdf/watersense_accomplishments.pdf")
If (success = 0) Then
outFile.WriteLine(pdf.LastErrorText)
WScript.Quit
End If
set json = CreateObject("Chilkat.JsonObject")
set jsonOut = CreateObject("Chilkat.JsonObject")
success = pdf.Optimize("audit",json,jsonOut)
If (success = 0) Then
outFile.WriteLine(pdf.LastErrorText)
WScript.Quit
End If
fileSize = jsonOut.IntOf("fileSize")
estimated = jsonOut.IntOf("estimatedSavings.total")
outFile.WriteLine("fileSize: " & fileSize)
outFile.WriteLine("estimated: " & estimated)
' Only bother when at least a tenth of the file would go.
If (estimated > (fileSize / 10)) Then
outFile.WriteLine("Worth optimizing.")
success = pdf.Optimize("optimize",json,jsonOut)
If (success = 0) Then
outFile.WriteLine(pdf.LastErrorText)
WScript.Quit
End If
success = pdf.WriteFile("qa_output/watersense_optimized.pdf")
If (success = 0) Then
outFile.WriteLine(pdf.LastErrorText)
WScript.Quit
End If
outFile.WriteLine("bytesSaved: " & jsonOut.IntOf("bytesSaved"))
Else
outFile.WriteLine("Already small enough; left as it is.")
End If
outFile.Close