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

Get the Encryption and Permissions of a Loaded PDF

See more PDF Protection Examples

Demonstrates the getInfo operation of Protection, which reports how the loaded PDF is protected: the method and algorithm, whether a password is needed to open it, how it was opened (with the user password, the owner password, an empty password, or a certificate), and each permission. The loaded member describes the file as it is, the pending member describes the security it will be saved with, and changed says whether the two differ. getInfo is the one operation that does not need owner rights.

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;

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;

  //  The password must be set before loading.
  pdf.UserPassword := 'secret';
  success := pdf.LoadFile('qa_data/pdf/encrypted_user_password.pdf');
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;

  json := TJsonObject.Create;
  jsonOut := TJsonObject.Create;
  success := pdf.Protection('getInfo',json,jsonOut);
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;

  jsonOut.EmitCompact := False;
  WriteLn(jsonOut.Emit());

  //  "loaded" is the security the file has.  "pending" is the security it will be saved with, which
  //  is the same until an operation such as encrypt or changePermissions changes it.
  WriteLn('algorithm: ' + jsonOut.StringOf('loaded.algorithm'));
  WriteLn('opened with: ' + jsonOut.StringOf('loaded.openedWith'));
  WriteLn('password needed to open: ' + jsonOut.BoolOf('loaded.userPasswordRequired'));
  WriteLn('may print: ' + jsonOut.StringOf('loaded.permissions.print'));
  WriteLn('may copy: ' + jsonOut.BoolOf('loaded.permissions.copy'));
  WriteLn('changed: ' + jsonOut.BoolOf('changed'));

  //  Sample output:

  //  {
  //    "changed": false,
  //    "loaded": {
  //      "encrypted": true,
  //      "method": "password",
  //      "algorithm": "rc4-128",
  //      "pdfVersion": "1.3",
  //      "scope": "all",
  //      "userPasswordRequired": true,
  //      "ownerPasswordSet": true,
  //      "openedWith": "user",
  //      "permissions": {
  //        "print": "full",
  //        "modify": true,
  //        "copy": true,
  //        "accessibility": true,
  //        "annotate": true,
  //        "fillForms": true,
  //        "assemble": true
  //      }
  //    },
  //    "pending": {
  //      "encrypted": true,
  //      "method": "password",
  //      "algorithm": "rc4-128",
  //      "pdfVersion": "1.3",
  //      "scope": "all",
  //      "userPasswordRequired": true,
  //      "ownerPasswordSet": true,
  //      "permissions": {
  //        "print": "full",
  //        "modify": true,
  //        "copy": true,
  //        "accessibility": true,
  //        "annotate": true,
  //        "fillForms": true,
  //        "assemble": true
  //      }
  //    }
  //  }
  //  
  //  algorithm: rc4-128
  //  opened with: user
  //  password needed to open: True
  //  may print: full
  //  may copy: True
  //  changed: False


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