Sample code for 30+ languages & platforms
Delphi DLL Requires Chilkat v11.7.0+

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 Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
fac: HCkFileAccess;
json: HCkJsonObject;
jsonOut: HCkJsonObject;
pdf1: HCkPdf;
pdf9: HCkPdf;
pdfBad: HCkPdf;

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 := CkFileAccess_Create();
json := CkJsonObject_Create();
jsonOut := CkJsonObject_Create();

pdf1 := CkPdf_Create();
success := CkPdf_LoadFile(pdf1,'qa_data/pdf/watersense_accomplishments.pdf');
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf1));
    Exit;
  end;
CkJsonObject_UpdateInt(json,'compressionLevel',1);
success := CkPdf_Optimize(pdf1,'optimize',json,jsonOut);
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf1));
    Exit;
  end;
success := CkPdf_WriteFile(pdf1,'qa_output/watersense_level1.pdf');
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf1));
    Exit;
  end;
Memo1.Lines.Add('level 1: ' + IntToStr(CkFileAccess_FileSize(fac,'qa_output/watersense_level1.pdf')));

pdf9 := CkPdf_Create();
success := CkPdf_LoadFile(pdf9,'qa_data/pdf/watersense_accomplishments.pdf');
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf9));
    Exit;
  end;
CkJsonObject_UpdateInt(json,'compressionLevel',9);
success := CkPdf_Optimize(pdf9,'optimize',json,jsonOut);
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf9));
    Exit;
  end;
success := CkPdf_WriteFile(pdf9,'qa_output/watersense_level9.pdf');
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf9));
    Exit;
  end;
Memo1.Lines.Add('level 9: ' + IntToStr(CkFileAccess_FileSize(fac,'qa_output/watersense_level9.pdf')));

//  A level outside 1..9 fails, and LastErrorText says why.
pdfBad := CkPdf_Create();
success := CkPdf_LoadFile(pdfBad,'qa_data/pdf/watersense_accomplishments.pdf');
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdfBad));
    Exit;
  end;
CkJsonObject_UpdateInt(json,'compressionLevel',12);
success := CkPdf_Optimize(pdfBad,'optimize',json,jsonOut);
if (success = False) then
  begin
    Memo1.Lines.Add('compressionLevel 12 was refused, as it should be.');
  end;

CkFileAccess_Dispose(fac);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
CkPdf_Dispose(pdf1);
CkPdf_Dispose(pdf9);
CkPdf_Dispose(pdfBad);