Lazarus Pascal Requires Chilkat v11.7.0+
Lazarus Pascal
Remove PDF Objects Nothing Refers To
See more PDF Optimization Examples
Demonstrates removeUnreferenced. When a page, an image or a whole section is replaced in an editor, the objects it used often stay in the file with nothing pointing at them any more. They are not old revisions -- they are in the current revision, simply unreachable -- so only a reachability walk from the document catalog finds them. In a publication assembled over several editing passes this is frequently the largest single saving.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.Pdf,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
pdf: TPdf;
json: TJsonObject;
jsonOut: TJsonObject;
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 watersense_accomplishments.pdf
pdf := TPdf.Create;
success := pdf.LoadFile('qa_data/pdf/watersense_accomplishments.pdf');
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
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('removeUnreferenced would save: ' + jsonOut.IntOf('estimatedSavings.removeUnreferenced'));
// Optimize with the defaults and see how many objects were actually dropped.
success := pdf.Optimize('optimize',json,jsonOut);
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
success := pdf.WriteFile('qa_output/watersense_optimized.pdf');
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
WriteLn('objects removed: ' + jsonOut.IntOf('actions.removeUnreferenced.count'));
WriteLn('bytes saved: ' + jsonOut.IntOf('actions.removeUnreferenced.bytesSaved'));
// Nothing a page needs is ever removed: the walk starts at the document catalog and follows every
// reference, including the ones in annotations, the structure tree, named destinations and the
// optional-content configuration.
WriteLn('pages after: ' + pdf.NumPages);
pdf.Free;
json.Free;
jsonOut.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.