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;
    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;
Begin
  PDF := TPDFlib.Create;
  PDF.LoadFromFile('NoUserPassword.pdf', '');
  PDF.Decrypt();
  PDF.SaveToFile('NoUserPassword_Decryted.pdf');
  PDF.Free;
End;

procedure TForm1.Button2Click(Sender: TObject);
Var
  PDF: TPDFlib;
  Pass: String;
Begin
  // If a PDF was encrypted with an owner password only, then we can remove the password and restriction directly.
  // If a PDF was encrypted with a user password aka an open password, then we need the user password to open and decrypt the file.
  PDF := TPDFlib.Create;
  Pass := InputBox('User password needed', 'Enter user password:', '');
  if Pass <> '' then
  begin
    PDF.LoadFromFile('HaveUserPassword.pdf', Pass);
    PDF.Decrypt();
    PDF.SaveToFile('HaveUserPassword_Decryted.pdf');
    PDF.Free;
  end
  else
    ShowMessage('User passowrd not entered, operation canceled.');
End;

End.
