//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "PDFium"
#pragma resource "*.dfm"
TFormMain *FormMain;
//---------------------------------------------------------------------------
__fastcall TFormMain::TFormMain(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::SpeedButtonPdfFileClick(TObject *Sender)
{
  OpenDialog->FileName = EditPdfFile->Text;
  if (OpenDialog->Execute())
	EditPdfFile->Text = OpenDialog->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ButtonExtractClick(TObject *Sender)
{
  ProgressBar->Position = 0;
  EditPdfFile->Enabled = false;
  SpeedButtonPdfFile->Enabled = false;
  ButtonExtract->Enabled = false;
  Screen->Cursor = crHourGlass;
  try
  {
	Pdf->FileName = EditPdfFile->Text;
	Pdf->PageNumber = 0;
	Pdf->Active = true;
	ProgressBar->Max = Pdf->PageCount;

	UnicodeString BaseName = ExtractFileName(Pdf->FileName);
	BaseName = BaseName.SubString(1, BaseName.Length() - ExtractFileExt(Pdf->FileName).Length());

	UnicodeString ImagesDir = ExtractFilePath(Pdf->FileName) + BaseName + L"-Images\\";
	if (!DirectoryExists(ImagesDir))
	  ForceDirectories(ImagesDir);

	UnicodeString FileName = ImagesDir + BaseName;

	for (int I = 1; I <= Pdf->PageCount; ++I)
	{
	  Pdf->PageNumber = I;
	  for (int J = 0; J < Pdf->BitmapCount; ++J)
	  {
		TBitmap *Bitmap = Pdf->Bitmap[J];
		try
		{
		  Bitmap->SaveToFile(FileName + "_Page" + IntToStr(I) + "_Image" + IntToStr(J + 1) + ".bmp");
		}
		__finally
		{
		  delete Bitmap;
		}
	  }
	  ProgressBar->Position = I;
	}
  }
  __finally
  {
	Screen->Cursor = crDefault;
	Pdf->Active = false;
	EditPdfFile->Enabled = true;
	SpeedButtonPdfFile->Enabled = true;
	ButtonExtract->Enabled = true;
  }
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::EditPdfFileChange(TObject *Sender)
{
  ButtonExtract->Enabled = FileExists(EditPdfFile->Text);
}
//---------------------------------------------------------------------------

