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

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

  3. In the New Project dialog box, choose either "Visual C# Projects" or "VB Projects" in the Projects Type List, and choose "Windows Application" or "Windows Forms Application" depending on your Visual Studio Version 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\DotNet4\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.Advantage.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 formats. Add any additional file format codec DLL if required in your application.

  6. 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
  7. 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 using or Imports section if there are any:

    C#
          using Leadtools; 
          using Leadtools.Codecs; 
          using Leadtools.Forms; 
          using Leadtools.Forms.DocumentWriters; 
          using Leadtools.Forms.Ocr; 
          using Leadtools.ImageProcessing; 
         

    VB
          Imports Leadtools 
          Imports Leadtools.Codecs 
          Imports Leadtools.Forms 
          Imports Leadtools.Forms.DocumentWriters 
          Imports Leadtools.Forms.Ocr 
          Imports Leadtools.ImageProcessing 
         

  8. Add the following private variables to the Form1 class:

    C#
          private IOcrEngine _ocrEngine; 
          private IOcrDocument _ocrDocument; 
         

    VB
          Private _ocrEngine As IOcrEngine 
          Private _ocrDocument As IOcrDocument 
         

  9. Add the following code for the button1 (Startup) Click handler:

    C#
          private void button1_Click(object sender, EventArgs e) 
          { 
             // Initialize the OCR engine 
             _ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, false); 
             // Start it up 
             _ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime"); 
             // Create the document 
              _ocrDocument = _ocrEngine.DocumentManager.CreateDocument(); 
             MessageBox.Show("OCR engine has been started successfully and document created"); 
          } 
         

    VB
          Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click 
             ' Initialize the OCR engine 
             _ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, False) 
             ' Start it up 
             _ocrEngine.Startup(Nothing, Nothing, Nothing, "C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime") 
             ' Create the document 
              _ocrDocument = _ocrEngine.DocumentManager.CreateDocument() 
             MessageBox.Show("OCR engine has been started successfully and document created") 
          End Sub 
         

  10. Add the following code for the button2 (Add Page) Click handler:

    C#
          private void button2_Click(object sender, EventArgs e) 
          { 
             // Browse to an existing document file on disk, for example @"C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.tif"; 
             string fileName = null; 
             using (OpenFileDialog dlg = new OpenFileDialog()) 
             { 
                if (dlg.ShowDialog(this) == DialogResult.OK) 
                   fileName = dlg.FileName; 
             } 
                     
             if (fileName == null) 
                return; 
                     
             // Add the first page from the file to the OCR document 
             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); 
          } 
         

    VB
          Private Sub button2_Click(sender As Object, e As EventArgs) 
             ' Browse to an existing document file on disk, for example @"C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.tif"; 
             Dim fileName As String = Nothing 
             Using dlg As New OpenFileDialog() 
                If dlg.ShowDialog(Me) = DialogResult.OK Then 
                   fileName = dlg.FileName 
                End If 
             End Using 
                     
             If IsNothing(fileName) Then Return 
                     
             ' Add the first page from the file to the OCR document 
             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 
         

  11. Add the following code for the button3 (Remove Pages) Click handler:

    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"); 
          } 
         

    VB
          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 
         

  12. Add the following code for the button4 (Flip Page) Click handler:

    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"); 
             } 
          } 
         

    VB
          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 
         

  13. Add the following code for the button5 (Shutdown) Click handler:

    C#
          private void button5_Click(object sender, EventArgs e) 
          { 
             // Destroy the document 
             _ocrDocument.Dispose(); 
             // Destroy the engine (will also shut it down) 
             _ocrEngine.Dispose(); 
             MessageBox.Show("OCR engine and document has been destroyed") 
          } 
         

    VB
          Private Sub button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button5.Click 
             ' Destroy the document 
             _ocrDocument.Dispose() 
             ' Destroy the engine (will also shut it down) 
             _ocrEngine.Dispose() 
             MessageBox.Show("OCR engine and document has been destroyed") 
          End Sub 
         

  14. Build, and Run the program to test it.

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

Help Version 19.0.2017.10.27
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
LEADTOOLS Imaging, Medical, and Document