PureBasic Requires Chilkat v11.7.0+
PureBasic
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 PureBasic Downloads
IncludeFile "CkPdf.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkFileAccess.pb"
Procedure ChilkatExample()
success.i = 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
fac.i = CkFileAccess::ckCreate()
If fac.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
jsonOut.i = CkJsonObject::ckCreate()
If jsonOut.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; generate: pack the objects (the default).
pdf1.i = CkPdf::ckCreate()
If pdf1.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckLoadFile(pdf1,"qa_data/pdf/grand_canyon_hiking_guide.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf1)
CkFileAccess::ckDispose(fac)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(pdf1)
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(json,"objectStreams","generate")
success = CkPdf::ckOptimize(pdf1,"optimize",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf1)
CkFileAccess::ckDispose(fac)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(pdf1)
ProcedureReturn
EndIf
success = CkPdf::ckWriteFile(pdf1,"qa_output/grand_canyon_objstm_generate.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf1)
CkFileAccess::ckDispose(fac)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(pdf1)
ProcedureReturn
EndIf
Debug "generate: " + Str(CkFileAccess::ckFileSize(fac,"qa_output/grand_canyon_objstm_generate.pdf")) + " bytes, objects packed: " + Str(CkJsonObject::ckIntOf(jsonOut,"actions.objectStreams.count"))
; disable: every object on its own, with a classic cross-reference table.
pdf2.i = CkPdf::ckCreate()
If pdf2.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckLoadFile(pdf2,"qa_data/pdf/grand_canyon_hiking_guide.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf2)
CkFileAccess::ckDispose(fac)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(pdf1)
CkPdf::ckDispose(pdf2)
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(json,"objectStreams","disable")
success = CkPdf::ckOptimize(pdf2,"optimize",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf2)
CkFileAccess::ckDispose(fac)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(pdf1)
CkPdf::ckDispose(pdf2)
ProcedureReturn
EndIf
success = CkPdf::ckWriteFile(pdf2,"qa_output/grand_canyon_objstm_disable.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf2)
CkFileAccess::ckDispose(fac)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(pdf1)
CkPdf::ckDispose(pdf2)
ProcedureReturn
EndIf
Debug "disable: " + Str(CkFileAccess::ckFileSize(fac,"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.
pdf3.i = CkPdf::ckCreate()
If pdf3.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckLoadFile(pdf3,"qa_data/pdf/grand_canyon_hiking_guide.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf3)
CkFileAccess::ckDispose(fac)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(pdf1)
CkPdf::ckDispose(pdf2)
CkPdf::ckDispose(pdf3)
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(json,"objectStreams","preserve")
success = CkPdf::ckOptimize(pdf3,"optimize",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf3)
CkFileAccess::ckDispose(fac)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(pdf1)
CkPdf::ckDispose(pdf2)
CkPdf::ckDispose(pdf3)
ProcedureReturn
EndIf
success = CkPdf::ckWriteFile(pdf3,"qa_output/grand_canyon_objstm_preserve.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf3)
CkFileAccess::ckDispose(fac)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(pdf1)
CkPdf::ckDispose(pdf2)
CkPdf::ckDispose(pdf3)
ProcedureReturn
EndIf
Debug "preserve: " + Str(CkFileAccess::ckFileSize(fac,"qa_output/grand_canyon_objstm_preserve.pdf")) + " bytes"
CkFileAccess::ckDispose(fac)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(pdf1)
CkPdf::ckDispose(pdf2)
CkPdf::ckDispose(pdf3)
ProcedureReturn
EndProcedure