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

PDF Owner Password vs. User Password

See more PDF Protection Examples

Demonstrates the difference between the two passwords of an encrypted PDF. Either one opens the file. The user password gives the rights the permissions grant. The owner password gives owner rights: no restriction applies, and it is the only one that lets the security be changed or removed. Every Protection operation except getInfo needs owner rights, and Chilkat does not offer a way around that.

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;
  json: TJsonObject;
  jsonOut: TJsonObject;
  user: TPdf;
  owner: 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 encrypted_user_password.pdf

  json := TJsonObject.Create;
  jsonOut := TJsonObject.Create;

  //  Opened with the user password.
  user := TPdf.Create;
  user.UserPassword := 'secret';
  success := user.LoadFile('qa_data/pdf/encrypted_user_password.pdf');
  if (success = False) then
    begin
      WriteLn(user.LastErrorText);
      Exit;
    end;
  success := user.Protection('getInfo',json,jsonOut);
  if (success = False) then
    begin
      WriteLn(user.LastErrorText);
      Exit;
    end;
  WriteLn('opened with: ' + jsonOut.StringOf('loaded.openedWith'));

  //  Changing the security needs owner rights, so this is refused.
  success := user.Protection('removeEncryption',json,jsonOut);
  if (success = False) then
    begin
      WriteLn('removeEncryption was refused: the PDF was not opened with its owner password.');
    end;

  //  Opened with the owner password.
  owner := TPdf.Create;
  owner.OwnerPassword := 'owner-secret';
  success := owner.LoadFile('qa_data/pdf/encrypted_user_password.pdf');
  if (success = False) then
    begin
      WriteLn(owner.LastErrorText);
      Exit;
    end;
  success := owner.Protection('getInfo',json,jsonOut);
  if (success = False) then
    begin
      WriteLn(owner.LastErrorText);
      Exit;
    end;
  WriteLn('opened with: ' + jsonOut.StringOf('loaded.openedWith'));

  //  Now it is allowed.
  success := owner.Protection('removeEncryption',json,jsonOut);
  if (success = False) then
    begin
      WriteLn(owner.LastErrorText);
      Exit;
    end;
  WriteLn('removeEncryption succeeded; will be saved encrypted: ' + jsonOut.BoolOf('pending.encrypted'));

  //  Sample output:

  //  opened with: user
  //  removeEncryption was refused: the PDF was not opened with its owner password.
  //  opened with: owner
  //  removeEncryption succeeded; will be saved encrypted: False


  json.Free;
  jsonOut.Free;
  user.Free;
  owner.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.