PowerBuilder Requires Chilkat v11.7.0+
PowerBuilder
Remove a PDF's Old Revisions
See more PDF Optimization Examples
Demonstrates dropOldRevisions. A PDF that has been edited and saved several times carries every earlier revision: the superseded objects are still in the file, reachable through the older cross-reference sections. The audit reports them as "oldRevisions", and dropping them is often the single largest saving in a real document. Dropping them requires a full rewrite, which is why it invalidates existing digital signatures.Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut
oleobject loo_Fac
li_Success = 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
loo_Pdf = create oleobject
li_rc = loo_Pdf.ConnectToNewObject("Chilkat.Pdf")
if li_rc < 0 then
destroy loo_Pdf
MessageBox("Error","Connecting to COM object failed")
return
end if
li_Success = loo_Pdf.LoadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf")
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Pdf
return
end if
// How much of this file is superseded revisions?
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
loo_JsonOut = create oleobject
li_rc = loo_JsonOut.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Pdf.Optimize("audit",loo_Json,loo_JsonOut)
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
return
end if
Write-Debug "fileSize: " + string(loo_JsonOut.IntOf("fileSize"))
Write-Debug "oldRevisions: " + string(loo_JsonOut.IntOf("usage.oldRevisions.bytes"))
// Drop them, and nothing else, so that the saving is attributable.
loo_Json.UpdateBool("compressUncompressed",0)
loo_Json.UpdateBool("recompressFlate",0)
loo_Json.UpdateBool("replaceLegacyFilters",0)
loo_Json.UpdateBool("imagePredictors",0)
loo_Json.UpdateBool("dedupeStreams",0)
loo_Json.UpdateBool("dedupeObjects",0)
loo_Json.UpdateBool("removeUnreferenced",0)
loo_Json.UpdateBool("removeUnusedResources",0)
loo_Json.UpdateBool("removeUnusedLayers",0)
loo_Json.UpdateBool("coalesceContent",0)
loo_Json.UpdateBool("tidyContent",0)
loo_Json.UpdateBool("subsetFonts",0)
loo_Json.UpdateBool("dropOldRevisions",1)
li_Success = loo_Pdf.Optimize("optimize",loo_Json,loo_JsonOut)
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
return
end if
li_Success = loo_Pdf.WriteFile("qa_output/grand_canyon_one_revision.pdf")
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
return
end if
// The saving is an estimate: what the old revisions occupied is known exactly, but the writer
// decides the final layout. Measure the written file for the real number.
loo_Fac = create oleobject
li_rc = loo_Fac.ConnectToNewObject("Chilkat.FileAccess")
Write-Debug "bytesSaved (reported): " + string(loo_JsonOut.IntOf("bytesSaved"))
Write-Debug "written file: " + string(loo_Fac.FileSize("qa_output/grand_canyon_one_revision.pdf"))
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_Fac