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

Delphi DLL
var
success: Boolean;
pdf: HCkPdf;
json: HCkJsonObject;
jsonOut: HCkJsonObject;
fac: HCkFileAccess;

begin
success := 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

pdf := CkPdf_Create();
success := CkPdf_LoadFile(pdf,'qa_data/pdf/grand_canyon_hiking_guide.pdf');
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
    Exit;
  end;

//  How much of this file is superseded revisions?
json := CkJsonObject_Create();
jsonOut := CkJsonObject_Create();
success := CkPdf_Optimize(pdf,'audit',json,jsonOut);
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
    Exit;
  end;

Memo1.Lines.Add('fileSize:     ' + IntToStr(CkJsonObject_IntOf(jsonOut,'fileSize')));
Memo1.Lines.Add('oldRevisions: ' + IntToStr(CkJsonObject_IntOf(jsonOut,'usage.oldRevisions.bytes')));

//  Drop them, and nothing else, so that the saving is attributable.
CkJsonObject_UpdateBool(json,'compressUncompressed',False);
CkJsonObject_UpdateBool(json,'recompressFlate',False);
CkJsonObject_UpdateBool(json,'replaceLegacyFilters',False);
CkJsonObject_UpdateBool(json,'imagePredictors',False);
CkJsonObject_UpdateBool(json,'dedupeStreams',False);
CkJsonObject_UpdateBool(json,'dedupeObjects',False);
CkJsonObject_UpdateBool(json,'removeUnreferenced',False);
CkJsonObject_UpdateBool(json,'removeUnusedResources',False);
CkJsonObject_UpdateBool(json,'removeUnusedLayers',False);
CkJsonObject_UpdateBool(json,'coalesceContent',False);
CkJsonObject_UpdateBool(json,'tidyContent',False);
CkJsonObject_UpdateBool(json,'subsetFonts',False);
CkJsonObject_UpdateBool(json,'dropOldRevisions',True);

success := CkPdf_Optimize(pdf,'optimize',json,jsonOut);
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
    Exit;
  end;

success := CkPdf_WriteFile(pdf,'qa_output/grand_canyon_one_revision.pdf');
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
    Exit;
  end;

//  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.
fac := CkFileAccess_Create();
Memo1.Lines.Add('bytesSaved (reported): ' + IntToStr(CkJsonObject_IntOf(jsonOut,'bytesSaved')));
Memo1.Lines.Add('written file:          ' + IntToStr(CkFileAccess_FileSize(fac,'qa_output/grand_canyon_one_revision.pdf')));

CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
CkFileAccess_Dispose(fac);