Perl Requires Chilkat v11.7.0+
Chilkat2 Perl
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 Perl Downloads
use strict;
use warnings;
use Chilkat2;
# 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
my $pdf = Chilkat2::Pdf->new;
my $success = $pdf->LoadFile('qa_data/pdf/grand_canyon_hiking_guide.pdf');
if (!$success) {
print $pdf->LastErrorText, "\n";
exit;
}
# How much of this file is superseded revisions?
my $json = Chilkat2::JsonObject->new;
my $jsonOut = Chilkat2::JsonObject->new;
$success = $pdf->Optimize('audit', $json, $jsonOut);
if (!$success) {
print $pdf->LastErrorText, "\n";
exit;
}
print 'fileSize: ', $jsonOut->IntOf('fileSize'), "\n";
print 'oldRevisions: ', $jsonOut->IntOf('usage.oldRevisions.bytes'), "\n";
# Drop them, and nothing else, so that the saving is attributable.
$json->UpdateBool('compressUncompressed', 0);
$json->UpdateBool('recompressFlate', 0);
$json->UpdateBool('replaceLegacyFilters', 0);
$json->UpdateBool('imagePredictors', 0);
$json->UpdateBool('dedupeStreams', 0);
$json->UpdateBool('dedupeObjects', 0);
$json->UpdateBool('removeUnreferenced', 0);
$json->UpdateBool('removeUnusedResources', 0);
$json->UpdateBool('removeUnusedLayers', 0);
$json->UpdateBool('coalesceContent', 0);
$json->UpdateBool('tidyContent', 0);
$json->UpdateBool('subsetFonts', 0);
$json->UpdateBool('dropOldRevisions', 1);
$success = $pdf->Optimize('optimize', $json, $jsonOut);
if (!$success) {
print $pdf->LastErrorText, "\n";
exit;
}
$success = $pdf->WriteFile('qa_output/grand_canyon_one_revision.pdf');
if (!$success) {
print $pdf->LastErrorText, "\n";
exit;
}
# 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.
my $fac = Chilkat2::FileAccess->new;
print 'bytesSaved (reported): ', $jsonOut->IntOf('bytesSaved'), "\n";
print 'written file: ', $fac->FileSize('qa_output/grand_canyon_one_revision.pdf'), "\n";