PureBasic Requires Chilkat v11.7.0+
PureBasic
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 PureBasic Downloads
IncludeFile "CkPdf.pb"
IncludeFile "CkJsonObject.pb"
Procedure ChilkatExample()
success.i = 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
pdf.i = CkPdf::ckCreate()
If pdf.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/grand_canyon_hiking_guide.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
ProcedureReturn
EndIf
; "audit" takes the same options as "optimize", so an empty JSON means the default options.
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
jsonOut.i = CkJsonObject::ckCreate()
If jsonOut.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckOptimize(pdf,"audit",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
Debug "fileSize: " + Str(CkJsonObject::ckIntOf(jsonOut,"fileSize"))
Debug "pdfVersion: " + CkJsonObject::ckStringOf(jsonOut,"pdfVersion")
Debug "pdfa: " + CkJsonObject::ckStringOf(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.
Debug "images: " + Str(CkJsonObject::ckIntOf(jsonOut,"usage.images.bytes")) + " in " + Str(CkJsonObject::ckIntOf(jsonOut,"usage.images.count")) + " objects"
Debug "fonts: " + Str(CkJsonObject::ckIntOf(jsonOut,"usage.fonts.bytes")) + " in " + Str(CkJsonObject::ckIntOf(jsonOut,"usage.fonts.count")) + " objects"
Debug "pageContent: " + Str(CkJsonObject::ckIntOf(jsonOut,"usage.pageContent.bytes"))
Debug "formXObjects: " + Str(CkJsonObject::ckIntOf(jsonOut,"usage.formXObjects.bytes"))
Debug "annotations: " + Str(CkJsonObject::ckIntOf(jsonOut,"usage.annotations.bytes"))
Debug "structureTree: " + Str(CkJsonObject::ckIntOf(jsonOut,"usage.structureTree.bytes"))
Debug "metadata: " + Str(CkJsonObject::ckIntOf(jsonOut,"usage.metadata.bytes"))
Debug "embeddedFiles: " + Str(CkJsonObject::ckIntOf(jsonOut,"usage.embeddedFiles.bytes"))
Debug "oldRevisions: " + Str(CkJsonObject::ckIntOf(jsonOut,"usage.oldRevisions.bytes"))
Debug "overhead: " + Str(CkJsonObject::ckIntOf(jsonOut,"usage.overhead.bytes"))
; What each option is expected to save. An option that would save nothing is left out.
Debug "estimated total: " + Str(CkJsonObject::ckIntOf(jsonOut,"estimatedSavings.total"))
est.i = CkJsonObject::ckCreate()
If est.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJsonObject::ckObjectOf2(jsonOut,"estimatedSavings",est)
If success = 0
Debug CkJsonObject::ckLastErrorText(jsonOut)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkJsonObject::ckDispose(est)
ProcedureReturn
EndIf
i.i = 0
numEst.i = CkJsonObject::ckSize(est)
While i < numEst
optionName.s = CkJsonObject::ckNameAt(est,i)
Debug " " + optionName + ": " + Str(CkJsonObject::ckIntOf(est,optionName))
i = i + 1
Wend
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkJsonObject::ckDispose(est)
ProcedureReturn
EndProcedure