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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPdf
    Variant vJson
    Handle hoJson
    Variant vJsonOut
    Handle hoJsonOut
    Variant vEst
    Handle hoEst
    Integer i
    Integer iNumEst
    String sOptionName
    String sTemp1
    Integer iTemp1
    Integer iTemp2

    Move False To iSuccess

    //  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

    Get Create (RefClass(cComChilkatPdf)) To hoPdf
    If (Not(IsComObjectCreated(hoPdf))) Begin
        Send CreateComObject of hoPdf
    End
    Get ComLoadFile Of hoPdf "qa_data/pdf/grand_canyon_hiking_guide.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  "audit" takes the same options as "optimize", so an empty JSON means the default options.
    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
    If (Not(IsComObjectCreated(hoJsonOut))) Begin
        Send CreateComObject of hoJsonOut
    End
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComOptimize Of hoPdf "audit" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComIntOf Of hoJsonOut "fileSize" To iTemp1
    Showln "fileSize: " iTemp1
    Get ComStringOf Of hoJsonOut "pdfVersion" To sTemp1
    Showln "pdfVersion: " sTemp1
    Get ComStringOf Of hoJsonOut "pdfa" To sTemp1
    Showln "pdfa: " sTemp1

    //  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.
    Get ComIntOf Of hoJsonOut "usage.images.bytes" To iTemp1
    Get ComIntOf Of hoJsonOut "usage.images.count" To iTemp2
    Showln "images:        " iTemp1 " in " iTemp2 " objects"
    Get ComIntOf Of hoJsonOut "usage.fonts.bytes" To iTemp1
    Get ComIntOf Of hoJsonOut "usage.fonts.count" To iTemp2
    Showln "fonts:         " iTemp1 " in " iTemp2 " objects"
    Get ComIntOf Of hoJsonOut "usage.pageContent.bytes" To iTemp1
    Showln "pageContent:   " iTemp1
    Get ComIntOf Of hoJsonOut "usage.formXObjects.bytes" To iTemp1
    Showln "formXObjects:  " iTemp1
    Get ComIntOf Of hoJsonOut "usage.annotations.bytes" To iTemp1
    Showln "annotations:   " iTemp1
    Get ComIntOf Of hoJsonOut "usage.structureTree.bytes" To iTemp1
    Showln "structureTree: " iTemp1
    Get ComIntOf Of hoJsonOut "usage.metadata.bytes" To iTemp1
    Showln "metadata:      " iTemp1
    Get ComIntOf Of hoJsonOut "usage.embeddedFiles.bytes" To iTemp1
    Showln "embeddedFiles: " iTemp1
    Get ComIntOf Of hoJsonOut "usage.oldRevisions.bytes" To iTemp1
    Showln "oldRevisions:  " iTemp1
    Get ComIntOf Of hoJsonOut "usage.overhead.bytes" To iTemp1
    Showln "overhead:      " iTemp1

    //  What each option is expected to save.  An option that would save nothing is left out.
    Get ComIntOf Of hoJsonOut "estimatedSavings.total" To iTemp1
    Showln "estimated total: " iTemp1

    Get Create (RefClass(cComChilkatJsonObject)) To hoEst
    If (Not(IsComObjectCreated(hoEst))) Begin
        Send CreateComObject of hoEst
    End
    Get pvComObject of hoEst to vEst
    Get ComObjectOf2 Of hoJsonOut "estimatedSavings" vEst To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoJsonOut To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Move 0 To i
    Get ComSize Of hoEst To iNumEst
    While (i < iNumEst)
        Get ComNameAt Of hoEst i To sOptionName
        Get ComIntOf Of hoEst sOptionName To iTemp1
        Showln "   " sOptionName ": " iTemp1
        Move (i + 1) To i
    Loop



End_Procedure