Sample code for 30+ languages & platforms
Tcl Requires Chilkat v11.7.0+

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

Tcl

load ./chilkat.dll

set 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

set pdf [new_CkPdf]

set success [CkPdf_LoadFile $pdf "qa_data/pdf/grand_canyon_hiking_guide.pdf"]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkPdf $pdf
    exit
}

#  How much of this file is superseded revisions?
set json [new_CkJsonObject]

set jsonOut [new_CkJsonObject]

set success [CkPdf_Optimize $pdf "audit" $json $jsonOut]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    exit
}

puts "fileSize:     [CkJsonObject_IntOf $jsonOut fileSize]"
puts "oldRevisions: [CkJsonObject_IntOf $jsonOut usage.oldRevisions.bytes]"

#  Drop them, and nothing else, so that the saving is attributable.
CkJsonObject_UpdateBool $json "compressUncompressed" 0
CkJsonObject_UpdateBool $json "recompressFlate" 0
CkJsonObject_UpdateBool $json "replaceLegacyFilters" 0
CkJsonObject_UpdateBool $json "imagePredictors" 0
CkJsonObject_UpdateBool $json "dedupeStreams" 0
CkJsonObject_UpdateBool $json "dedupeObjects" 0
CkJsonObject_UpdateBool $json "removeUnreferenced" 0
CkJsonObject_UpdateBool $json "removeUnusedResources" 0
CkJsonObject_UpdateBool $json "removeUnusedLayers" 0
CkJsonObject_UpdateBool $json "coalesceContent" 0
CkJsonObject_UpdateBool $json "tidyContent" 0
CkJsonObject_UpdateBool $json "subsetFonts" 0
CkJsonObject_UpdateBool $json "dropOldRevisions" 1

set success [CkPdf_Optimize $pdf "optimize" $json $jsonOut]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    exit
}

set success [CkPdf_WriteFile $pdf "qa_output/grand_canyon_one_revision.pdf"]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    exit
}

#  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.
set fac [new_CkFileAccess]

puts "bytesSaved (reported): [CkJsonObject_IntOf $jsonOut bytesSaved]"
puts "written file:          [CkFileAccess_FileSize $fac qa_output/grand_canyon_one_revision.pdf]"

delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
delete_CkFileAccess $fac