Swift Requires Chilkat v11.7.0+
Swift
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 Swift Downloads
func chilkatTest() {
var success: Bool = false
// 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
let fac = CkoFileAccess()!
let json = CkoJsonObject()!
let jsonOut = CkoJsonObject()!
// generate: pack the objects (the default).
let pdf1 = CkoPdf()!
success = pdf1.loadFile(filePath: "qa_data/pdf/grand_canyon_hiking_guide.pdf")
if success == false {
print("\(pdf1.lastErrorText!)")
return
}
json.updateString(jsonPath: "objectStreams", value: "generate")
success = pdf1.optimize(op: "optimize", jsonIn: json, jsonOut: jsonOut)
if success == false {
print("\(pdf1.lastErrorText!)")
return
}
success = pdf1.writeFile(path: "qa_output/grand_canyon_objstm_generate.pdf")
if success == false {
print("\(pdf1.lastErrorText!)")
return
}
print("generate: \(fac.fileSize(path: "qa_output/grand_canyon_objstm_generate.pdf").intValue) bytes, objects packed: \(jsonOut.int(of: "actions.objectStreams.count").intValue)")
// disable: every object on its own, with a classic cross-reference table.
let pdf2 = CkoPdf()!
success = pdf2.loadFile(filePath: "qa_data/pdf/grand_canyon_hiking_guide.pdf")
if success == false {
print("\(pdf2.lastErrorText!)")
return
}
json.updateString(jsonPath: "objectStreams", value: "disable")
success = pdf2.optimize(op: "optimize", jsonIn: json, jsonOut: jsonOut)
if success == false {
print("\(pdf2.lastErrorText!)")
return
}
success = pdf2.writeFile(path: "qa_output/grand_canyon_objstm_disable.pdf")
if success == false {
print("\(pdf2.lastErrorText!)")
return
}
print("disable: \(fac.fileSize(path: "qa_output/grand_canyon_objstm_disable.pdf").intValue) bytes")
// preserve: leave the file's own structure alone. Use it when the output must stay readable by
// whatever reads the input today.
let pdf3 = CkoPdf()!
success = pdf3.loadFile(filePath: "qa_data/pdf/grand_canyon_hiking_guide.pdf")
if success == false {
print("\(pdf3.lastErrorText!)")
return
}
json.updateString(jsonPath: "objectStreams", value: "preserve")
success = pdf3.optimize(op: "optimize", jsonIn: json, jsonOut: jsonOut)
if success == false {
print("\(pdf3.lastErrorText!)")
return
}
success = pdf3.writeFile(path: "qa_output/grand_canyon_objstm_preserve.pdf")
if success == false {
print("\(pdf3.lastErrorText!)")
return
}
print("preserve: \(fac.fileSize(path: "qa_output/grand_canyon_objstm_preserve.pdf").intValue) bytes")
}