Working with Pages Tutorial

To create and run a program that demonstrates how to add pages to an OCR document:

 

1.

Create a new directory in the \Examples\API directory called DocumentTutor

2.

Copy everything in the SimpleLoad directory into the DocumentTutor directory.

3.

Compile the project as it is and run SimpleLoad.exe to familiarize yourself with the basic program.

4.

In the Imports.cpp, add the following lines:

 

#if defined(WIN64)

   …

   …

   #pragma comment(lib, "..\\..\\..\\Lib\\API\\x64\\Ltdoc_x.lib")

#else

   …

   …

   #pragma comment(lib, "..\\..\\..\\Lib\\API\\Win32\\Ltdoc_u.lib")

#endif // #if defined(WIN64)

 

5.

Add the following line in StdAfx.h in the DocumentTutor directory:

 

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

 

6.

Define the following global variables in Ezfunc.h in the DocumentTutor directory:

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

 

Also, add the following defines:

 

#define IDM_FLIP_PAGE        200

 

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, NULL) != SUCCESS)
      return 0;

 

b.

Add the following after the GetMessage() loop

L_DocShutDown(&hDoc);

 

 

8.

Edit EZFUNC.RC file in the DocumentTutor directory and add the following lines:

 

#include "EZFUNC.H"

 

MAIN_MENU   MENU

BEGIN

   MENUITEM "Flip Page"   IDM_FLIP_PAGE

END

 

9.

In InitApplication, change the line:

wcWindowClass.lpszMenuName = NULL;

to:

wcWindowClass.lpszMenuName = TEXT("MAIN_MENU");

 

10.

In MainWndProc, change the line:

 

Demos_CombinePath(szImagesDirectory, L_TEXT("Image1.cmp"), szFilename, _countof(szFilename));

 

To:

 

Demos_CombinePath(szImagesDirectory, L_TEXT("ocr1.tif"), szFilename, _countof(szFilename));

 

11.

In InitInstance, change the menu parameter in CreateWindow from NULL to

LoadMenu(hInstance, MAKEINTRESOURCE("MAIN_MENU"))

12.

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

      UNREFERENCED_PARAMETER(ps);

      UNREFERENCED_PARAMETER(AdjustedWidth);

      UNREFERENCED_PARAMETER(AdjustedHeight);

 

      nRet = L_DocAddPage(hDoc, &LeadBitmap, 0);
      if (nRet != SUCCESS)
      {
         wsprintf (achBuff, TEXT("Error %d adding page at index 0"), nRet);
         MessageBox (NULL, achBuff, TEXT("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.CPP, remove the following block:

      /* Get the current window size and client area */
      GetWindowRect(hWnd, &rWndSize);
      GetClientRect(hWnd,&rClientSize);
      /* 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 spaces 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 entire 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.CPP 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.CPP in DocumentTutor 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 SimpleLoad.exe.

19.

On the Build menu, select Execute SimpleLoad.exe.

20.

Save this project to use it for testing other code samples.