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

Xbase++
LOCAL nSuccess
LOCAL oFac
LOCAL oJson
LOCAL oJsonOut
LOCAL oPdf1
LOCAL oPdf2
LOCAL oPdf3

nSuccess := 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

oFac := CreateObject("Chilkat.FileAccess")
oJson := CreateObject("Chilkat.JsonObject")
oJsonOut := CreateObject("Chilkat.JsonObject")

//  generate: pack the objects (the default).
oPdf1 := CreateObject("Chilkat.Pdf")
nSuccess := oPdf1:LoadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf")
IF (nSuccess == 0)
    ? oPdf1:LastErrorText
    oFac:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    oPdf1:destroy()
    RETURN
ENDIF

oJson:UpdateString("objectStreams", "generate")
nSuccess := oPdf1:Optimize("optimize", oJson, oJsonOut)
IF (nSuccess == 0)
    ? oPdf1:LastErrorText
    oFac:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    oPdf1:destroy()
    RETURN
ENDIF

nSuccess := oPdf1:WriteFile("qa_output/grand_canyon_objstm_generate.pdf")
IF (nSuccess == 0)
    ? oPdf1:LastErrorText
    oFac:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    oPdf1:destroy()
    RETURN
ENDIF

? "generate: " + Str(oFac:FileSize("qa_output/grand_canyon_objstm_generate.pdf")) + " bytes, objects packed: " + Str(oJsonOut:IntOf("actions.objectStreams.count"))

//  disable: every object on its own, with a classic cross-reference table.
oPdf2 := CreateObject("Chilkat.Pdf")
nSuccess := oPdf2:LoadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf")
IF (nSuccess == 0)
    ? oPdf2:LastErrorText
    oFac:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    oPdf1:destroy()
    oPdf2:destroy()
    RETURN
ENDIF

oJson:UpdateString("objectStreams", "disable")
nSuccess := oPdf2:Optimize("optimize", oJson, oJsonOut)
IF (nSuccess == 0)
    ? oPdf2:LastErrorText
    oFac:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    oPdf1:destroy()
    oPdf2:destroy()
    RETURN
ENDIF

nSuccess := oPdf2:WriteFile("qa_output/grand_canyon_objstm_disable.pdf")
IF (nSuccess == 0)
    ? oPdf2:LastErrorText
    oFac:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    oPdf1:destroy()
    oPdf2:destroy()
    RETURN
ENDIF

? "disable:  " + Str(oFac: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.
oPdf3 := CreateObject("Chilkat.Pdf")
nSuccess := oPdf3:LoadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf")
IF (nSuccess == 0)
    ? oPdf3:LastErrorText
    oFac:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    oPdf1:destroy()
    oPdf2:destroy()
    oPdf3:destroy()
    RETURN
ENDIF

oJson:UpdateString("objectStreams", "preserve")
nSuccess := oPdf3:Optimize("optimize", oJson, oJsonOut)
IF (nSuccess == 0)
    ? oPdf3:LastErrorText
    oFac:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    oPdf1:destroy()
    oPdf2:destroy()
    oPdf3:destroy()
    RETURN
ENDIF

nSuccess := oPdf3:WriteFile("qa_output/grand_canyon_objstm_preserve.pdf")
IF (nSuccess == 0)
    ? oPdf3:LastErrorText
    oFac:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    oPdf1:destroy()
    oPdf2:destroy()
    oPdf3:destroy()
    RETURN
ENDIF

? "preserve: " + Str(oFac:FileSize("qa_output/grand_canyon_objstm_preserve.pdf")) + " bytes"

oFac:destroy()
oJson:destroy()
oJsonOut:destroy()
oPdf1:destroy()
oPdf2:destroy()
oPdf3:destroy()