LEADTOOLS OCR Send comments on this topic. | Back to Introduction - All Topics | Help Version 17.0.3.22
Programming with Leadtools .NET OCR

The LEADTOOLS OCR features provide methods for incorporating optical character recognition (OCR) technology into an application. OCR is used to process bitmap document images into text.

Once the LEADTOOLS .NET OCR toolkit is installed to the system, the user is ready to begin programming with LEADTOOLS OCR. Please note that the OCR features must be unlocked before the user can actually use the OCR properties, methods, and events. For more information on unlocking LEAD features, refer to Unlocking Special LEAD Features.

You can start using LEADTOOLS for .NET OCR in your application by adding a reference to the Leadtools.Forms.Ocr.dll and Leadtools.Forms.DocumentWriter.dll assemblies into your .NET application. These assemblies contain the various interfaces, classes, structures and delegates used to program with LEADTOOLS OCR.

Since the toolkit supports multiple engines, the actual code that interfaces with the engine is stored in a separate assembly that will be loaded dynamically once an instance of the IOcrEngine interface is created. Hence, you must make sure the engine assembly you are planning to use resides next to the Leadtools.Forms.Ocr.dll assembly. You can add the engine assembly as a reference to your project if desired to automatically detect dependencies, even though this is not required by LEADTOOLS.

LEADTOOLS provides methods to:

LEADTOOLS uses an OCR handle to interact with the OCR engine and the OCR document containing the list of pages. The OCR handle is a communication session between LEADTOOLS OCR and an OCR engine installed on the system. This OCR handle is an internal structure that contains all the necessary information for recognition, getting and setting information, and text verification.

The following is an outline of the general steps involved in recognizing one or more pages.

  1. Select the engine type you wish to use and create an instance of the IOcrEngine interface. For more information, refer to Creating an OCR Engine Instance.

  2. Startup the OCR Engine with the IOcrEngine.Startup method. For more information, refer to Starting and Shutting down the Engine.

  3. Establish an OCR document with one or more pages. For more information, refer to Working with OCR Pages.

  4. Establish zones on the page(s), either manually or automatically. (This is optional. A page can be recognized with or without zones.) For more information, refer to Working with OCR Zones.

  5. Optional. Set the active languages to be used by the OCR engine. (The default is English). For more information, refer to Working with OCR Languages.

  6. Optional. Set the spell checking language. (The default is English). For more information, refer to OCR Spell Language Dictionaries.

  7. Optional. Set any special recognition module options. This is required only if the page contains zones, created either automatically or manually. For more information, refer to Recognizing OCR Pages, An Overview of OCR Recognition Modules and Using OMR in LEADTOOLS .NET OCR.

  8. Recognize. For more information, refer to Recognizing OCR Pages.

  9. Save recognition results, if desired. The results can be saved to either a file or to memory. For more information, refer to Recognizing OCR Pages.

  10. Shut down the OCR engine when finished. For more information, refer to Starting and Shutting down the Engine.

Where steps 4, 5, 6 and 7 can pretty much be done in any order, as long as they are carried out after starting up the OCR engine and before recognizing a page.

For more information on the engine assemblies refer to OcrEngineType" and Files To Be Included With Your Application.

The following example shows how to perform the above steps in code:

Visual Basic

             ' Assuming you added "Imports Leadtools.Forms.Ocr" and "Imports Leadtools.Forms.DocumentWriter" at the beginning of this class
             ' *** Step 1: Select the engine type and create an instance of the IOcrEngine interface.
             ' We will use the LEADTOOLS OCR Plus engine and use it in the same process
             Dim ocrEngine As IOcrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Plus, False)
            
             ' *** Step 2: Startup the engine.
            
             ' Use the default parameters
             ocrEngine.Startup(Nothing, Nothing, Nothing, Nothing)
            
             ' *** Step 3: Create an OCR document with one or more pages.
            
             Dim ocrDocument As IOcrDocument = ocrEngine.DocumentManager.CreateDocument()
            
             ' Add all the pages of a multi-page TIF image to the document
             ocrDocument.Pages.AddPages("C:\Users\Public\Documents\LEADTOOLS Images\Ocr.tif", 1, -1, Nothing)
            
             ' *** Step 4: Establish zones on the page(s), either manually or automatically
            
             ' Automatic zoning
             ocrDocument.Pages.AutoZone(Nothing)
            
             ' *** Step 5: (Optional) Set the active languages to be used by the OCR engine
            
             ' Enable English and German languages
             ocrEngine.LanguageManager.EnableLanguages(New String() {"en", "de"})
            
             ' *** Step 6: (Optional) Set the spell checking language
             ' Enable the spell checking system and set English as the spell language
             ocrEngine.SpellCheckManager.Enabled = True
             ocrEngine.SpellCheckManager.SpellLanguage = "en"
            
             ' *** Step 7: (Optional) Set any special recognition module options
            
             ' Change the fill method for the first zone in the first page to be Omr
             Dim ocrZone As OcrZone = ocrDocument.Pages(0).Zones(0)
             ocrZone.FillMethod = OcrZoneFillMethod.Omr
             ocrDocument.Pages(0).Zones(0) = ocrZone
            
             ' *** Step 8: Recognize
            
             ocrDocument.Pages.Recognize(Nothing)
            
            ' *** Step 9: Save recognition results
            
            ' Save the results to a PDF file
             ocrDocument.Save("C:\Users\Public\Documents\LEADTOOLS Images\Document.pdf", DocumentFormat.Pdf, Nothing)
             ocrDocument.Dispose()
            
             ' *** Step 10: Shut down the OCR engine when finished
             ocrEngine.Shutdown()
             ocrEngine.Dispose()
             

C#

             // Assuming you added "using Leadtools.Codecs;", "using Leadtools.Forms.Ocr;" and "using Leadtools.Forms.DocumentWriters;" at the beginning of this class
             // *** Step 1: Select the engine type and create an instance of the IOcrEngine interface.
            
             // We will use the LEADTOOLS OCR Plus engine and use it in the same process
             IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Plus, false);
            
             // *** Step 2: Startup the engine.
            
             // Use the default parameters
             ocrEngine.Startup(null, null, null, null);
            
             // *** Step 3: Create an OCR document with one or more pages.
            
             IOcrDocument ocrDocument = ocrEngine.DocumentManager.CreateDocument();
             
             // Add all the pages of a multi-page TIF image to the document
             ocrDocument.Pages.AddPages(@"C:\Users\Public\Documents\LEADTOOLS Images\Ocr.tif", 1, -1, null);
            
             // *** Step 4: Establish zones on the page(s), either manually or automatically
            
             // Automatic zoning
             ocrDocument.Pages.AutoZone(null);
            
             // *** Step 5: (Optional) Set the active languages to be used by the OCR engine
            
             // Enable English and German languages
             ocrEngine.LanguageManager.EnableLanguages(new string[] { "en", "de" });
            
             // *** Step 6: (Optional) Set the spell checking language
            
             // Enable the spell checking system and set English as the spell language
             ocrEngine.SpellCheckManager.Enabled = true;
             ocrEngine.SpellCheckManager.SpellLanguage = "en";
            
             // *** Step 7: (Optional) Set any special recognition module options
            
             // Change the fill method for the first zone in the first page to be default
             OcrZone ocrZone = ocrDocument.Pages[0].Zones[0];
             ocrZone.FillMethod = OcrZoneFillMethod.Default;
             ocrDocument.Pages[0].Zones[0] = ocrZone;
            
             // *** Step 8: Recognize
            
             ocrDocument.Pages.Recognize(null);
            
             // *** Step 9: Save recognition results
            
             // Save the results to a PDF file
             ocrDocument.Save(@"C:\Users\Public\Documents\LEADTOOLS Images\Document.pdf", DocumentFormat.Pdf, null);
             ocrDocument.Dispose();
            
             // *** Step 10: Shut down the OCR engine when finished
             ocrEngine.Shutdown();
             ocrEngine.Dispose();
             

The following example shows the minimum necessary steps required to perform the same task, this time using the proper C# and VB.NET "using" keywords:

Visual Basic

             ' Create the engine instance
             Using ocrEngine As IOcrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Plus, False)
                ' Startup the engine
                ocrEngine.Startup(Nothing, Nothing, Nothing, Nothing)
            
                ' Create the document
                Using ocrDocument as IOcrDocument = ocrEngine.DocumentManager.CreateDocument()
            
                   ' Add all the pages of a multi-page TIF image to the document
                   ocrDocument.Pages.AddPages("C:\Users\Public\Documents\LEADTOOLS Images\Ocr.tif", 1, -1, Nothing)
            
                   ' Recognize the pages
                   ocrDocument.Pages.Recognize(Nothing)
            
                   'Save recognition results
                   ocrDocument.Save("C:\Users\Public\Documents\LEADTOOLS Images\Document.pdf", DocumentFormat.Pdf, Nothing)
                End Using
            
             ' The engine will automatically shuts down when Dispose is called
             End Using
             

C#

             // Create the engine instance
             using(IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Plus, false))
             {
                // Startup the engine
                ocrEngine.Startup(null, null, null, null);
            
                // Create the OCR document
                using(IOcrDocument ocrDocument = ocrEngine.DocumentManager.CreateDocument())
                {
            
                   // Add all the pages of a multi-page TIF image to the document
                   ocrDocument.Pages.AddPages(@"C:\Users\Public\Documents\LEADTOOLS Images\Ocr.tif", 1, -1, null);
            
                   // Recognize the pages
                   ocrDocument.Pages.Recognize(null);
            
                   // Save recognition results
                   ocrDocument.Save(@"C:\Users\Public\Documents\LEADTOOLS Images\Document.pdf", DocumentFormat.Pdf, null);
                }
            
                // The engine will automatically shuts down when Dispose is called
             }
             

Finally, the following example shows how to perform the same task above using the one shot "fire and forget" IOcrAutoRecognizeManager interface:

Visual Basic

             ' Create the engine instance
             Using ocrEngine As IOcrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Plus, False)
                ' Startup the engine
                ocrEngine.Startup(Nothing, Nothing, Nothing, Nothing)
            
                ' Convert the multi-page TIF image to a PDF document
                ocrEngine.AutoRecognizeManager.Run( _
                   "C:\Users\Public\Documents\LEADTOOLS Images\Ocr.tif", _
                   "C:\Users\Public\Documents\LEADTOOLS Images\Document.pdf", _
                   DocumentFormat.Pdf, _
                   Nothing, _
                   Nothing)
             End Using
             

C#

             // Create the engine instance
             using(IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Plus, false))
             {
                // Startup the engine
                ocrEngine.Startup(null, null, null, null);
            
                // Convert the multi-page TIF image to a PDF document
                ocrEngine.AutoRecognizeManager.Run(
                   @"C:\Users\Public\Documents\LEADTOOLS Images\Ocr.tif",
                   @"C:\Users\Public\Documents\LEADTOOLS Images\Document.pdf",
                   DocumentFormat.Pdf,
                   null,
                   null);
             }