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

Lua
local chilkat = require("chilkat")

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

local pdf = chilkat.Pdf.new()
local success = pdf:load_file("qa_data/pdf/grand_canyon_hiking_guide.pdf")
if not success then
    print(pdf.last_error_text)
    return
end

-- How much of this file is superseded revisions?
local json = chilkat.JsonObject.new()
local jsonOut = chilkat.JsonObject.new()
success = pdf:optimize("audit", json, jsonOut)
if not success then
    print(pdf.last_error_text)
    return
end

print("fileSize:     " .. jsonOut:int_of("fileSize"))
print("oldRevisions: " .. jsonOut:int_of("usage.oldRevisions.bytes"))

-- Drop them, and nothing else, so that the saving is attributable.
json:update_bool("compressUncompressed", false)
json:update_bool("recompressFlate", false)
json:update_bool("replaceLegacyFilters", false)
json:update_bool("imagePredictors", false)
json:update_bool("dedupeStreams", false)
json:update_bool("dedupeObjects", false)
json:update_bool("removeUnreferenced", false)
json:update_bool("removeUnusedResources", false)
json:update_bool("removeUnusedLayers", false)
json:update_bool("coalesceContent", false)
json:update_bool("tidyContent", false)
json:update_bool("subsetFonts", false)
json:update_bool("dropOldRevisions", true)

success = pdf:optimize("optimize", json, jsonOut)
if not success then
    print(pdf.last_error_text)
    return
end

success = pdf:write_file("qa_output/grand_canyon_one_revision.pdf")
if not success then
    print(pdf.last_error_text)
    return
end

-- 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.
local fac = chilkat.FileAccess.new()
print("bytesSaved (reported): " .. jsonOut:int_of("bytesSaved"))
print("written file:          " .. fac:file_size("qa_output/grand_canyon_one_revision.pdf"))