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

Rust

// 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 = chilkat::Pdf::new();
if pdf.load_file("qa_data/pdf/grand_canyon_hiking_guide.pdf").is_err() {
    println!("{}", pdf.last_error_text());
    return;
}

// How much of this file is superseded revisions?
let json = chilkat::JsonObject::new();
let json_out = chilkat::JsonObject::new();
if pdf.optimize("audit", &json, &json_out).is_err() {
    println!("{}", pdf.last_error_text());
    return;
}

println!("fileSize:     {}", json_out.int_of("fileSize"));
println!("oldRevisions: {}", json_out.int_of("usage.oldRevisions.bytes"));

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

if pdf.optimize("optimize", &json, &json_out).is_err() {
    println!("{}", pdf.last_error_text());
    return;
}

if pdf.write_file("qa_output/grand_canyon_one_revision.pdf").is_err() {
    println!("{}", pdf.last_error_text());
    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 = chilkat::FileAccess::new();
println!("bytesSaved (reported): {}", json_out.int_of("bytesSaved"));
println!("written file:          {}", fac.file_size("qa_output/grand_canyon_one_revision.pdf"));