Objective-C Requires Chilkat v11.7.0+
Objective-C
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 Objective-C Downloads
#import <CkoPdf.h>
#import <CkoJsonObject.h>
#import <CkoFileAccess.h>
BOOL success = NO;
// 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
CkoPdf *pdf = [[CkoPdf alloc] init];
success = [pdf LoadFile: @"qa_data/pdf/grand_canyon_hiking_guide.pdf"];
if (success == NO) {
NSLog(@"%@",pdf.LastErrorText);
return;
}
// How much of this file is superseded revisions?
CkoJsonObject *json = [[CkoJsonObject alloc] init];
CkoJsonObject *jsonOut = [[CkoJsonObject alloc] init];
success = [pdf Optimize: @"audit" jsonIn: json jsonOut: jsonOut];
if (success == NO) {
NSLog(@"%@",pdf.LastErrorText);
return;
}
NSLog(@"%@%d",@"fileSize: ",[[jsonOut IntOf: @"fileSize"] intValue]);
NSLog(@"%@%d",@"oldRevisions: ",[[jsonOut IntOf: @"usage.oldRevisions.bytes"] intValue]);
// Drop them, and nothing else, so that the saving is attributable.
[json UpdateBool: @"compressUncompressed" value: NO];
[json UpdateBool: @"recompressFlate" value: NO];
[json UpdateBool: @"replaceLegacyFilters" value: NO];
[json UpdateBool: @"imagePredictors" value: NO];
[json UpdateBool: @"dedupeStreams" value: NO];
[json UpdateBool: @"dedupeObjects" value: NO];
[json UpdateBool: @"removeUnreferenced" value: NO];
[json UpdateBool: @"removeUnusedResources" value: NO];
[json UpdateBool: @"removeUnusedLayers" value: NO];
[json UpdateBool: @"coalesceContent" value: NO];
[json UpdateBool: @"tidyContent" value: NO];
[json UpdateBool: @"subsetFonts" value: NO];
[json UpdateBool: @"dropOldRevisions" value: YES];
success = [pdf Optimize: @"optimize" jsonIn: json jsonOut: jsonOut];
if (success == NO) {
NSLog(@"%@",pdf.LastErrorText);
return;
}
success = [pdf WriteFile: @"qa_output/grand_canyon_one_revision.pdf"];
if (success == NO) {
NSLog(@"%@",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.
CkoFileAccess *fac = [[CkoFileAccess alloc] init];
NSLog(@"%@%d",@"bytesSaved (reported): ",[[jsonOut IntOf: @"bytesSaved"] intValue]);
NSLog(@"%@%d",@"written file: ",[[fac FileSize: @"qa_output/grand_canyon_one_revision.pdf"] intValue]);