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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut
oleobject loo_Est
long i
long li_NumEst
string ls_OptionName

li_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

loo_Pdf = create oleobject
li_rc = loo_Pdf.ConnectToNewObject("Chilkat.Pdf")
if li_rc < 0 then
    destroy loo_Pdf
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Pdf.LoadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    return
end if

//  "audit" takes the same options as "optimize", so an empty JSON means the default options.
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_JsonOut = create oleobject
li_rc = loo_JsonOut.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Pdf.Optimize("audit",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

Write-Debug "fileSize: " + string(loo_JsonOut.IntOf("fileSize"))
Write-Debug "pdfVersion: " + loo_JsonOut.StringOf("pdfVersion")
Write-Debug "pdfa: " + loo_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.
Write-Debug "images:        " + string(loo_JsonOut.IntOf("usage.images.bytes")) + " in " + string(loo_JsonOut.IntOf("usage.images.count")) + " objects"
Write-Debug "fonts:         " + string(loo_JsonOut.IntOf("usage.fonts.bytes")) + " in " + string(loo_JsonOut.IntOf("usage.fonts.count")) + " objects"
Write-Debug "pageContent:   " + string(loo_JsonOut.IntOf("usage.pageContent.bytes"))
Write-Debug "formXObjects:  " + string(loo_JsonOut.IntOf("usage.formXObjects.bytes"))
Write-Debug "annotations:   " + string(loo_JsonOut.IntOf("usage.annotations.bytes"))
Write-Debug "structureTree: " + string(loo_JsonOut.IntOf("usage.structureTree.bytes"))
Write-Debug "metadata:      " + string(loo_JsonOut.IntOf("usage.metadata.bytes"))
Write-Debug "embeddedFiles: " + string(loo_JsonOut.IntOf("usage.embeddedFiles.bytes"))
Write-Debug "oldRevisions:  " + string(loo_JsonOut.IntOf("usage.oldRevisions.bytes"))
Write-Debug "overhead:      " + string(loo_JsonOut.IntOf("usage.overhead.bytes"))

//  What each option is expected to save.  An option that would save nothing is left out.
Write-Debug "estimated total: " + string(loo_JsonOut.IntOf("estimatedSavings.total"))

loo_Est = create oleobject
li_rc = loo_Est.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_JsonOut.ObjectOf2("estimatedSavings",loo_Est)
if li_Success = 0 then
    Write-Debug loo_JsonOut.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Est
    return
end if

i = 0
li_NumEst = loo_Est.Size
do while i < li_NumEst
    ls_OptionName = loo_Est.NameAt(i)
    Write-Debug "   " + ls_OptionName + ": " + string(loo_Est.IntOf(ls_OptionName))
    i = i + 1
loop


destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_Est