unit MainForm;
{$I ..\..\..\Lib\PDFlibDefines.inc}

Interface

Uses
{$IFDEF XE2+}
  Winapi.Windows,
  Winapi.Messages,
  System.Classes,
  System.SysUtils,
  System.Variants,
  Vcl.Controls,
  Vcl.StdCtrls,
  Vcl.Forms,
  Vcl.Dialogs,
  Vcl.Graphics,
{$ELSE}
  Windows,
  Messages,
  Classes,
  SysUtils,
  Variants,
  Controls,
  StdCtrls,
  Forms,
  Dialogs,
  Graphics,
{$ENDIF}
  PDFlibrary;

Type
  TForm1 = Class(TForm)
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    Button2: TButton;
    Procedure Button1Click(Sender: TObject);
    Procedure Button2Click(Sender: TObject);
  Private
    { Private declarations }
  Public
    { Public declarations }
  End;

Var
  Form1: TForm1;

Implementation

{$R *.dfm}

Procedure TForm1.Button1Click(Sender: TObject);
Var
  PDF: TPDFlib;
  i: Shortint;
  Msg: String;
Begin
  OpenDialog1.Title := 'Select file for InputFileName';
  OpenDialog1.InitialDir := '.\';
  OpenDialog1.Filter := 'PDF Files (*.pdf)|*.pdf';
  OpenDialog1.Execute;
  Msg := '';
  PDF := TPDFlib.Create;
  If PDF.LoadFromFile(OpenDialog1.FileName, '') = 1 Then
  Begin
    // PDFlib.EncryptFile(InputFileName, OutputFileName, OwnerPassword, UserPassword, Strength, Permissions: Integer): Integer;
    // The Strength of encryption to use:
    // 0 = 40-bit RC4 encryption
    // 1 = 128-bit RC4 encryption
    // 2 = 128-bit AES encryption (requires Acrobat 7 or later)
    // 3 = 256-bit AES encryption (requires Acrobat 9 or later)
    // 4 = 256-bit AES encryption (requires Acorbat X or later)
    // Permissions: A value created with the EncodePermissions function:
    // TPDFlib.EncodePermissions(CanPrint, CanCopy, CanChange, CanAddNotes, CanFillFields, CanCopyAccess, CanAssemble, CanPrintFull: Integer): Integer;
    // CanPrint Set to 1 to allow print the document
    // CanCopy  Set to 1 to allow copy text and graphics
    // CanChange  Set to 1 to allow edit the document
    // CanAddNotes  Set to 1 to allow add annotations
    // CanFillFields  Set to 1 to allow fill in form fields. Only works with 128-bit encryption.
    // CanCopyAccess  Set to 1 to enable copying for use with accessibility features. Only works with 128-bit encryption.
    // CanAssemble  Set to 1 to allow assemble the document. Only works with 128-bit encryption.
    // CanPrintFull Set to 0 to force low-resolution printing only. Only works with 128-bit encryption or higher.

    Try
      i := PDF.EncryptFile(OpenDialog1.FileName, ChangeFileExt(ExtractFileName(OpenDialog1.FileName), '') +
        '-owner.pdf', 'owner', '', 2, PDF.EncodePermissions(1, 0, 0, 0, 0, 0, 0, 1));
      Case i Of
        0:
          Msg := 'The document could not be encrypted.';
        1:
          Msg := 'The document was encrypted with owner password: owner';
      End;
      ShowMessage(Msg);
    Finally
      PDF.Free;
    End;
  End;
End;

Procedure TForm1.Button2Click(Sender: TObject);
Var
  PDF: TPDFlib;
  i: Shortint;
  Msg: String;
Begin
  OpenDialog1.Title := 'Select file for InputFileName';
  OpenDialog1.InitialDir := '.\';
  OpenDialog1.Filter := 'PDF Files (*.pdf)|*.pdf';
  OpenDialog1.Execute;
  Msg := '';

  PDF := TPDFlib.Create;
  If PDF.LoadFromFile(OpenDialog1.FileName, '') = 1 Then
  Begin
    Try
      i := PDF.EncryptFile(OpenDialog1.FileName,
        ChangeFileExt(ExtractFileName(OpenDialog1.FileName), '') +
        '-owner-user.pdf', 'owner', 'user', 2, PDF.EncodePermissions(1, 0, 0, 0, 0, 0, 0, 1));
      Case i Of
        0:
          Msg := 'The document could not be encrypted.';
        1:
          Msg := 'The document was encrypted with owner password: owner & user password: user';
      End;
      ShowMessage(Msg);
    Finally
      PDF.Free;
    End;
  End;
End;

End.
