Lazarus Pascal Requires Chilkat v11.7.0+
Lazarus Pascal
Deflate Compression Level When Optimizing a PDF
See more PDF Optimization Examples
Demonstrates compressionLevel, which is the deflate level (1 to 9, 9 by default) used for every stream the optimizer re-encodes. Level 9 is the smallest and the slowest; level 1 is the quickest. The difference is usually a percent or two, so level 9 is the right default for a file that will be downloaded many times, and a lower level is worth considering only when a server optimizes PDFs on the fly. A value outside 1 to 9 is an error.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;
fac: TFileAccess;
json: TJsonObject;
jsonOut: TJsonObject;
pdf1: TPdf;
pdf9: TPdf;
pdfBad: TPdf;
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
fac := TFileAccess.Create;
json := TJsonObject.Create;
jsonOut := TJsonObject.Create;
pdf1 := TPdf.Create;
success := pdf1.LoadFile('qa_data/pdf/watersense_accomplishments.pdf');
if (success = False) then
begin
WriteLn(pdf1.LastErrorText);
Exit;
end;
json.UpdateInt('compressionLevel',1);
success := pdf1.Optimize('optimize',json,jsonOut);
if (success = False) then
begin
WriteLn(pdf1.LastErrorText);
Exit;
end;
success := pdf1.WriteFile('qa_output/watersense_level1.pdf');
if (success = False) then
begin
WriteLn(pdf1.LastErrorText);
Exit;
end;
WriteLn('level 1: ' + fac.FileSize('qa_output/watersense_level1.pdf'));
pdf9 := TPdf.Create;
success := pdf9.LoadFile('qa_data/pdf/watersense_accomplishments.pdf');
if (success = False) then
begin
WriteLn(pdf9.LastErrorText);
Exit;
end;
json.UpdateInt('compressionLevel',9);
success := pdf9.Optimize('optimize',json,jsonOut);
if (success = False) then
begin
WriteLn(pdf9.LastErrorText);
Exit;
end;
success := pdf9.WriteFile('qa_output/watersense_level9.pdf');
if (success = False) then
begin
WriteLn(pdf9.LastErrorText);
Exit;
end;
WriteLn('level 9: ' + fac.FileSize('qa_output/watersense_level9.pdf'));
// A level outside 1..9 fails, and LastErrorText says why.
pdfBad := TPdf.Create;
success := pdfBad.LoadFile('qa_data/pdf/watersense_accomplishments.pdf');
if (success = False) then
begin
WriteLn(pdfBad.LastErrorText);
Exit;
end;
json.UpdateInt('compressionLevel',12);
success := pdfBad.Optimize('optimize',json,jsonOut);
if (success = False) then
begin
WriteLn('compressionLevel 12 was refused, as it should be.');
end;
fac.Free;
json.Free;
jsonOut.Free;
pdf1.Free;
pdf9.Free;
pdfBad.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.