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.
Start Visual Studio
Choose File->New->Project from the menu.
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.
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.
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:
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.
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 |
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:
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Forms;
using Leadtools.Forms.DocumentWriters;
using Leadtools.Forms.Ocr;
using Leadtools.ImageProcessing;
Imports Leadtools
Imports Leadtools.Codecs
Imports Leadtools.Forms
Imports Leadtools.Forms.DocumentWriters
Imports Leadtools.Forms.Ocr
Imports Leadtools.ImageProcessing
Add the following private variables to the Form1 class:
private IOcrEngine _ocrEngine;
private IOcrDocument _ocrDocument;
Private _ocrEngine As IOcrEngine
Private _ocrDocument As IOcrDocument
Add the following code for the button1 (Startup) Click
handler:
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");
}
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
Add the following code for the button2 (Add Page) Click
handler:
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);
}
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
Add the following code for the button3 (Remove Pages) Click
handler:
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");
}
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
Add the following code for the button4 (Flip Page) Click
handler:
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");
}
}
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
Add the following code for the button5 (Shutdown) Click
handler:
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")
}
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
Build, and Run the program to test it.
Save this project to use for testing other code samples.
OCR Tutorial - Recognizing Pages
OCR Tutorial - Adding and Painting Zones
OCR Tutorial - Working with Recognition Results
OCR Tutorial - Scanning to Searchable PDF
Getting Started (Guide to Example Programs)
Programming with LEADTOOLS .NET OCR
An Overview of OCR Recognition Modules
Creating an OCR Engine Instance
Starting and Shutting Down the OCR Engine
Multi-Threading with LEADTOOLS OCR
OCR Spell Language Dictionaries
Using OMR in LEADTOOLS .NET OCR