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

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

//  "audit" takes the same options as "optimize", so an empty JSON means the default options.
Dim json As New Chilkat.JsonObject
Dim jsonOut As New Chilkat.JsonObject
success = pdf.Optimize("audit",json,jsonOut)
If (success = False) Then
    System.DebugLog(pdf.LastErrorText)
    Return
End If

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

//  What each option is expected to save.  An option that would save nothing is left out.
System.DebugLog("estimated total: " + Str(jsonOut.IntOf("estimatedSavings.total")))

Dim est As New Chilkat.JsonObject
success = jsonOut.ObjectOf2("estimatedSavings",est)
If (success = False) Then
    System.DebugLog(jsonOut.LastErrorText)
    Return
End If

Dim i As Int32
i = 0
Dim numEst As Int32
numEst = est.Size
While i < numEst
    Dim optionName As String
    optionName = est.NameAt(i)
    System.DebugLog("   " + optionName + ": " + Str(est.IntOf(optionName)))
    i = i + 1
Wend