Working With Pages (Visual C++ 5.0 and later)

Take the following steps to create and run a program that shows how to work with pages in an OCR document. Remember, the purpose of the tutorials is to provide you with a quick and easy way to generate an OCR program.

1.

Start a new project as follows:

 

Run Microsoft Visual C++ 5.0, select the File > New menu option, then do the following:

 

a.

Click the Projects tab.

 

b.

Select "MFC AppWizard (exe)" as the project type

 

c.

In the Project name text box, specify "OCRTutor".

 

d.

In the Location text box, specify the path of the project.

 

e.

Click the OK button.

2.

In the Step 1 dialog box, do the following:

 

a.

Select Dialog based.

 

b.

Click the Next button.

3.

In the Step 2 of 4 dialog box, do the following:

 

a.

Ensure that About Box is selected.

 

b.

Ensure that 3D controls is selected.

 

c.

Select OLE controls (ActiveX controls in 5.0).

 

d.

Click the Next button.

4.

In the Step 3 of 4 dialog box, do the following:

 

a.

For comments, ensure that Yes, Please is selected.

 

b.

For how to use the MFC library, select As a shared DLL.

 

c.

Click the Next button.

5.

In the Step 4 of 4 dialog box, just click Finish.

6.

Read the New Project Information, and click OK. (The AppWizard creates the project files and opens the project).

7.

Add #import and #include statements to your program so you can access the LEAD OCR COM constants and classes:

 

a.

In the Project Workspace, click the FileView tab.

 

b.

Double-click the OCRTutor Files folder to open it.

 

c.

Double-click the Header Files folder to open it.

 

d.

Double-click the StdAfx.h file to edit it.

 

e.

Add the following lines to the end of the file (keep in mind, you may have to change the path to where the dlls reside):

#import <LTRDC14N.DLL> no_namespace, named_guids
#import <ltrvr14n.dll> no_namespace, named_guids
#import <ltr14n.dll> no_namespace, named_guids
#import <ltrpr14n.dll> no_namespace, named_guids
#import <ltrio14n.dll> no_namespace, named_guids, exclude("LoadResizeConstants") 

8.

Add seven command buttons to the main window and set the ID and Caption properties as follows:

 

ID

Caption

 

IDC_STARTUP

Start Up

 

IDC_ADDPAGE

Add Page

 

IDC_SETLANGUAGES

Set Langs

 

IDC_SHUTDOWN

Shut Down

 

IDC_REMOVEPAGE

Remove Page

 

IDC_FLIPPAGE

Flip Page

9.

Edit the OCRTutorDlg.h file and change the definition of COCRTutorDlg : CDialog by inserting the following lines after DECLARE_MESSAGE_MAP:

public: 

ILEADRasterDocument *m_pRasterDoc
ILEADRaster *m_pRaster;
ILEADRasterIO * m_pRasterIO;

10.

Edit the OCRTutorDlg.cpp file and add the following to the OnInitDialog procedure as follows:

m_pRasterDoc = NULL; 
m_pRaster = NULL;
m_pRasterIO = NULL;

11.

Press Ctrl-W to go to the MFC Class Wizard; then do the following:

 

a.

Click the Message Maps tab.

 

b.

In the Class Name combo box, select COCRTutorDlg.

 

c.

In the Object IDs list box, select IDC_STARTUP.

 

d.

In the Messages list box, select BN_CLICKED

 

e.

Click the Add function button. Choose OK for the default function name (OnStartup).

 

f.

Click the Edit Code button and code the OnStartup procedure as follows:

   HRESULT hr = CoCreateInstance(CLSID_LEADRasterDocument, NULL, CLSCTX_ALL, IID_ILEADRasterDocument, (void**)&m_pRasterDoc); 
   if (FAILED(hr))
   {
      AfxMessageBox(TEXT("Can't instantiate the OCR COM Object..."));
      return;
   }

   //create a temp LEADRaster object and use it
   //to unlock support for the OCR Document object
   hr = S_OK;
   IClassFactory2 *pCF2=NULL;
   CLSID clsid;

   static const WCHAR BASED_CODE_szID[] = L"LEADRaster.LEADRaster";

   CLSIDFromProgID(_szID, &clsid);
   hr = CoGetClassObject(clsid, CLSCTX_ALL, NULL, IID_IClassFactory, (void**)&pCF2);
   if (FAILED (hr))
   {
      AfxMessageBox (TEXT("Failed to create a Class Factory Object. The Demo will not work properly ..."));
   }
   else
   {
      //This object is licensed, so we need to pass a license key
      static const WCHAR BASED_CODE_szLic[] = L"LEADTOOLS OCX Copyright (c) 1991-2005 LEAD Technologies, Inc.";
      BSTR lpLic = SysAllocString(_szLic);
      pCF2->CreateInstanceLic(NULL, NULL, IID_IUnknown, lpLic, (void**)&m_pRaster);
      SysFreeString(lpLic);
      pCF2->Release();

      // notice the OCR key is a test key.
      m_pRaster->UnlockSupport(L_SUPPORT_OCR, "TestKey");
   }

   int nRet = m_pRasterDoc->StartUp ();
   if (nRet != 0)
   {
      AfxMessageBox(TEXT("Can't start the OCR engine."));
      m_pRasterDoc->Release();
      m_pRasterDoc = NULL;
   }

12.

Press Ctrl-W to go to the MFC Class Wizard; then do the following:

 

a.

Click the Message Maps tab.

 

b.

In the Class Name combo box, select COCRTutorDlg.

 

c.

In the Object IDs list box, select IDC_SHUTDOWN.

 

d.

In the Messages list box, select BN_CLICKED

 

e.

Click the Add function button. Choose OK for the default function name (OnShutdown).

 

f.

Click the Edit Code button and code the OnShutdown procedure as follows:

int nRet = m_pRasterDoc->ShutDown();
if(nRet != 0)
   AfxMessageBox(TEXT("The engine could not be shut down!"));
else
{
   if(m_pRasterDoc)
      m_pRasterDoc->Release();

   if(m_pRaster)
      m_pRaster->Release();

   if(m_pRasterIO)
      m_pRasterIO->Release();
}

13.

Press Ctrl-W to go to the MFC Class Wizard; then do the following:

 

a.

Click the Message Maps tab.

 

b.

In the Class Name combo box, select COCRTutorDlg.

 

c.

In the Object IDs list box, select IDC_ADDPAGE.

 

d.

In the Messages list box, select BN_CLICKED

 

e.

Click the Add function button. Choose OK for the default function name (OnAddpage).

 

f.

Click the Edit Code button and code the OnAddpage procedure as follows:

   CString  csBuffer;

   HRESULT hr = CoCreateInstance(CLSID_LEADRasterIO, NULL, CLSCTX_ALL, IID_ILEADRasterIO, (void**)&m_pRasterIO);
   if (FAILED(hr)) 
   {
      AfxMessageBox(TEXT("Can't instantiate the Raster IO Object...")); 
      return;
   }
   int nRet = m_pRasterIO->Load(m_pRaster,"c:\\OCRTEST.gif", 0, 0, 1);
   if(nRet!=0)
   {
      AfxMessageBox(TEXT("Error loading file"));
      return;
   }

   int nRet = m_pRasterDoc->AddPage(m_pRaster, 0);
   if(nRet == 0)
   {
      AfxMessageBox(TEXT("The engine added a new page to the document."));

      csBuffer.Format (TEXT("Page Width = %d\nPage Height = %d\nPage Bits Per          Pixel = %d\n"),
                     m_pRasterDoc->PageWidth [0],
                     m_pRasterDoc->PageHeight [0],
                     m_pRasterDoc->PageBitsPerPixel [0]);

      AfxMessageBox(csBuffer);
   }
   else
      AfxMessageBox(TEXT("The engine could not add a new page to the document."));

14.

Press Ctrl-W to go to the MFC Class Wizard; then do the following:

 

a.

Click the Message Maps tab.

 

b.

In the Class Name combo box, select COCRTutorDlg.

 

c.

In the Object IDs list box, select IDC_SETLANGUAGES.

 

d.

In the Messages list box, select BN_CLICKED

 

e.

Click the Add function button. Choose OK for the default function name (OnSetlanguages).

 

f.

Click the Edit Code button and code the OnSetlanguages procedure as follows:

   CString csBuffer;
   m_pRasterDoc->ActiveLanguagesCount = 1;
   m_pRasterDoc->ActiveLanguage[0] = LANGID_ENGLISH;
   nRet = m_pRasterDoc->SelectLanguages ();
   if (nRet != 0)
   {
      csBuffer.Format (TEXT("Error %d Setting English as default language"), nRet);
      AfxMessageBox (csBuffer);
   }
   else
      AfxMessageBox(TEXT("English is set as the active language."));

15.

Press Ctrl-W to go to the MFC Class Wizard; then do the following:

 

a.

Click the Message Maps tab.

 

b.

In the Class Name combo box, select COCRTutorDlg.

 

c.

In the Object IDs list box, select IDC_REMOVEPAGE.

 

d.

In the Messages list box, select BN_CLICKED

 

e.

Click the Add function button. Choose OK for the default function name (OnRemovepage).

 

f.

Click the Edit Code button and code the OnRemovepage procedure as follows:

   int nPageCount = 0;
   int i, nRet;
   CString csBuffer;

   nPageCount = m_pRasterDoc->PageCount;
   for (i=0; i<nPageCount; i++)
   {
      m_pRasterDoc->CleanupPages(TRUE);
      nRet = m_pRasterDoc->RemovePage(i);
      if (nRet == 0)
      {
         csBuffer.Format (TEXT("Page # %d was removed successfully."), i);
         AfxMessageBox(csBuffer);
      }
   }

16.

Press Ctrl-W to go to the MFC Class Wizard; then do the following:

 

a.

Click the Message Maps tab.

 

b.

In the Class Name combo box, select COCRTutorDlg.

 

c.

In the Object IDs list box, select IDC_FLIPPAGE.

 

d.

In the Messages list box, select BN_CLICKED

 

e.

Click the Add function button. Choose OK for the default function name (OnFlippage).

 

f.

Click the Edit Code button and code the OnFlippage procedure as follows:

   CString csBuffer;
   ILEADRasterProcess * pRasterProc;

   int nRet = m_pRasterDoc->ExportPage (m_pRaster, 0);
   {
      HRESULT hr = CoCreateInstance(CLSID_LEADRasterProcess, NULL, CLSCTX_ALL, IID_ILEADRasterProcess, (void**)&pRasterProc); 

      if (FAILED(hr))
      {
         AfxMessageBox(TEXT("Can't instantiate the OCR COM Object..."));
         return;
      }

      pRasterProc->Flip(m_pRaster);
      nRet = m_pRasterDoc->UpdatePage (m_pRaster, 0);
      if (nRet == 0)
      {
         csBuffer.Format (TEXT("The specified page was updated successfully."));

         AfxMessageBox (csBuffer);
      }
      else
      {
         csBuffer.Format (TEXT("Error %d in updating page bitmap for page # 0"), nRet);
         AfxMessageBox (csBuffer);
      }

      m_pRasterDoc->ActivePageIndex = 0;
      pRasterProc->Release();
   }

17.

On the Build menu, select Build OCRTutor.exe.

18.

On the Build menu, select Execute OCRTutor.exe.

19.

Save this project to use for testing other code samples.