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

Visual FoxPro
LOCAL lnSuccess
LOCAL loPdf
LOCAL loJson
LOCAL loJsonOut
LOCAL loFac

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

loPdf = CreateObject('Chilkat.Pdf')
lnSuccess = loPdf.LoadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf")
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    CANCEL
ENDIF

*  How much of this file is superseded revisions?
loJson = CreateObject('Chilkat.JsonObject')
loJsonOut = CreateObject('Chilkat.JsonObject')
lnSuccess = loPdf.Optimize("audit",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

? "fileSize:     " + STR(loJsonOut.IntOf("fileSize"))
? "oldRevisions: " + STR(loJsonOut.IntOf("usage.oldRevisions.bytes"))

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

lnSuccess = loPdf.Optimize("optimize",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

lnSuccess = loPdf.WriteFile("qa_output/grand_canyon_one_revision.pdf")
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
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.
loFac = CreateObject('Chilkat.FileAccess')
? "bytesSaved (reported): " + STR(loJsonOut.IntOf("bytesSaved"))
? "written file:          " + STR(loFac.FileSize("qa_output/grand_canyon_one_revision.pdf"))

RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
RELEASE loFac