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

Lua
local chilkat = require("chilkat")

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

local fac = chilkat.FileAccess.new()
local json = chilkat.JsonObject.new()
local jsonOut = chilkat.JsonObject.new()

-- generate: pack the objects (the default).
local pdf1 = chilkat.Pdf.new()
local success = pdf1:load_file("qa_data/pdf/grand_canyon_hiking_guide.pdf")
if not success then
    print(pdf1.last_error_text)
    return
end

json:update_string("objectStreams", "generate")
success = pdf1:optimize("optimize", json, jsonOut)
if not success then
    print(pdf1.last_error_text)
    return
end

success = pdf1:write_file("qa_output/grand_canyon_objstm_generate.pdf")
if not success then
    print(pdf1.last_error_text)
    return
end

print("generate: " .. fac:file_size("qa_output/grand_canyon_objstm_generate.pdf") .. " bytes, objects packed: " .. jsonOut:int_of("actions.objectStreams.count"))

-- disable: every object on its own, with a classic cross-reference table.
local pdf2 = chilkat.Pdf.new()
success = pdf2:load_file("qa_data/pdf/grand_canyon_hiking_guide.pdf")
if not success then
    print(pdf2.last_error_text)
    return
end

json:update_string("objectStreams", "disable")
success = pdf2:optimize("optimize", json, jsonOut)
if not success then
    print(pdf2.last_error_text)
    return
end

success = pdf2:write_file("qa_output/grand_canyon_objstm_disable.pdf")
if not success then
    print(pdf2.last_error_text)
    return
end

print("disable:  " .. fac:file_size("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.
local pdf3 = chilkat.Pdf.new()
success = pdf3:load_file("qa_data/pdf/grand_canyon_hiking_guide.pdf")
if not success then
    print(pdf3.last_error_text)
    return
end

json:update_string("objectStreams", "preserve")
success = pdf3:optimize("optimize", json, jsonOut)
if not success then
    print(pdf3.last_error_text)
    return
end

success = pdf3:write_file("qa_output/grand_canyon_objstm_preserve.pdf")
if not success then
    print(pdf3.last_error_text)
    return
end

print("preserve: " .. fac:file_size("qa_output/grand_canyon_objstm_preserve.pdf") .. " bytes")