Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.7.0+

Password-Protect a PDF with AES-256 Encryption

See more PDF Protection Examples

Demonstrates the simplest way to encrypt a PDF so that it needs a password to open. The passwords come from the UserPassword and OwnerPassword properties, and Protection with the encrypt operation and an empty JSON uses the defaults: AES-256, everything encrypted, every permission granted. Protection only records the security the PDF will be saved with. Nothing is encrypted until WriteFile is called.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
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.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  pdf: TPdf;
  json: TJsonObject;
  jsonOut: TJsonObject;
  checker: TPdf;
  info: TJsonObject;
  reader: TPdf;

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

  pdf := TPdf.Create;
  success := pdf.LoadFile('qa_data/pdf/two_page_memo.pdf');
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;

  //  The passwords are taken from these two properties at the time Protection is called.
  //  The user password opens the PDF.  The owner password opens it too, and is the one needed
  //  later to change or remove the protection.
  pdf.UserPassword := 'user-secret';
  pdf.OwnerPassword := 'owner-secret';

  //  An empty JSON asks for the defaults: AES-256, everything encrypted, every permission granted.
  json := TJsonObject.Create;
  jsonOut := TJsonObject.Create;
  success := pdf.Protection('encrypt',json,jsonOut);
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;

  //  Nothing has been encrypted yet.  "pending" describes the security the PDF will be saved with.
  WriteLn('algorithm: ' + jsonOut.StringOf('pending.algorithm'));
  WriteLn('PDF version: ' + jsonOut.StringOf('pending.pdfVersion'));
  WriteLn('password needed to open: ' + jsonOut.BoolOf('pending.userPasswordRequired'));

  //  Writing the file applies it.
  success := pdf.WriteFile('qa_output/memo_encrypted.pdf');
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;

  //  PreLoadCheck reports a file's encryption without loading it, and needs no password.
  checker := TPdf.Create;
  info := TJsonObject.Create;
  success := checker.PreLoadCheck('qa_output/memo_encrypted.pdf',info);
  if (success = False) then
    begin
      WriteLn(checker.LastErrorText);
      Exit;
    end;
  WriteLn('encrypted: ' + info.BoolOf('encrypted'));
  WriteLn('openPasswordRequired: ' + info.BoolOf('openPasswordRequired'));

  //  With the password, the PDF opens normally.
  reader := TPdf.Create;
  reader.UserPassword := 'user-secret';
  success := reader.LoadFile('qa_output/memo_encrypted.pdf');
  if (success = False) then
    begin
      WriteLn(reader.LastErrorText);
      Exit;
    end;
  WriteLn('pages: ' + reader.NumPages);

  //  Sample output:

  //  algorithm: aes-256
  //  PDF version: 1.7
  //  password needed to open: True
  //  encrypted: True
  //  openPasswordRequired: True
  //  pages: 2


  pdf.Free;
  json.Free;
  jsonOut.Free;
  checker.Free;
  info.Free;
  reader.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.