Delphi ActiveX Requires Chilkat v11.7.0+
Delphi ActiveX
Encrypt and Sign a PDF, and Sign an Encrypted PDF
See more PDF Protection Examples
Demonstrates signing together with encryption. A pending encrypt is applied by SignPdf before it signs, so one call produces a PDF that is both encrypted and signed. A PDF that is already encrypted can be signed too, and stays encrypted. When it was opened with only the user password its permissions decide: adding a signature field needs both the modify and the annotate permission. Loading the PDF with its owner password lifts that.Chilkat Delphi ActiveX Downloads
var
success: Integer;
cert: TChilkatCert;
pdf: TChilkatPdf;
json: TChilkatJsonObject;
jsonOut: TChilkatJsonObject;
sigOpts: TChilkatJsonObject;
check: TChilkatPdf;
empty: TChilkatJsonObject;
info: TChilkatJsonObject;
sigInfo: TChilkatJsonObject;
verified: Integer;
owner: TChilkatPdf;
twice: TChilkatPdf;
begin
success := 0;
// 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 two_page_memo.pdf
// The signing certificate, with its private key.
cert := TChilkatCert.Create(Self);
success := cert.LoadPfxFile('qa_data/pfx/myPdfSigningCert.pfx','secret');
if (success = 0) then
begin
Memo1.Lines.Add(cert.LastErrorText);
Exit;
end;
pdf := TChilkatPdf.Create(Self);
success := pdf.LoadFile('qa_data/pdf/two_page_memo.pdf');
if (success = 0) then
begin
Memo1.Lines.Add(pdf.LastErrorText);
Exit;
end;
// Encrypt with AES-256 and do not permit modifying.
pdf.UserPassword := 'user-secret';
pdf.OwnerPassword := 'owner-secret';
json := TChilkatJsonObject.Create(Self);
json.UpdateBool('permissions.modify',0);
jsonOut := TChilkatJsonObject.Create(Self);
success := pdf.Protection('encrypt',json.ControlInterface,jsonOut.ControlInterface);
if (success = 0) then
begin
Memo1.Lines.Add(pdf.LastErrorText);
Exit;
end;
// The encryption is still pending. SignPdf applies it first and signs the encrypted result.
success := pdf.SetSigningCert(cert.ControlInterface);
if (success = 0) then
begin
Memo1.Lines.Add(pdf.LastErrorText);
Exit;
end;
sigOpts := TChilkatJsonObject.Create(Self);
sigOpts.UpdateBool('invisibleSignature',1);
sigOpts.UpdateInt('signingCertificateV2',1);
sigOpts.UpdateInt('signingTime',1);
success := pdf.SignPdf(sigOpts.ControlInterface,'qa_output/memo_encrypted_signed.pdf');
if (success = 0) then
begin
Memo1.Lines.Add(pdf.LastErrorText);
Exit;
end;
// The result is encrypted and signed.
check := TChilkatPdf.Create(Self);
check.UserPassword := 'user-secret';
success := check.LoadFile('qa_output/memo_encrypted_signed.pdf');
if (success = 0) then
begin
Memo1.Lines.Add(check.LastErrorText);
Exit;
end;
empty := TChilkatJsonObject.Create(Self);
info := TChilkatJsonObject.Create(Self);
success := check.Protection('getInfo',empty.ControlInterface,info.ControlInterface);
if (success = 0) then
begin
Memo1.Lines.Add(check.LastErrorText);
Exit;
end;
Memo1.Lines.Add('algorithm: ' + info.StringOf('loaded.algorithm'));
Memo1.Lines.Add('signatures: ' + IntToStr(check.NumSignatures));
sigInfo := TChilkatJsonObject.Create(Self);
verified := check.VerifySignature(0,sigInfo.ControlInterface);
Memo1.Lines.Add('signature verifies: ' + IntToStr(Ord(verified)));
// Signing an encrypted PDF. This one was opened with only the user password, and its
// permissions do not allow modifying, so another signature is refused.
success := check.SetSigningCert(cert.ControlInterface);
if (success = 0) then
begin
Memo1.Lines.Add(check.LastErrorText);
Exit;
end;
success := check.SignPdf(sigOpts.ControlInterface,'qa_output/memo_signed_twice.pdf');
if (success = 0) then
begin
Memo1.Lines.Add('A second signature was refused: the permissions do not allow it.');
end;
// Loaded with the owner password, the same PDF can be signed. It stays encrypted, and the
// first signature stays valid because the new one is added as an incremental update.
owner := TChilkatPdf.Create(Self);
owner.UserPassword := 'user-secret';
owner.OwnerPassword := 'owner-secret';
success := owner.LoadFile('qa_output/memo_encrypted_signed.pdf');
if (success = 0) then
begin
Memo1.Lines.Add(owner.LastErrorText);
Exit;
end;
success := owner.SetSigningCert(cert.ControlInterface);
if (success = 0) then
begin
Memo1.Lines.Add(owner.LastErrorText);
Exit;
end;
success := owner.SignPdf(sigOpts.ControlInterface,'qa_output/memo_signed_twice.pdf');
if (success = 0) then
begin
Memo1.Lines.Add(owner.LastErrorText);
Exit;
end;
twice := TChilkatPdf.Create(Self);
twice.UserPassword := 'user-secret';
success := twice.LoadFile('qa_output/memo_signed_twice.pdf');
if (success = 0) then
begin
Memo1.Lines.Add(twice.LastErrorText);
Exit;
end;
Memo1.Lines.Add('signatures after signing with owner rights: ' + IntToStr(twice.NumSignatures));
verified := twice.VerifySignature(0,sigInfo.ControlInterface);
Memo1.Lines.Add('first signature still verifies: ' + IntToStr(Ord(verified)));
verified := twice.VerifySignature(1,sigInfo.ControlInterface);
Memo1.Lines.Add('second signature verifies: ' + IntToStr(Ord(verified)));
// Sample output:
// algorithm: aes-256
// signatures: 1
// signature verifies: True
// A second signature was refused: the permissions do not allow it.
// signatures after signing with owner rights: 2
// first signature still verifies: True
// second signature verifies: True