AutoIt Requires Chilkat v11.7.0+
AutoIt
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 AutoIt Downloads
Local $bSuccess = 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
$oPdf = ObjCreate("Chilkat.Pdf")
$bSuccess = $oPdf.LoadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf")
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
; How much of this file is superseded revisions?
$oJson = ObjCreate("Chilkat.JsonObject")
$oJsonOut = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oPdf.Optimize("audit",$oJson,$oJsonOut)
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("fileSize: " & $oJsonOut.IntOf("fileSize") & @CRLF)
ConsoleWrite("oldRevisions: " & $oJsonOut.IntOf("usage.oldRevisions.bytes") & @CRLF)
; Drop them, and nothing else, so that the saving is attributable.
$oJson.UpdateBool("compressUncompressed",False)
$oJson.UpdateBool("recompressFlate",False)
$oJson.UpdateBool("replaceLegacyFilters",False)
$oJson.UpdateBool("imagePredictors",False)
$oJson.UpdateBool("dedupeStreams",False)
$oJson.UpdateBool("dedupeObjects",False)
$oJson.UpdateBool("removeUnreferenced",False)
$oJson.UpdateBool("removeUnusedResources",False)
$oJson.UpdateBool("removeUnusedLayers",False)
$oJson.UpdateBool("coalesceContent",False)
$oJson.UpdateBool("tidyContent",False)
$oJson.UpdateBool("subsetFonts",False)
$oJson.UpdateBool("dropOldRevisions",True)
$bSuccess = $oPdf.Optimize("optimize",$oJson,$oJsonOut)
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
$bSuccess = $oPdf.WriteFile("qa_output/grand_canyon_one_revision.pdf")
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
; 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.
$oFac = ObjCreate("Chilkat.FileAccess")
ConsoleWrite("bytesSaved (reported): " & $oJsonOut.IntOf("bytesSaved") & @CRLF)
ConsoleWrite("written file: " & $oFac.FileSize("qa_output/grand_canyon_one_revision.pdf") & @CRLF)