Sample code for 30+ languages & platforms
VBScript Requires Chilkat v11.7.0+

Where a PDF's Bytes Go: the Optimize Audit

See more PDF Optimization Examples

Demonstrates the "audit" operation, which measures the loaded PDF and changes nothing. The report says how large the file is, which PDF version it claims, whether it claims to be PDF/A, where its bytes go ("usage", as stored in the file, so compressed sizes), and how many bytes each lossless option would be expected to save ("estimatedSavings"). Use it to decide whether optimizing is worth the trouble, and which options matter for this file.

Chilkat VBScript Downloads

VBScript
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 grand_canyon_hiking_guide.pdf

set pdf = CreateObject("Chilkat.Pdf")
success = pdf.LoadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf")
If (success = 0) Then
    outFile.WriteLine(pdf.LastErrorText)
    WScript.Quit
End If

'  "audit" takes the same options as "optimize", so an empty JSON means the default options.
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

outFile.WriteLine("fileSize: " & jsonOut.IntOf("fileSize"))
outFile.WriteLine("pdfVersion: " & jsonOut.StringOf("pdfVersion"))
outFile.WriteLine("pdfa: " & jsonOut.StringOf("pdfa"))

'  Where the bytes go.  Every category is present even when it is 0, and the sizes are the stored
'  (compressed) sizes, so they add up to fileSize.  "overhead" is what is left: the cross-reference
'  tables, the object headers and the white space between objects.
outFile.WriteLine("images:        " & jsonOut.IntOf("usage.images.bytes") & " in " & jsonOut.IntOf("usage.images.count") & " objects")
outFile.WriteLine("fonts:         " & jsonOut.IntOf("usage.fonts.bytes") & " in " & jsonOut.IntOf("usage.fonts.count") & " objects")
outFile.WriteLine("pageContent:   " & jsonOut.IntOf("usage.pageContent.bytes"))
outFile.WriteLine("formXObjects:  " & jsonOut.IntOf("usage.formXObjects.bytes"))
outFile.WriteLine("annotations:   " & jsonOut.IntOf("usage.annotations.bytes"))
outFile.WriteLine("structureTree: " & jsonOut.IntOf("usage.structureTree.bytes"))
outFile.WriteLine("metadata:      " & jsonOut.IntOf("usage.metadata.bytes"))
outFile.WriteLine("embeddedFiles: " & jsonOut.IntOf("usage.embeddedFiles.bytes"))
outFile.WriteLine("oldRevisions:  " & jsonOut.IntOf("usage.oldRevisions.bytes"))
outFile.WriteLine("overhead:      " & jsonOut.IntOf("usage.overhead.bytes"))

'  What each option is expected to save.  An option that would save nothing is left out.
outFile.WriteLine("estimated total: " & jsonOut.IntOf("estimatedSavings.total"))

set est = CreateObject("Chilkat.JsonObject")
success = jsonOut.ObjectOf2("estimatedSavings",est)
If (success = 0) Then
    outFile.WriteLine(jsonOut.LastErrorText)
    WScript.Quit
End If

i = 0
numEst = est.Size
Do While i < numEst
    optionName = est.NameAt(i)
    outFile.WriteLine("   " & optionName & ": " & est.IntOf(optionName))
    i = i + 1
Loop

outFile.Close