Delphi DLL Requires Chilkat v11.7.0+
Delphi DLL
Watermark a Signed PDF Without Breaking the Signature
See more PDF Watermark Examples
Demonstrates the one approach a signed PDF accepts while its signatures are being preserved. Adding a watermark to the page content rewrites the page, which invalidates every existing signature, so with PreserveSignatures true (the default) only the annotation approach is allowed: an annotation is added as an incremental update that leaves the signed bytes untouched. Removing anything is refused for the same reason. Set PreserveSignatures to false to allow the other approaches, and the signatures in the file stop verifying.Chilkat Delphi DLL Downloads
var
success: Boolean;
pdf: HCkPdf;
json: HCkJsonObject;
jsonOut: HCkJsonObject;
check: 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();
CkJsonObject_UpdateString(json,'type','text');
CkJsonObject_UpdateString(json,'text','PAID');
CkJsonObject_UpdateString(json,'id','paid');
CkJsonObject_UpdateString(json,'color','#2E7D32');
CkJsonObject_UpdateString(json,'position.align','topRight');
CkJsonObject_UpdateNumber(json,'position.margin','36');
// An artifact watermark changes the page content, so it is refused while the signatures matter.
CkJsonObject_UpdateString(json,'approach','artifact');
success := CkPdf_Watermark(pdf,'add',json,jsonOut);
if (success = False) then
begin
Memo1.Lines.Add('The artifact approach was refused on a signed PDF, as it should be.');
end;
// An annotation is added without touching the signed bytes.
CkJsonObject_UpdateString(json,'approach','annotation');
success := CkPdf_Watermark(pdf,'add',json,jsonOut);
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
Exit;
end;
Memo1.Lines.Add('annotation added, pages: ' + CkJsonObject__stringOf(jsonOut,'pages'));
success := CkPdf_WriteFile(pdf,'qa_output/invoice_paid.pdf');
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
Exit;
end;
// The signature still verifies: the original bytes are unchanged and the annotation was appended.
check := CkPdf_Create();
success := CkPdf_LoadFile(check,'qa_output/invoice_paid.pdf');
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(check));
Exit;
end;
sigInfo := CkJsonObject_Create();
verified := CkPdf_VerifySignature(check,0,sigInfo);
Memo1.Lines.Add('signature still verifies: ' + IntToStr(Ord(verified)));
// Sample output:
// signatures: 1
// The artifact approach was refused on a signed PDF, as it should be.
// annotation added, pages: 1
// signature still verifies: True
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
CkPdf_Dispose(check);
CkJsonObject_Dispose(sigInfo);