Unit MainForm;

{$I ..\..\..\Lib\PDFlibDefines.inc}

Interface

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

Type
  TForm1 = Class(TForm)
    Button3: TButton;
    Label10: TLabel;
    dlgOpen: TOpenDialog;
    Button1: TButton;
    Edit1: TEdit;
    ListBox1: TListBox;
    Button2: TButton;
    Procedure Button3Click(Sender: TObject);
    Procedure Label10Click(Sender: TObject);
    Procedure Label10MouseLeave(Sender: TObject);
    Procedure Label10MouseEnter(Sender: TObject);
    Procedure Button1Click(Sender: TObject);
    Procedure Button2Click(Sender: TObject);
    Procedure Edit1Exit(Sender: TObject);
  Private
    { Private declarations }
  Public
    { Public declarations }
  End;

Var
  Form1: TForm1;

Implementation

{$R *.dfm}

Procedure TForm1.Button3Click(Sender: TObject);
Var
  i, iImages: Integer;
  StrMessage, strInputFile, strOutputFile, strImageFile, strImageExt: String;
Begin

  // Check to see that user actually has any items in the list
  If (ListBox1.Items.Count < 1) Then
  Begin
    MessageBox(Handle, 'Please load PDF file to the list and try again.', 'Error:', MB_OK or MB_ICONSTOP);
    Abort;
  End;
  With TPDFlib.Create Do
    Try
      For i := 0 To ListBox1.Items.Count - 1 Do
      Begin
        // PDF output filename
        strInputFile := ListBox1.Items.Strings[i];
      strOutputFile := StringReplace((Edit1.Text + '\' + ExtractFileName(strInputFile)), ExtractFileExt(strInputFile), '', [rfIgnoreCase]);
        // Load the PDF file
        If (LoadFromFile(strInputFile, '') = 1) Then
        Begin
          // Check to see if the document contains any images
          If FindImages >= 1 Then
          Begin
            // Iterate through each image in the document
            For iImages := 1 To FindImages Do
            Begin
              strImageFile := strOutputFile + Format('_%.4d', [iImages]);
              // Check to see if the ImageID is valid
              If SelectImage(GetImageID(iImages)) = 1 Then
                // Get the file extention of the image
                Case ImageType Of
                0: strImageExt := ''; //Image is not valid
                1: strImageExt := '.jpg';
                2: strImageExt := '.bmp';
                3: strImageExt := '.tiff';
                Else
                  strImageExt := '.other' // Unknown/Undefined file extention
                End;
              // If we have a file extention, extract and save the image
              If Trim(strImageExt) <> '' Then
                SaveImageToFile(strImageFile + strImageExt);
              If iImages = FindImages Then
              MessageBox(Handle, 'Images have extracted from the PDF.', ' Y-a-h-o-!', MB_OK);
            End;
          End;
        End
        Else
        Begin
          StrMessage := StrMessage + 'Error, file could not be opened.';
          MessageBox(Handle, PChar(StrMessage), 'Error:', MB_OK or MB_ICONSTOP);
        End;
      End;

    Finally
      Free;
    End;

End;

Procedure TForm1.Label10MouseEnter(Sender: TObject);
Begin
  Label10.Font.Color := clRed;
End;

Procedure TForm1.Label10MouseLeave(Sender: TObject);
Begin
  Label10.Font.Color := clBlue;
End;

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

Procedure TForm1.Button1Click(Sender: TObject);
Var
  dlgOpen: TOpenDialog; // Open dialog variable
  i: Integer;
Begin
  ListBox1.Items.Clear;
  dlgOpen := TOpenDialog.Create(self);
  dlgOpen.InitialDir := ExtractFilePath(ParamStr(0));
  dlgOpen.Options := [ofAllowMultiSelect];
  dlgOpen.Filter := 'PDF Files (PDF)|*.pdf';
  dlgOpen.FilterIndex := 1;
  If dlgOpen.Execute() Then
  Begin
    For i := 0 To dlgOpen.Files.Count - 1 Do
      ListBox1.Items.Add(dlgOpen.Files[i]);
  End;
  dlgOpen.Free;
End;

Procedure TForm1.Button2Click(Sender: TObject);
Var
  sFolder: String;
Begin
  sFolder := BrowseForFolder('', '', True);
  If (DirectoryExists(sFolder)) and (Length(Trim(sFolder)) > 0) Then
    Edit1.Text := sFolder;
End;

Procedure TForm1.Edit1Exit(Sender: TObject);
Begin
  If Edit1.Text = '' Then
    Edit1.Text := '.\extracted';
End;

End.
