Unit MainForm;

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

Interface

// You need Delphi 2010 or above to compile this demo because it uses TStopwatch from Diagnostics unit which only contained in
// Delphi 2010 and above editions. If you want to compile this demo using older versions of Delphi, you can try to remove the
// TStopwatch related codes.
Uses
{$IFDEF XE2+}
  Winapi.Windows, Winapi.Messages, System.Variants, System.SysUtils,
  System.Classes, Vcl.Graphics, Vcl.Printers, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls,
  Vcl.StdCtrls, Vcl.ExtCtrls, System.Diagnostics,
{$ELSE}
  Windows,
  Messages,
  Variants,
  SysUtils,
  Classes,
  Graphics,
  Controls,
  Forms,
  Dialogs,
  StdCtrls,
  ExtCtrls,
  ComCtrls,
{$IFDEF D2010+}
  Diagnostics,
{$ENDIF}
{$ENDIF}
  PDFlibrary;

Type
  TForm1 = Class(TForm)
    btnBrowse: TButton;
    cmbOptions: TComboBox;
    Label2: TLabel;
    btnExtract: TButton;
    edtPDF: TEdit;
    dlgOpen: TOpenDialog;
    ProgressBar1: TProgressBar;
    Label3: TLabel;
    Label1: TLabel;
    Procedure btnBrowseClick(Sender: TObject);
    Procedure FormCreate(Sender: TObject);
    Procedure btnExtractClick(Sender: TObject);
  Private
    { Private declarations }
  Public
    { Public declarations }
    PDF: TPDFlib;
  End;

Var
  Form1: TForm1;

Implementation

{$R *.dfm}

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

Procedure TConvThread.Execute;
Var
  i: Integer;
{$IFDEF D2010+}
  sw: TStopwatch;
{$ENDIF}
  Output: TStringList;
  PdfText: String;
  TxtName: String;
Begin
  FreeOnTerminate := True;
  With Form1 Do
  Begin
{$IFDEF D2010+}
    Label3.Caption := 'Elapsed time in ms: ...';
{$ELSE}
    Label3.Caption :=
      'Need to compile with Delphi 2010 and above to show progressing bar ...';
{$ENDIF}
    With TPDFlib.Create Do
      Try
        If (LoadFromFile(edtPDF.Text, '') = 1) Then
        Begin
{$IFDEF D2010+}
          sw := TStopwatch.StartNew;
          ProgressBar1.Min := 0;
          ProgressBar1.Max := PageCount;
          ProgressBar1.Position := 0;
          ProgressBar1.Step := 1;
{$ENDIF}
          TxtName := StringReplace(ExtractFileName(edtPDF.Text), '.pdf', '.txt', [rfReplaceAll, rfIgnoreCase]);

          Begin
            Output := TStringList.Create;
            For i := 1 To PageCount Do
            Begin
              PdfText := PdfText + ExtractFilePageText(edtPDF.Text, '', i, cmbOptions.ItemIndex);
              //PdfText := PdfText + ExtractFilePageText(edtPDF.Text, '', i, 7);
{$IFDEF D2010+}
              ProgressBar1.StepIt;
              ProgressBar1.Update;
{$ENDIF}
            End;
            Output.Add(PdfText);
            Output.SaveToFile('.\' + TxtName);
            PdfText := '';
            Output.Free;
          End;
{$IFDEF D2010+}
          Label3.Caption := 'Elapsed time in ms: ' +
            IntToStr(sw.ElapsedMilliseconds);
{$ENDIF}
        End
        Else
          MessageBox(Handle, 'Error, file could not be opened.', 'Error:',
            MB_OK or MB_ICONSTOP)
      Finally
        Free;
      End;
  End;
End;

Procedure TForm1.btnBrowseClick(Sender: TObject);
Begin
  dlgOpen.Options := [ofFileMustExist];
  dlgOpen.Filter := 'PDF files|*.pdf';
  If dlgOpen.Execute Then
    edtPDF.Text := dlgOpen.FileName;
End;

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

Procedure TForm1.FormCreate(Sender: TObject);
Begin
  cmbOptions.Items.Add('0. Extract text in human readable format');
  cmbOptions.Items.Add('1. Deprecated - Do not select');
  cmbOptions.Items.Add('2. Return a CSV string including font, color, size and position of each piece of text on the page');
  cmbOptions.Items.Add('3. Return a CSV string for each piece of text on page with Font Name, PdfText Color, PdfText Size, Co-ordinates, PdfText');
  cmbOptions.Items.Add('4. Similar to option 3, but individual words are returned, making searching for words easier');
  cmbOptions.Items.Add('5. Similar to option 3 but character widths are output after each block of text');
  cmbOptions.Items.Add('6. Similar to option 4 but character widths are output after each line of text');
  cmbOptions.Items.Add('7. Extract text in human readable format with improved accuracy compared to option 0');
  cmbOptions.Items.Add('8. Similar output format as option 0 but using the more accurate algorithm. Returns unformatted lines');
  cmbOptions.ItemIndex := 0;
End;

End.
