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

PDF Object Streams: generate, preserve or disable

See more PDF Optimization Examples

Demonstrates the objectStreams option. An object stream packs many small objects (dictionaries, not stream data) into one compressed stream, which is usually a worthwhile saving in a document with many pages or tags. "generate" (the default) packs them and writes a cross-reference stream; "preserve" keeps the file as it is -- packed if it already was, not packed if it was not; "disable" writes every object on its own with a classic cross-reference table, which is what a PDF 1.4 reader needs. Object streams require PDF 1.5 and are skipped for PDF/A-1.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Fac
oleobject loo_Json
oleobject loo_JsonOut
oleobject loo_Pdf1
oleobject loo_Pdf2
oleobject loo_Pdf3

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_Fac = create oleobject
li_rc = loo_Fac.ConnectToNewObject("Chilkat.FileAccess")
if li_rc < 0 then
    destroy loo_Fac
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

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

//  generate: pack the objects (the default).
loo_Pdf1 = create oleobject
li_rc = loo_Pdf1.ConnectToNewObject("Chilkat.Pdf")

li_Success = loo_Pdf1.LoadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf1.LastErrorText
    destroy loo_Fac
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Pdf1
    return
end if

loo_Json.UpdateString("objectStreams","generate")
li_Success = loo_Pdf1.Optimize("optimize",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf1.LastErrorText
    destroy loo_Fac
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Pdf1
    return
end if

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

Write-Debug "generate: " + string(loo_Fac.FileSize("qa_output/grand_canyon_objstm_generate.pdf")) + " bytes, objects packed: " + string(loo_JsonOut.IntOf("actions.objectStreams.count"))

//  disable: every object on its own, with a classic cross-reference table.
loo_Pdf2 = create oleobject
li_rc = loo_Pdf2.ConnectToNewObject("Chilkat.Pdf")

li_Success = loo_Pdf2.LoadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf2.LastErrorText
    destroy loo_Fac
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Pdf1
    destroy loo_Pdf2
    return
end if

loo_Json.UpdateString("objectStreams","disable")
li_Success = loo_Pdf2.Optimize("optimize",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf2.LastErrorText
    destroy loo_Fac
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Pdf1
    destroy loo_Pdf2
    return
end if

li_Success = loo_Pdf2.WriteFile("qa_output/grand_canyon_objstm_disable.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf2.LastErrorText
    destroy loo_Fac
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Pdf1
    destroy loo_Pdf2
    return
end if

Write-Debug "disable:  " + string(loo_Fac.FileSize("qa_output/grand_canyon_objstm_disable.pdf")) + " bytes"

//  preserve: leave the file's own structure alone.  Use it when the output must stay readable by
//  whatever reads the input today.
loo_Pdf3 = create oleobject
li_rc = loo_Pdf3.ConnectToNewObject("Chilkat.Pdf")

li_Success = loo_Pdf3.LoadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf3.LastErrorText
    destroy loo_Fac
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Pdf1
    destroy loo_Pdf2
    destroy loo_Pdf3
    return
end if

loo_Json.UpdateString("objectStreams","preserve")
li_Success = loo_Pdf3.Optimize("optimize",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf3.LastErrorText
    destroy loo_Fac
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Pdf1
    destroy loo_Pdf2
    destroy loo_Pdf3
    return
end if

li_Success = loo_Pdf3.WriteFile("qa_output/grand_canyon_objstm_preserve.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf3.LastErrorText
    destroy loo_Fac
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Pdf1
    destroy loo_Pdf2
    destroy loo_Pdf3
    return
end if

Write-Debug "preserve: " + string(loo_Fac.FileSize("qa_output/grand_canyon_objstm_preserve.pdf")) + " bytes"


destroy loo_Fac
destroy loo_Json
destroy loo_JsonOut
destroy loo_Pdf1
destroy loo_Pdf2
destroy loo_Pdf3