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

interface

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

Type
  TForm1 = Class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    Edit2: TEdit;
    Label3: TLabel;
    Edit3: TEdit;
    Label4: TLabel;
    Edit4: TEdit;
    Button1: TButton;
    Button2: TButton;
    Label5: TLabel;
    Edit5: TEdit;
    Label6: TLabel;
    Edit6: TEdit;
    Label7: TLabel;
    Edit7: TEdit;
    Label8: TLabel;
    Edit8: TEdit;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    Button3: TButton;
    Label10: TLabel;
    Procedure Button1Click(Sender: TObject);
    Procedure Button2Click(Sender: TObject);
    Procedure Button3Click(Sender: TObject);
    Procedure Label10Click(Sender: TObject);
  Private
    { Private declarations }
  Public
    { Public declarations }
  End;

Var
  Form1: TForm1;

Implementation

{$R *.dfm}

Procedure TForm1.Button1Click(Sender: TObject);
Begin
  OpenDialog1.Title := 'Select file for InputFileName';
  OpenDialog1.Filter := 'PDF Files (*.pdf)|*.pdf';
  If OpenDialog1.Execute Then
    Edit1.Text := OpenDialog1.FileName;
End;

Procedure TForm1.Button2Click(Sender: TObject);
Begin
  OpenDialog1.Title := 'Select file for PFXFileName';
  OpenDialog1.Filter := 'PKCS #12 Files (*.pfx, *.p12)|*.pfx;*.p12';
  If OpenDialog1.Execute Then
    Edit3.Text := OpenDialog1.FileName;
End;

Procedure TForm1.Button3Click(Sender: TObject);
Var
  PDFlib: TPDFlib;
  I: Integer;
  Msg: String;
Begin
  Msg := '';
  If SaveDialog1.Execute Then
  Begin
    PDFlib := TPDFlib.Create;
    Try
      I := PDFlib.SignFile(Edit1.Text, Edit2.Text, Edit5.Text, SaveDialog1.FileName, Edit3.Text, Edit4.Text, Edit6.Text, Edit7.Text, Edit8.Text);
      Msg := 'SignFile result = ' + IntToStr(I) + ' "';
      Case I Of
        1: Msg := Msg + 'The file was signed successfully';
        2: Msg := Msg + 'Input PDF not found';
        3: Msg := Msg + 'Input PDF cannot be read';
        4: Msg := Msg + 'Input PDF password incorrect';
        5: Msg := Msg + 'Certificate file not found';
        6: Msg := Msg + 'Certificate file is invalid';
        7: Msg := Msg + 'Incorrect certificate password';
        8: Msg := Msg + 'Unknown certificate format';
        9: Msg := Msg + 'No private key found in certificate file';
        10: Msg := Msg + 'Could not write output file';
        11: Msg := Msg + 'Could not apply signature';
      End;
      Msg := Msg + '"';
      ShowMessage(Msg);
    Finally
      PDFlib.Free;
    End;
  End;
End;

Procedure TForm1.Label10Click(Sender: TObject);
Begin
  ShellExecute(Handle, 'open', 'https://www.loslab.com/purchase/pdf-library', Nil, Nil, SW_SHOW);
End;

End.
