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

Swift

func chilkatTest() {
    var success: Bool = 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

    let pdf = CkoPdf()!
    success = pdf.loadFile(filePath: "qa_data/pdf/grand_canyon_hiking_guide.pdf")
    if success == false {
        print("\(pdf.lastErrorText!)")
        return
    }

    //  How much of this file is superseded revisions?
    let json = CkoJsonObject()!
    let jsonOut = CkoJsonObject()!
    success = pdf.optimize(op: "audit", jsonIn: json, jsonOut: jsonOut)
    if success == false {
        print("\(pdf.lastErrorText!)")
        return
    }

    print("fileSize:     \(jsonOut.int(of: "fileSize").intValue)")
    print("oldRevisions: \(jsonOut.int(of: "usage.oldRevisions.bytes").intValue)")

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

    success = pdf.optimize(op: "optimize", jsonIn: json, jsonOut: jsonOut)
    if success == false {
        print("\(pdf.lastErrorText!)")
        return
    }

    success = pdf.writeFile(path: "qa_output/grand_canyon_one_revision.pdf")
    if success == false {
        print("\(pdf.lastErrorText!)")
        return
    }

    //  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.
    let fac = CkoFileAccess()!
    print("bytesSaved (reported): \(jsonOut.int(of: "bytesSaved").intValue)")
    print("written file:          \(fac.fileSize(path: "qa_output/grand_canyon_one_revision.pdf").intValue)")

}