Sample code for 30+ languages & platforms
Classic ASP 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 Classic ASP Downloads

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
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 = Server.CreateObject("Chilkat.Pdf")
success = pdf.LoadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

'  How much of this file is superseded revisions?
set json = Server.CreateObject("Chilkat.JsonObject")
set jsonOut = Server.CreateObject("Chilkat.JsonObject")
success = pdf.Optimize("audit",json,jsonOut)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "fileSize:     " & jsonOut.IntOf("fileSize")) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "oldRevisions: " & jsonOut.IntOf("usage.oldRevisions.bytes")) & "</pre>"

'  Drop them, and nothing else, so that the saving is attributable.
success = json.UpdateBool("compressUncompressed",0)
success = json.UpdateBool("recompressFlate",0)
success = json.UpdateBool("replaceLegacyFilters",0)
success = json.UpdateBool("imagePredictors",0)
success = json.UpdateBool("dedupeStreams",0)
success = json.UpdateBool("dedupeObjects",0)
success = json.UpdateBool("removeUnreferenced",0)
success = json.UpdateBool("removeUnusedResources",0)
success = json.UpdateBool("removeUnusedLayers",0)
success = json.UpdateBool("coalesceContent",0)
success = json.UpdateBool("tidyContent",0)
success = json.UpdateBool("subsetFonts",0)
success = json.UpdateBool("dropOldRevisions",1)

success = pdf.Optimize("optimize",json,jsonOut)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

success = pdf.WriteFile("qa_output/grand_canyon_one_revision.pdf")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
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.
set fac = Server.CreateObject("Chilkat.FileAccess")
Response.Write "<pre>" & Server.HTMLEncode( "bytesSaved (reported): " & jsonOut.IntOf("bytesSaved")) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "written file:          " & fac.FileSize("qa_output/grand_canyon_one_revision.pdf")) & "</pre>"

%>
</body>
</html>