Working with Pages

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

1.

Create a new directory in the LTWINxxx\EXAMPLES\DLL directory called EZOCRDOC.

2.

Copy everything in the EZFUNC directory into the EZOCRDOC directory.

3.

Compile the project as it is and run EZFUNC32.exe to familiarize yourself with the basic program. You can run the exe file from the MS-DOS prompt, or set the image file to add through the Build->Settings->Debug->Program Arguments menu.

4.

On the Insert menu, select Files into Project. Add the Ltdoc_n.lib file to the project from the LTWINxxx\LIB directory.

5.

Define the following global variables in EZFUNC.C in the EZOCRDOC directory:

L_HDOC hDoc; /* OCR Document handle */
PAGEINFO PageInfo;
LANGIDS LangId[1];

6.

Add the following include statement after the include statements in EZFUNC.C in the EZOCRDOC directory:

#include "..\\..\\..\\include\\ltdoc.h" /* LEADTOOLS main OCR document header file */

7.

Add the following statements in the WinMain function as follows:

 

a.

Add the following before calling InitInstance() statement

   /* Note that this is a sample key, which will not work in your toolkit. You should replace the sample key */ 
   L_UnlockSupport(L_SUPPORT_OCR, TEXT("TestKey"));
   if (L_DocStartUp(&hDoc) != SUCCESS)
      return 0;

 

b.

Add the following after the GetMessage() loop

L_DocShutDown(&hDoc);

8.

Add a menu resource to the project. If you are familiar with this process, you can skip to the next step.

 

a.

From the Insert menu, select Resource. Highlight Menu and click on NEW.

 

b.

The IDR_MENU will appear. Right click on IDR_MENU, select Properties and change the name to MAIN_MENU1.

 

c.

Right click on the menu entry (the outlined rectangle), and select Properties.

 

d.

In the Caption box enter User Mode. Make sure Pop-up is not selected and press Enter.

 

e.

Set the ID to IDM_FLIP_PAGE and the Caption to Flip Page. Make sure Checked has been selected. Press Enter. Save the resource.

9.

Add the following include statement with the include statements in EZFUNC.C in the EZOCRDOC directory:

#include "resource.h"

10.

In InitApplication, change the line:

wcWindowClass.lpszMenuName = NULL;

to:

wcWindowClass.lpszMenuName = TEXT("MAIN_MENU1");

11.

In InitInstance, change the menu parameter in CreateWindow from NULL to LoadMenu(hInstance, MAKEINTRESOURCE(MAIN_MENU1)).

12.

Under WM_CREATE in EZFUNC.C, add the following code after "L_LoadBitmap()" :

      nRet = L_DocAddPage(hDoc, &LeadBitmap, 0);
      if (nRet != SUCCESS)
      {
         wsprintf (achBuff, "Error %d adding page at index 0", nRet);
         MessageBox (NULL, achBuff, "Error", MB_OK);
         /* We have an error, so post WM_DESTROY */
         PostMessage (hWnd, WM_DESTROY, 0, 0);
         return (FALSE);
      }

      memset(&PageInfo, 0, sizeof(PAGEINFO));
      nRet = L_DocGetPageInfo (hDoc, 0, &PageInfo, sizeof(PAGEINFO));
      if (nRet == SUCCESS)
      {
         wsprintf (achBuff, TEXT("Page Width = %d\nPage Height = %d\nPage Bits Per Pixel = %d\n"),
                   PageInfo.nWidth,
                   PageInfo.nHeight,
                   PageInfo.nBitsPerPixel);

         MessageBox (NULL, achBuff, TEXT("Pages Info"), MB_OK);
      }

      LangId[0] = LANG_ID_ENGLISH;
      nRet = L_DocSelectLanguages(hDoc, LangId, 1);
      if (nRet != SUCCESS)
      {
         wsprintf (achBuff, TEXT("Error %d Setting English as the default language."), nRet);
         MessageBox (NULL, achBuff, TEXT("Error"), MB_OK);

         RemoveDocumentPages();
         /* We have an error, so post WM_DESTROY */
         PostMessage (hWnd, WM_DESTROY, 0, 0);
         return (FALSE);
      }

13.

Under WM_CREATE in EZFUNC.C, remove the following block:

      /* Get the current window size and client area */
      GetWindowRect(hWnd, &rWndSize);
      GetClientRect(hWnd,&rClientSize);
      /* Use this function to fit the displayed image in the client area. */
      CalcDisplay (  rClientSize.right,   /* Width allowed */
                     rClientSize.bottom,  /* Height allowed */
                     BITMAPWIDTH(&LeadBitmap),    /* Width factor, for aspect ratio */
                     BITMAPHEIGHT(&LeadBitmap),   /* Height factor, for aspect ratio */   
                     NULL,                /* Resulting left value, not used */
                     NULL,                /* Resulting top value, not used */
                     &DisplayWidth,       /* Resulting width value */
                    &DisplayHeight);     /* Resulting height value */
      /* Adjust the window size to remove blank space at the right or bottom */
      AdjustedWidth = rWndSize.right - rWndSize.left + DisplayWidth - rClientSize.right;
      AdjustedHeight = rWndSize.bottom - rWndSize.top + DisplayHeight - rClientSize.bottom;
      MoveWindow(hWnd, rWndSize.left, rWndSize.top, AdjustedWidth, AdjustedHeight, FALSE);
      /* Get the client area of the adjusted window */
      GetClientRect(hWnd,&rClientSize);
      /* Make the destination rectangle for painting the same as the client area */
      rLeadDest = rClientSize;
      /* Set the source rectangle to use the whole bitmap */
      SetRect(&rLeadSource, 0, 0, LeadBitmap.Width, LeadBitmap.Height);
      /* Force paint palette creation */
      SendMessage (hWnd, WM_QUERYNEWPALETTE, 0, 0L);

14.

Replace WM_PAINT in EZFUNC.C as follows:

 

a.

Remove the following block:

   case WM_PAINT:
      /* Get the handle to the device context */
      hdc = BeginPaint (hWnd, &ps); 
      if (LeadBitmap.Flags.Allocated)  /* Do we have an image? */
      {
         if (hpalPaint) /* If we have a paint palette, select it */
         {
            hPalette = SelectPalette (hdc, hpalPaint, TRUE);
            /* Uncomment this if you do not process WM_QUERYNEWPALETTE */
            /* RealizePalette (hdc); */
         }
         /* Paint the image */
         L_PaintDC(hdc,
                    &LeadBitmap,
                    &rLeadSource,   /* Source rectangle */
                    NULL,           /* Default source clip area */
                    &rLeadDest,     /* Destination rectangle */
                    &ps.rcPaint,    /* Dest clip set by WM_PAINT */
                   SRCCOPY);       /* Normal Paint */
         if (hpalPaint)         /* Return old palette */
            SelectPalette (hdc, hPalette, TRUE);
      }
      EndPaint (hWnd, &ps);     /* Return DC */
      return (0);

 

b.

Add the following block:

   case WM_PAINT:
      return 0;

15.

Add the following function in EZFUNC.C in EZOCR directory

void RemoveDocumentPages()
{
   L_INT nPageCount = 0;
   L_INT i, nRet;
   L_TCHAR szBuffer[1024];

   nRet = L_DocGetPageCount(hDoc, &nPageCount);
   if (nRet == SUCCESS)
   {
      for (i=0; i<nPageCount; i++)
      {
         L_DocCleanupPages(hDoc, TRUE);
         nRet = L_DocRemovePage(hDoc, i);
         if (nRet == SUCCESS)
         {
            wsprintf(szBuffer, TEXT("The page # %d was removed successfully"), i);
            MessageBox(NULL, szBuffer, TEXT("Notice!"), MB_OK);
         }
      }
   }
}

16.

Also, add the following statement before the MainWndProc function

void RemoveDocumentPages();

17.

In the MainWndProc procedure, add the following code to the switch statement:

   case WM_COMMAND: 
      switch(LOWORD(wParam)) 
      {
      case IDM_FLIP_PAGE: 
         {
            BITMAPHANDLE Bitmap;

            L_InitBitmap(&Bitmap, sizeof(BITMAPHANDLE), 0, 0, 0);
            nRet = L_DocExportPage(hDoc, &Bitmap, sizeof(BITMAPHANDLE), 0);
            {
               L_FlipBitmap(&Bitmap);
               L_DocLockPage(hDoc, 0);
               nRet = L_DocUpdatePage(hDoc, &Bitmap, 0);
               if (nRet == SUCCESS)
               {
                  wsprintf (achBuff, TEXT("The specified page was updated successfully."));
                  MessageBox (NULL, achBuff, TEXT("Notice!"), MB_OK);
               }
               else
               {
                  wsprintf (achBuff, TEXT("Error %d in updating the page bitmap for page # 0"), nRet);
                  MessageBox (NULL, achBuff, TEXT("Error"), MB_OK);
               }

               L_DocUnlockPage(hDoc, 0);
               L_DocSetActivePage(hDoc, 0);

               L_FreeBitmap(&Bitmap);
            }
         }
         break; 
      }

      return 0;

 

18.

On the Build menu, select Build Ezfunc32.exe.

19.

On the Build menu, select Execute Ezfunc32.exe.

20.

Save this project to use for testing other code samples.