Lazarus Pascal Requires Chilkat v11.7.0+
Lazarus Pascal
Shrink a PDF Losslessly
See more PDF Optimization Examples
Demonstrates the "optimize" operation with the default options, which is the lossless preset: nothing is resampled, recoloured or thrown away, so every page looks exactly as it did and all the text is still there. The changes are kept in memory until WriteFile (or WriteBd) is called. Note that the real saving is "fileSize" minus the size of the file that was written, not "bytesSaved": the rewrite also packs objects into object streams and writes a compact cross-reference stream, which belongs to no option.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;
sizeBefore: Integer;
sizeAfter: Integer;
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;
// An empty JSON means the default options: every lossless optimization, object streams generated,
// old revisions dropped, deflate level 9.
json := TJsonObject.Create;
jsonOut := TJsonObject.Create;
success := pdf.Optimize('optimize',json,jsonOut);
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
// Nothing has been written yet. WriteFile (or WriteBd) produces the optimized PDF.
success := pdf.WriteFile('qa_output/grand_canyon_optimized.pdf');
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
fac := TFileAccess.Create;
sizeBefore := jsonOut.IntOf('fileSize');
sizeAfter := fac.FileSize('qa_output/grand_canyon_optimized.pdf');
WriteLn('before: ' + sizeBefore);
WriteLn('after: ' + sizeAfter);
WriteLn('saved: ' + sizeBefore - sizeAfter);
// What the options themselves account for. This is smaller than the real saving above, because the
// rewrite's own gains (object streams, the cross-reference stream, the old sections left behind)
// belong to no single option.
WriteLn('bytesSaved (options only): ' + jsonOut.IntOf('bytesSaved'));
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.