Sample code for 30+ languages & platforms
PureBasic Requires Chilkat v11.7.0+

Optimize a PDF Without Rewriting It (Incremental Update)

See more PDF Optimization Examples

Demonstrates the one combination of options that does not rewrite the file. With dropOldRevisions false and objectStreams "disable", the optimized objects are appended to the original bytes as an incremental update: every byte of the input is still there, byte for byte, followed by the changes and a new cross-reference section. That is what keeps an existing digital signature valid, and what lets a reader that has the original cached fetch only the tail. The file gets a little larger before it gets smaller, so this is for compatibility, not for size.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkFileAccess.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkPdf.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 watersense_accomplishments.pdf

    pdf.i = CkPdf::ckCreate()
    If pdf.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/watersense_accomplishments.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        ProcedureReturn
    EndIf

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateBool(json,"dropOldRevisions",0)
    CkJsonObject::ckUpdateString(json,"objectStreams","disable")

    jsonOut.i = CkJsonObject::ckCreate()
    If jsonOut.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPdf::ckOptimize(pdf,"optimize",json,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    success = CkPdf::ckWriteFile(pdf,"qa_output/watersense_incremental.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    fac.i = CkFileAccess::ckCreate()
    If fac.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    Debug "original: " + Str(CkJsonObject::ckIntOf(jsonOut,"fileSize"))
    Debug "written:  " + Str(CkFileAccess::ckFileSize(fac,"qa_output/watersense_incremental.pdf"))

    ;  Everything the optimizer changed is in the appended part; the original bytes are the start of the
    ;  new file.  Any other combination of options -- dropOldRevisions, or generated object streams --
    ;  rewrites the document instead.
    Debug "bytesSaved: " + Str(CkJsonObject::ckIntOf(jsonOut,"bytesSaved"))


    CkPdf::ckDispose(pdf)
    CkJsonObject::ckDispose(json)
    CkJsonObject::ckDispose(jsonOut)
    CkFileAccess::ckDispose(fac)


    ProcedureReturn
EndProcedure