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

Change the Permissions of an Encrypted PDF

See more PDF Protection Examples

Demonstrates the changePermissions operation, which changes what an encrypted PDF permits and keeps its passwords, algorithm and scope. The JSON takes the same permissions member as the encrypt operation, and a permission that is not mentioned is granted. The operation needs owner rights. For an AES-256 PDF, load with both passwords, because the user password cannot be recovered from the owner password alone.

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;
  reader: TPdf;
  empty: TJsonObject;
  info: 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 encrypted_user_password.pdf

  pdf := TPdf.Create;

  //  Changing the permissions needs owner rights.  Both passwords are given here, which is what
  //  an AES-256 PDF requires for this operation.
  pdf.UserPassword := 'secret';
  pdf.OwnerPassword := 'owner-secret';
  success := pdf.LoadFile('qa_data/pdf/encrypted_user_password.pdf');
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;

  //  No printing and no copying.  Everything not mentioned is granted.
  json := TJsonObject.Create;
  json.UpdateString('permissions.print','none');
  json.UpdateBool('permissions.copy',False);

  jsonOut := TJsonObject.Create;
  success := pdf.Protection('changePermissions',json,jsonOut);
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;
  WriteLn('print: ' + jsonOut.StringOf('pending.permissions.print'));
  WriteLn('copy: ' + jsonOut.BoolOf('pending.permissions.copy'));
  WriteLn('modify: ' + jsonOut.BoolOf('pending.permissions.modify'));
  WriteLn('password still needed to open: ' + jsonOut.BoolOf('pending.userPasswordRequired'));

  success := pdf.WriteFile('qa_output/minutes_no_print.pdf');
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;

  //  The same user password opens the result, now with the new permissions.
  reader := TPdf.Create;
  reader.UserPassword := 'secret';
  success := reader.LoadFile('qa_output/minutes_no_print.pdf');
  if (success = False) then
    begin
      WriteLn(reader.LastErrorText);
      Exit;
    end;
  empty := TJsonObject.Create;
  info := TJsonObject.Create;
  success := reader.Protection('getInfo',empty,info);
  if (success = False) then
    begin
      WriteLn(reader.LastErrorText);
      Exit;
    end;
  WriteLn('saved file, print: ' + info.StringOf('loaded.permissions.print'));
  WriteLn('saved file, copy: ' + info.BoolOf('loaded.permissions.copy'));

  //  Sample output:

  //  print: none
  //  copy: False
  //  modify: True
  //  password still needed to open: True
  //  saved file, print: none
  //  saved file, copy: False


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