Lazarus Pascal Requires Chilkat v11.7.0+
Lazarus Pascal
Encrypt a PDF for a Certificate Instead of a Password
See more PDF Protection Examples
Demonstrates certificate security, where a PDF is encrypted for one or more recipients and each opens it with their own certificate and private key. No password is involved. Only the public certificate of a recipient is needed to encrypt. Each recipient has its own permissions, and the changeSecurity permission gives a recipient owner rights. PreLoadCheck tells an application that a file is certificate-encrypted and which certificates can open it. A PDF that cannot be opened, because no matching certificate and private key were provided, fails to load.Chilkat Lazarus Pascal Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.Pdf,
Chilkat.Cert,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
recipient: TCert;
pdf: TPdf;
json: TJsonObject;
jsonOut: TJsonObject;
reader: TPdf;
info: TJsonObject;
myCert: TCert;
empty: TJsonObject;
loadedInfo: TJsonObject;
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 two_page_memo.pdf
// The recipient's certificate. Only the public certificate is needed to encrypt.
recipient := TCert.Create;
success := recipient.LoadFromFile('qa_data/certs/recipient.cer');
if (success = False) then
begin
WriteLn(recipient.LastErrorText);
Exit;
end;
pdf := TPdf.Create;
success := pdf.LoadFile('qa_data/pdf/two_page_memo.pdf');
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
// Call AddEncryptCert once for each recipient.
success := pdf.AddEncryptCert(recipient);
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
// recipientPermissions[0] belongs to the first certificate added. changeSecurity gives that
// recipient owner rights, so the file can be managed later.
json := TJsonObject.Create;
json.UpdateString('method','certificate');
json.UpdateBool('recipientPermissions[0].changeSecurity',True);
jsonOut := TJsonObject.Create;
success := pdf.Protection('encrypt',json,jsonOut);
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
WriteLn('method: ' + jsonOut.StringOf('pending.method'));
WriteLn('algorithm: ' + jsonOut.StringOf('pending.algorithm'));
WriteLn('recipients: ' + jsonOut.SizeOfArray('pending.recipients'));
success := pdf.WriteFile('qa_output/memo_for_recipient.pdf');
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
// PreLoadCheck says how the file is protected, without loading it.
reader := TPdf.Create;
info := TJsonObject.Create;
success := reader.PreLoadCheck('qa_output/memo_for_recipient.pdf',info);
if (success = False) then
begin
WriteLn(reader.LastErrorText);
Exit;
end;
WriteLn('saved file, method: ' + info.StringOf('method'));
WriteLn('saved file, recipients: ' + info.SizeOfArray('recipients'));
// The recipient opens it with the certificate and its private key, here from a .pfx file.
// (On Windows, a certificate that is installed in the Windows certificate store with its private
// key is found automatically when the PDF is loaded, and SetDecryptCert is not needed for it.)
myCert := TCert.Create;
success := myCert.LoadPfxFile('qa_data/pfx/recipient.pfx','pfx-password');
if (success = False) then
begin
WriteLn(myCert.LastErrorText);
Exit;
end;
success := reader.SetDecryptCert(myCert);
if (success = False) then
begin
WriteLn(reader.LastErrorText);
Exit;
end;
success := reader.LoadFile('qa_output/memo_for_recipient.pdf');
if (success = False) then
begin
WriteLn(reader.LastErrorText);
Exit;
end;
empty := TJsonObject.Create;
loadedInfo := TJsonObject.Create;
success := reader.Protection('getInfo',empty,loadedInfo);
if (success = False) then
begin
WriteLn(reader.LastErrorText);
Exit;
end;
WriteLn('opened with: ' + loadedInfo.StringOf('loaded.openedWith'));
WriteLn('pages: ' + reader.NumPages);
// Sample output:
// method: certificate
// algorithm: aes-256
// recipients: 1
// saved file, method: certificate
// saved file, recipients: 1
// opened with: certificate
// pages: 2
recipient.Free;
pdf.Free;
json.Free;
jsonOut.Free;
reader.Free;
info.Free;
myCert.Free;
empty.Free;
loadedInfo.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.