LEADTOOLS For .NET Class Library Send comments on this topic. | Back to Introduction - All Topics | Help Version 17.0.3.28
OCR Tutorial - Working with Pages

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 Visual Studio 2005 or 2008

  2. Choose File->New->Project from the menu

  3. In the New Project dialog box, choose either "Visual C# Projects" or "Visual Basic Projects" in the Projects Type List, and choose "Windows Application" in Visual Studio 2005 or "Windows Forms Application" in Visual Studio 2008 from the Templates List

  4. Type the project name as "OcrTutorial" in the Project Name field, and then choose OK. If desired, type a new location for your project or select a directory using the Browse button, and then choose OK.

  5. In the "Solution Explorer" window, right-click on the "References" folder, and select "Add Reference..." from the context menu. In the "Add Reference" dialog box, select the ".NET" tab and browse to LEADTOOLS For .NET "<LEADTOOLS_INSTALLDIR>\Bin\DotNet\Win32" folder and select the following DLLs:

    • Leadtools.dll
    • Leadtools.Codecs.dll
    • Leadtools.Forms.dll
    • Leadtools.Forms.DocumentWriters.dll
    • Leadtools.Forms.Ocr.dll
    • Leadtools.Forms.Ocr.Plus.dll
    • Leadtools.Codecs.Bmp.dll
    • Leadtools.Codecs.Cmp.dll
    • Leadtools.Codecs.Tif.dll
    • Leadtools.Codecs.Fax.dll

    Note: The Leadtools.Codecs.*.dll references added are for the BMP, JPG, CMP, TIF and FAX image file formats. Add any additional file format codec DLL if required in your application.

  6. Switch to Form1 code view (Right-click Form1 in the solution explorer then select View Code) and add the following lines at the beginning of the file after any Importsor usingsection if there are any:

    [Visual Basic]

          
    
                Imports Leadtools
                Imports Leadtools.Forms
                Imports Leadtools.Forms.DocumentWriters
                Imports Leadtools.Forms.Ocr
                Imports Leadtools.ImageProcessing
                     
                
    
    
        
    

    [C#]

          
    
                using Leadtools;
                using Leadtools.Forms;
                using Leadtools.Forms.DocumentWriters;
                using Leadtools.Forms.Ocr;
                using Leadtools.ImageProcessing;
                     
                
    
    
        
    
  7. Add the following private variables to the Form1 class:

    [Visual Basic]

          
    
                Private _ocrEngine As IOcrEngine
                Private _ocrDocument As IOcrDocument
                     
                
    
    
        
    

    [C#]

          
    
                private IOcrEngine _ocrEngine;
                private IOcrDocument _ocrDocument;
                     
                
    
    
        
    
  8. Add the following code to the Form1 constructor (in Visual Basic, you can copy/paste the whole Sub New code from here):

    [Visual Basic]

          
    
                Sub New()
                   ' This call is required by the Windows Form Designer.
                   InitializeComponent()
                   ' Add any initialization after the InitializeComponent() call.
                   ' Unlock the OCR support
                   ' Please replace with your own key
                   RasterSupport.Unlock(RasterSupportType.OcrPlus, "Replace with your own key here")
                   ' Initialize the OCR engine
                   _ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Plus, False)
                End Sub
                     
                
    
    
        
    

    [C#]

          
    
                public Form1()
                {
                   InitializeComponent();
                   // Unlock the OCR support
                   // Please replace with your own key
                   RasterSupport.Unlock(RasterSupportType.OcrPlus, "Replace with your own key here");
                   // Initialize the OCR engine
                   _ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Plus, false);
                }
                     
                
    
    
        
    
  9. Drag and drop five buttons in Form1. Leave all the buttons names as the default "button1, button2 ...", then change the Text property of each button to the following:

    Button Text button1 Startup button2 Add Page button3 Remove Pages button4 Flip Page button5 Shutdown

  10. Add the following code for the button1 (Startup) control’s Clickhandler:

    [Visual Basic]

          
    
                Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
                   ' Start up the OCR engine
                   _ocrEngine.Startup(Nothing, Nothing, Nothing, Nothing)
                   ' Create the document
                    _ocrDocument = _ocrEngine.DocumentManager.CreateDocument()
                   MessageBox.Show("OCR engine has been started successfully")
                End Sub
                     
                
    
    
        
    

    [C#]

          
    
                private void button1_Click(object sender, EventArgs e)
                {
                   // Start up the OCR engine
                   _ocrEngine.Startup(null, null, null, null);
                   // Create the document
                    _ocrDocument = _ocrEngine.DocumentManager.CreateDocument();
                   MessageBox.Show("OCR engine has been started successfully");
                }
                     
                
    
    
        
    
  11. Add the following code for the button2 (Add Page) control’s Clickhandler:

    [Visual Basic]

          
    
                Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
                   ' Add the a page from a TIF file to the OCR document
                   Dim fileName As String = "C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.tif"
                   Dim page As IOcrPage = _ocrDocument.Pages.AddPage(fileName, Nothing)
                   Dim pageCount As Integer = _ocrDocument.Pages.Count
                   ' Show some information about this page
                   Dim message As String = String.Format( _
                      "Total pages is {0}\nLast page added size = {1} by {2}\nResolution = {3} by {4}\nBits per pixel = {5}", _
                      pageCount, _
                      page.Width, page.Height, _
                      page.DpiX, page.DpiY, _
                      page.BitsPerPixel)
                   MessageBox.Show(message)
                End Sub
                     
                
    
    
        
    

    [C#]

          
    
                private void button2_Click(object sender, EventArgs e)
                {
                   // Add the a page from a TIF file to the OCR document
                   string fileName = @"C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.tif";
                   IOcrPage page = _ocrDocument.Pages.AddPage(fileName, null);
                   int pageCount = _ocrDocument.Pages.Count;
                   // Show some information about this page
                   string message = string.Format(
                      "Total pages is {0}\nLast page added size = {1} by {2}\nResolution = {3} by {4}\nBits per pixel = {5}",
                      pageCount,
                      page.Width, page.Height,
                      page.DpiX, page.DpiY,
                      page.BitsPerPixel);
                   MessageBox.Show(message);
                }
                     
                
    
    
        
    
  12. Add the following code for the button3 (Remove Pages) control’s Clickhandler:

    [Visual Basic]

          
    
                Private Sub button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button3.Click
                   ' Remove all added pages from the OCR document.
                   _ocrDocument.Pages.Clear()
                   MessageBox.Show("All the pages has been removed")
                End Sub
                     
                
    
    
        
    

    [C#]

          
    
                private void button3_Click(object sender, EventArgs e)
                {
                   // Remove all added pages from the OCR document.
                   _ocrDocument.Pages.Clear();
                   MessageBox.Show("All the pages has been removed");
                }
                     
                
    
    
        
    
  13. Add the following code for the button4 (Flip Page) control’s Clickhandler:

    [Visual Basic]

          
    
                Private Sub button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button4.Click
                   ' Get the last page added to the OCR document
                   If (_ocrDocument.Pages.Count < 1) Then
                      Return
                   End If
                   Dim page As IOcrPage = _ocrDocument.Pages(_ocrDocument.Pages.Count - 1)
                   ' Get a RasterImage that represents this page
                   Using image As RasterImage = page.GetRasterImage()
                      ' Flip this image vertically
                      Dim cmd As New FlipCommand(False)
                      cmd.Run(image)
                      ' The RasterImage is a copy of the page, so update it
                      page.SetRasterImage(image)
                      MessageBox.Show("Last page has been flipped")
                   End Using
                End Sub
                     
                
    
    
        
    

    [C#]

          
    
                private void button4_Click(object sender, EventArgs e)
                {
                   // Get the last page added to the OCR document
                   if(_ocrDocument.Pages.Count < 1)
                      return;
                   IOcrPage page = _ocrDocument.Pages[_ocrDocument.Pages.Count - 1];
                   // Get a RasterImage that represents this page
                   using(RasterImage image = page.GetRasterImage())
                   {
                      // Flip this image vertically
                      FlipCommand cmd = new FlipCommand(false);
                      cmd.Run(image);
                      // The RasterImage is a copy of the page, so update it
                      page.SetRasterImage(image);
                      MessageBox.Show("Last page has been flipped");
                   }
                }
                     
                
    
    
        
    
  14. Add the following code for the button5 (Shutdown) control’s Clickhandler:

    [Visual Basic]

          
    
                Private Sub button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button5.Click
                   ' Destory the document
                   _ocrDocument.Dispose()
                   ' Shut down the OCR engine
                   _ocrEngine.Shutdown()
                   MessageBox.Show("OCR engine has been shut down")
                End Sub
                     
                
    
    
        
    

    [C#]

          
    
                private void button5_Click(object sender, EventArgs e)
                {
                   // Destory the document
                   _ocrDocument.Dispose();
                   // Shut down the OCR engine
                   _ocrEngine.Shutdown();
                   MessageBox.Show("OCR engine has been shut down");
                }
                     
                
    
    
        
    
  15. Build, and Run the program to test it.

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