Lazarus Pascal Requires Chilkat v11.7.0+
Lazarus Pascal
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 Lazarus Pascal Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.FileAccess,
Chilkat.Pdf,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
pdf: TPdf;
json: TJsonObject;
jsonOut: TJsonObject;
fac: TFileAccess;
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 := TPdf.Create;
success := pdf.LoadFile('qa_data/pdf/grand_canyon_hiking_guide.pdf');
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
// How much of this file is superseded revisions?
json := TJsonObject.Create;
jsonOut := TJsonObject.Create;
success := pdf.Optimize('audit',json,jsonOut);
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
WriteLn('fileSize: ' + jsonOut.IntOf('fileSize'));
WriteLn('oldRevisions: ' + jsonOut.IntOf('usage.oldRevisions.bytes'));
// Drop them, and nothing else, so that the saving is attributable.
json.UpdateBool('compressUncompressed',False);
json.UpdateBool('recompressFlate',False);
json.UpdateBool('replaceLegacyFilters',False);
json.UpdateBool('imagePredictors',False);
json.UpdateBool('dedupeStreams',False);
json.UpdateBool('dedupeObjects',False);
json.UpdateBool('removeUnreferenced',False);
json.UpdateBool('removeUnusedResources',False);
json.UpdateBool('removeUnusedLayers',False);
json.UpdateBool('coalesceContent',False);
json.UpdateBool('tidyContent',False);
json.UpdateBool('subsetFonts',False);
json.UpdateBool('dropOldRevisions',True);
success := pdf.Optimize('optimize',json,jsonOut);
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
success := pdf.WriteFile('qa_output/grand_canyon_one_revision.pdf');
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
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 := TFileAccess.Create;
WriteLn('bytesSaved (reported): ' + jsonOut.IntOf('bytesSaved'));
WriteLn('written file: ' + fac.FileSize('qa_output/grand_canyon_one_revision.pdf'));
pdf.Free;
json.Free;
jsonOut.Free;
fac.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.