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

Visual Basic 6.0
Dim success As Long
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

Dim pdf As New ChilkatPdf
success = pdf.LoadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf")
If (success = 0) Then
    Debug.Print pdf.LastErrorText
    Exit Sub
End If

'  "audit" takes the same options as "optimize", so an empty JSON means the default options.
Dim json As New ChilkatJsonObject
Dim jsonOut As New ChilkatJsonObject
success = pdf.Optimize("audit",json,jsonOut)
If (success = 0) Then
    Debug.Print pdf.LastErrorText
    Exit Sub
End If

Debug.Print "fileSize: " & jsonOut.IntOf("fileSize")
Debug.Print "pdfVersion: " & jsonOut.StringOf("pdfVersion")
Debug.Print "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.
Debug.Print "images:        " & jsonOut.IntOf("usage.images.bytes") & " in " & jsonOut.IntOf("usage.images.count") & " objects"
Debug.Print "fonts:         " & jsonOut.IntOf("usage.fonts.bytes") & " in " & jsonOut.IntOf("usage.fonts.count") & " objects"
Debug.Print "pageContent:   " & jsonOut.IntOf("usage.pageContent.bytes")
Debug.Print "formXObjects:  " & jsonOut.IntOf("usage.formXObjects.bytes")
Debug.Print "annotations:   " & jsonOut.IntOf("usage.annotations.bytes")
Debug.Print "structureTree: " & jsonOut.IntOf("usage.structureTree.bytes")
Debug.Print "metadata:      " & jsonOut.IntOf("usage.metadata.bytes")
Debug.Print "embeddedFiles: " & jsonOut.IntOf("usage.embeddedFiles.bytes")
Debug.Print "oldRevisions:  " & jsonOut.IntOf("usage.oldRevisions.bytes")
Debug.Print "overhead:      " & jsonOut.IntOf("usage.overhead.bytes")

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

Dim est As New ChilkatJsonObject
success = jsonOut.ObjectOf2("estimatedSavings",est)
If (success = 0) Then
    Debug.Print jsonOut.LastErrorText
    Exit Sub
End If

Dim i As Long
i = 0
Dim numEst As Long
numEst = est.Size
Do While i < numEst
    Dim optionName As String
    optionName = est.NameAt(i)
    Debug.Print "   " & optionName & ": " & est.IntOf(optionName)
    i = i + 1
Loop