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

Choose Which PDF Optimizations to Apply

See more PDF Optimization Examples

Demonstrates turning individual optimizations off. Every option is on by default except the two reserved ones; setting one to false leaves that part of the file alone. Here the old revisions are kept (so the output is an incremental update that leaves the original bytes untouched) and the fonts are not subset, which is what a workflow that will later fill in form fields or replace text needs. The "actions" in the report name only the options that did something.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut
oleobject loo_Actions
oleobject loo_OneAction
long i
long li_NumActions
string ls_ActionName

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

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

//  The options, all boolean, all true by default:
//    compressUncompressed    compress streams stored with no filter
//    recompressFlate         deflate again at the chosen level
//    replaceLegacyFilters    re-encode LZW, RunLength, ASCIIHex and ASCII85 streams as Flate
//    imagePredictors         apply a PNG predictor where it makes an image stream smaller
//    dedupeStreams           merge streams whose contents are identical
//    dedupeObjects           merge objects whose contents are identical
//    removeUnreferenced      drop objects nothing refers to
//    removeUnusedResources   drop resource entries no page uses
//    removeUnusedLayers      drop optional-content groups nothing uses
//    coalesceContent         join a page's content streams into one
//    tidyContent             remove redundant operators and white space from page content
//    subsetFonts             keep only the glyphs the document uses
//    dropOldRevisions        drop superseded revisions (a full rewrite)
//  
//  mergeFontSubsets and compactType1Fonts are reserved for a later release and are reported in
//  "skipped" when requested.  See the "Why an Optimization Was Skipped" example.

loo_Json.UpdateBool("subsetFonts",0)
loo_Json.UpdateBool("dropOldRevisions",0)

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

li_Success = loo_Pdf.Optimize("optimize",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

li_Success = loo_Pdf.WriteFile("qa_output/grand_canyon_some_options.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

Write-Debug "bytesSaved: " + string(loo_JsonOut.IntOf("bytesSaved"))

//  Only the options that actually did something appear under "actions", each with the number of
//  objects it affected and the bytes it saved.
loo_Actions = create oleobject
li_rc = loo_Actions.ConnectToNewObject("Chilkat.JsonObject")

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

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

i = 0
li_NumActions = loo_Actions.Size
do while i < li_NumActions
    ls_ActionName = loo_Actions.NameAt(i)
    li_Success = loo_Actions.ObjectOf2(ls_ActionName,loo_OneAction)
    if li_Success = 0 then
        Write-Debug loo_Actions.LastErrorText
        destroy loo_Pdf
        destroy loo_Json
        destroy loo_JsonOut
        destroy loo_Actions
        destroy loo_OneAction
        return
    end if

    Write-Debug ls_ActionName + ": " + string(loo_OneAction.IntOf("bytesSaved")) + " bytes, " + string(loo_OneAction.IntOf("count")) + " objects"
    i = i + 1
loop


destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_Actions
destroy loo_OneAction