Xbase++ Requires Chilkat v11.7.0+
Xbase++
Remove PDF Objects Nothing Refers To
See more PDF Optimization Examples
Demonstrates removeUnreferenced. When a page, an image or a whole section is replaced in an editor, the objects it used often stay in the file with nothing pointing at them any more. They are not old revisions -- they are in the current revision, simply unreachable -- so only a reachability walk from the document catalog finds them. In a publication assembled over several editing passes this is frequently the largest single saving.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oPdf
LOCAL oJson
LOCAL oJsonOut
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 watersense_accomplishments.pdf
oPdf := CreateObject("Chilkat.Pdf")
nSuccess := oPdf:LoadFile("qa_data/pdf/watersense_accomplishments.pdf")
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
RETURN
ENDIF
oJson := CreateObject("Chilkat.JsonObject")
oJsonOut := CreateObject("Chilkat.JsonObject")
nSuccess := oPdf:Optimize("audit", oJson, oJsonOut)
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
RETURN
ENDIF
? "fileSize: " + Str(oJsonOut:IntOf("fileSize"))
? "removeUnreferenced would save: " + Str(oJsonOut:IntOf("estimatedSavings.removeUnreferenced"))
// Optimize with the defaults and see how many objects were actually dropped.
nSuccess := oPdf:Optimize("optimize", oJson, oJsonOut)
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
RETURN
ENDIF
nSuccess := oPdf:WriteFile("qa_output/watersense_optimized.pdf")
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
RETURN
ENDIF
? "objects removed: " + Str(oJsonOut:IntOf("actions.removeUnreferenced.count"))
? "bytes saved: " + Str(oJsonOut:IntOf("actions.removeUnreferenced.bytesSaved"))
// Nothing a page needs is ever removed: the walk starts at the document catalog and follows every
// reference, including the ones in annotations, the structure tree, named destinations and the
// optional-content configuration.
? "pages after: " + Str(oPdf:NumPages)
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()