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

B4X
Dim success As Boolean = 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 ChilkatPdf
pdf.Initialize("pdf")
success = pdf.LoadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf")
If success = False Then
    Log(pdf.LastErrorText)
    Return
End If


'  "audit" takes the same options as "optimize", so an empty JSON means the default options.
Dim json As ChilkatJsonObject
json.Initialize
Dim jsonOut As ChilkatJsonObject
jsonOut.Initialize
success = pdf.Optimize("audit", json, jsonOut)
If success = False Then
    Log(pdf.LastErrorText)
    Return
End If


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

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

Dim est As ChilkatJsonObject
est.Initialize
success = jsonOut.ObjectOf2("estimatedSavings", est)
If success = False Then
    Log(jsonOut.LastErrorText)
    Return
End If


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