Delphi DLL Requires Chilkat v11.7.0+
Delphi DLL
Audit a PDF First, Optimize Only If It Is Worth It
See more PDF Optimization Examples
Demonstrates deciding whether to rewrite a PDF at all. "audit" costs a fraction of a second and changes nothing, so a batch job can audit every file and optimize only those where enough would be saved. Here the threshold is a tenth of the file; a file already as small as it can be is left alone.Chilkat Delphi DLL Downloads
var
success: Boolean;
pdf: HCkPdf;
json: HCkJsonObject;
jsonOut: HCkJsonObject;
fileSize: Integer;
estimated: 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 watersense_accomplishments.pdf
pdf := CkPdf_Create();
success := CkPdf_LoadFile(pdf,'qa_data/pdf/watersense_accomplishments.pdf');
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
Exit;
end;
json := CkJsonObject_Create();
jsonOut := CkJsonObject_Create();
success := CkPdf_Optimize(pdf,'audit',json,jsonOut);
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
Exit;
end;
fileSize := CkJsonObject_IntOf(jsonOut,'fileSize');
estimated := CkJsonObject_IntOf(jsonOut,'estimatedSavings.total');
Memo1.Lines.Add('fileSize: ' + IntToStr(fileSize));
Memo1.Lines.Add('estimated: ' + IntToStr(estimated));
// Only bother when at least a tenth of the file would go.
if (estimated > (fileSize / 10)) then
begin
Memo1.Lines.Add('Worth optimizing.');
success := CkPdf_Optimize(pdf,'optimize',json,jsonOut);
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
Exit;
end;
success := CkPdf_WriteFile(pdf,'qa_output/watersense_optimized.pdf');
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
Exit;
end;
Memo1.Lines.Add('bytesSaved: ' + IntToStr(CkJsonObject_IntOf(jsonOut,'bytesSaved')));
end
else
begin
Memo1.Lines.Add('Already small enough; left as it is.');
end;
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);