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

interface

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

Type
  TForm1 = Class(TForm)
    Button1: TButton;
    ProgressBar1: TProgressBar;
    Label1: TLabel;
    Procedure Button1Click(Sender: TObject);
  Private
    { Private declarations }
  Public
    { Public declarations }
  End;

Var
  Form1: TForm1;

Implementation

{$R *.dfm}

Procedure EmptyFolder(Path: String);
Var
  sr: TSearchRec;
Begin
  If FindFirst(Path + '*.*', faAnyFile, sr) = 0 Then
  Begin
    Repeat
      DeleteFile(Path + sr.Name);
    Until FindNext(sr) <> 0;
    FindClose(sr);
  End;
End;

Type
  TConvThread = Class(TThread)
  Protected
    Procedure Execute; Override;
  End;

  // function TPDFlib.RenderDocumentToFile(DPI: Double;
  // StartPage, EndPage, Options: Integer; FileName: WideString): Integer;
  // Options  int32  0 = BMP output
  // 1 = JPEG output
  // 2 = WMF output
  // 3 = EMF output
  // 4 = EPS output
  // 5 = PNG output
  // 6 = GIF output
  // 7 = TIFF (LZW) output
  // 8 = EMF+ output
  // 9 = HTML5 output
  // 10 = TIFF (G4) output

Procedure TConvThread.Execute;
Var
  i: Integer;
{$IFDEF D2010+}
  sw: TStopwatch;
{$ENDIF}
Begin
  FreeOnTerminate := True;
{$IFDEF D2010+}
  Form1.Label1.Caption := 'Elapsed time in ms: ...';
{$ELSE}
  Form1.Label1.Caption := 'Converting, Please wait ...';
{$ENDIF}
  EmptyFolder(GetCurrentDir + '\Converted\');
  With TPDFlib.Create Do
    Try
{$IFDEF D2010+}
      sw := TStopwatch.StartNew;
{$ENDIF}
      If (LoadFromFile('PdfToImage.pdf', '') = 1) Then
      Begin
{$IFDEF D2010+}
      Form1.ProgressBar1.Min := 0;
      Form1.ProgressBar1.Max := PageCount;
      Form1.ProgressBar1.Position := 0;
      Form1.ProgressBar1.Step := 1;
{$ENDIF}
      // Render each page of the document to a separate file.
      For i := 1 To PageCount Do
      Begin
        RenderPageToFile(150, i, 1, 'Converted\image' + IntToStr(i) + '.jpg');
{$IFDEF D2010+}
        Form1.ProgressBar1.StepIt;
        Form1.ProgressBar1.Update;
{$ENDIF}
        End;
{$IFDEF D2010+}
      Form1.Label1.Caption := 'Elapsed time in ms: ' + IntToStr(sw.ElapsedMilliseconds);
{$ELSE}
      Form1.Label1.Caption := 'Conversion finished!';
{$ENDIF}
      ShellExecute(Application.Handle, PChar('explore'), 'Converted', Nil,
          Nil, SW_SHOWNORMAL);
      End
      Else
        MessageBox(Handle, 'Error, file could not be opened.', 'Error:',
          MB_OK or MB_ICONSTOP);
    Finally
      Free;
    End;
End;

Procedure TForm1.Button1Click(Sender: TObject);
Begin
  TConvThread.Create(False);
End;

End.
