Tcl Requires Chilkat v11.7.0+
Tcl
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 Tcl Downloads
load ./chilkat.dll
set 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 [new_CkPdf]
set success [CkPdf_LoadFile $pdf "qa_data/pdf/grand_canyon_hiking_guide.pdf"]
if {$success == 0} then {
puts [CkPdf_lastErrorText $pdf]
delete_CkPdf $pdf
exit
}
# "audit" takes the same options as "optimize", so an empty JSON means the default options.
set json [new_CkJsonObject]
set jsonOut [new_CkJsonObject]
set success [CkPdf_Optimize $pdf "audit" $json $jsonOut]
if {$success == 0} then {
puts [CkPdf_lastErrorText $pdf]
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
exit
}
puts "fileSize: [CkJsonObject_IntOf $jsonOut fileSize]"
puts "pdfVersion: [CkJsonObject_stringOf $jsonOut pdfVersion]"
puts "pdfa: [CkJsonObject_stringOf $jsonOut 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.
puts "images: [CkJsonObject_IntOf $jsonOut usage.images.bytes] in [CkJsonObject_IntOf $jsonOut usage.images.count] objects"
puts "fonts: [CkJsonObject_IntOf $jsonOut usage.fonts.bytes] in [CkJsonObject_IntOf $jsonOut usage.fonts.count] objects"
puts "pageContent: [CkJsonObject_IntOf $jsonOut usage.pageContent.bytes]"
puts "formXObjects: [CkJsonObject_IntOf $jsonOut usage.formXObjects.bytes]"
puts "annotations: [CkJsonObject_IntOf $jsonOut usage.annotations.bytes]"
puts "structureTree: [CkJsonObject_IntOf $jsonOut usage.structureTree.bytes]"
puts "metadata: [CkJsonObject_IntOf $jsonOut usage.metadata.bytes]"
puts "embeddedFiles: [CkJsonObject_IntOf $jsonOut usage.embeddedFiles.bytes]"
puts "oldRevisions: [CkJsonObject_IntOf $jsonOut usage.oldRevisions.bytes]"
puts "overhead: [CkJsonObject_IntOf $jsonOut usage.overhead.bytes]"
# What each option is expected to save. An option that would save nothing is left out.
puts "estimated total: [CkJsonObject_IntOf $jsonOut estimatedSavings.total]"
set est [new_CkJsonObject]
set success [CkJsonObject_ObjectOf2 $jsonOut "estimatedSavings" $est]
if {$success == 0} then {
puts [CkJsonObject_lastErrorText $jsonOut]
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
delete_CkJsonObject $est
exit
}
set i 0
set numEst [CkJsonObject_get_Size $est]
while {$i < $numEst} {
set optionName [CkJsonObject_nameAt $est $i]
puts " $optionName: [CkJsonObject_IntOf $est $optionName]"
set i [expr $i + 1]
}
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
delete_CkJsonObject $est