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

Optimizing a Signed PDF (PreserveSignatures)

See more PDF Optimization Examples

Demonstrates what happens when a signed PDF is optimized. Optimizing rewrites the document, and a rewrite breaks every existing digital signature, so with PreserveSignatures true (the default) Optimize refuses and LastErrorText says why. Setting PreserveSignatures to false allows it, and the signature that was in the file no longer verifies afterwards. "audit" is always allowed, because it changes nothing.

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
pdf: HCkPdf;
json: HCkJsonObject;
jsonOut: HCkJsonObject;
pdf2: HCkPdf;
sigInfo: HCkJsonObject;
verified: Boolean;

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 invoice_signed.pdf

pdf := CkPdf_Create();
success := CkPdf_LoadFile(pdf,'qa_data/pdf/invoice_signed.pdf');
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
    Exit;
  end;

Memo1.Lines.Add('signatures: ' + IntToStr(CkPdf_getNumSignatures(pdf)));

json := CkJsonObject_Create();
jsonOut := CkJsonObject_Create();

//  An audit changes nothing, so it is allowed on a signed PDF.
success := CkPdf_Optimize(pdf,'audit',json,jsonOut);
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
    Exit;
  end;
Memo1.Lines.Add('audit ok, estimated savings: ' + IntToStr(CkJsonObject_IntOf(jsonOut,'estimatedSavings.total')));

//  Optimizing is refused while PreserveSignatures is true.
success := CkPdf_Optimize(pdf,'optimize',json,jsonOut);
if (success = False) then
  begin
    Memo1.Lines.Add('Refused while PreserveSignatures is true.');
  end;

//  Allow the rewrite.  Do this only when the signatures no longer matter -- an invoice being
//  archived, say, where the signed original is kept elsewhere.
CkPdf_putPreserveSignatures(pdf,False);
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/invoice_optimized.pdf');
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
    Exit;
  end;
Memo1.Lines.Add('optimized, bytesSaved: ' + IntToStr(CkJsonObject_IntOf(jsonOut,'bytesSaved')));

//  The signature is still in the file, but it no longer verifies: the bytes it covered have changed.
pdf2 := CkPdf_Create();
success := CkPdf_LoadFile(pdf2,'qa_output/invoice_optimized.pdf');
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf2));
    Exit;
  end;
Memo1.Lines.Add('signatures after: ' + IntToStr(CkPdf_getNumSignatures(pdf2)));

sigInfo := CkJsonObject_Create();
verified := CkPdf_VerifySignature(pdf2,0,sigInfo);
Memo1.Lines.Add('verifies after optimizing: ' + IntToStr(Ord(verified)));

CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
CkPdf_Dispose(pdf2);
CkJsonObject_Dispose(sigInfo);