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 .NET. Choose File->New->Project… from the menu. 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 the Templates List. Type the project name as "OcrTutorials" 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 "\LEAD Technologies\LEADTOOLS 15\Bin\DotNet\Win32 " folder and select the following DLLs: Leadtools.dll Leadtools.Codecs.dll Leadtools.Document.dll Leadtools.WinForms.dll
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: [Visual Basic]
[C#]Imports Leadtools Imports Leadtools.Codecs Imports Leadtools.WinForms Imports Leadtools.Document Imports Leadtools.ImageProcessing
using Leadtools; using Leadtools.Codecs; using Leadtools.WinForms; using Leadtools.Document; using Leadtools.ImageProcessing;
Add the following private variables to the Form1 class: [Visual Basic]
[C#]Private WithEvents rasterDocument As RasterDocumentEngine Private WithEvents rasterDocumentZoneData As rasterDocumentZoneData
private RasterDocumentEngine rasterDocument; private RasterDocumentZoneData rasterDocumentZoneData;
Add an event handler to the Form1 Load event and add the following code: [Visual Basic]
[C#]Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Initialize a new RasterDocument object rasterDocument = RasterDocumentEngine.Instance End Sub
private void Form1_Load(object sender, EventArgs e) { // Initialize a new RasterDocument object rasterDocument = RasterDocumentEngine.Instance; }
Drag and drop six buttons in Form1. Leave all the buttons names as the default "button1, button2 ...", then change the following properties of button1: Property Value Text StartUp Change the following properties of button2: Property Value Text Add Page Change the following properties of button3: Property Value Text Remove Pages Change the following properties of button4: Property Value Text Shutdown Change the following properties of button5: Property Value Text Flip Page Change the following properties of button6: Property Value Text Set Language Add the following code to the Form1 class: [Visual Basic]
[C#]Private Sub rasterDocument_Verify(ByVal sender As Object, ByVal e As Leadtools.Document.RasterDocumentVerifyEventArgs) Handles rasterDocument.Verify e.VerifyCode = RasterDocumentVerifyCode.Sure End Sub
void rasterDocument_Verify(object sender, RasterDocumentVerifyEventArgs e) { e.VerifyCode = RasterDocumentVerifyCode.Sure; }
Add the following code for the button1 control’s click procedure: [Visual Basic]
[C#]Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click ' Unlock Support for Ocr ' Note that this is a sample key, which will not work in your toolkit RasterSupport.Unlock(RasterSupportType.Ocr, "TestKey") ' Start up the Ocr engine rasterDocument.Startup() MsgBox("Ocr Object has been started successfully") End Sub
private void button1_Click(object sender, System.EventArgs e) { // Unlock Support for Ocr // Note that this is a sample key, which will not work in your toolkit RasterSupport.Unlock(RasterSupportType.Ocr, "TestKey"); // Start up the Ocr engine rasterDocument.Startup(); MessageBox.Show("Ocr Object has been started successfully"); }
Add the following code for the button2 control’s click procedure: [Visual Basic]
[C#]Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click RasterCodecs.Startup() 'Initialize a new RasterCodecs object Dim codecs As RasterCodecs = New RasterCodecs 'Load a temporary image Dim rasterImage As RasterImage = codecs.Load("C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\ocr1.tif") Try 'Some information about the new added image Dim msg As String msg = String.Format("Page Width = {0}{1}Page Height = {2}{3}Page Bits Per Pixel = {4}", _ rasterImage.Width, _ rasterImage.Height, _ rasterImage.BitsPerPixel) MessageBox.Show(msg) Try 'Add the temporary image as a page to the RasterDocumentEngine object rasterDocument.AddPage(RasterImage, -1) Catch ex As Exception MessageBox.Show("Error: " + ex.Message + ". The engine could not add a new page to the document") End Try Finally RasterImage.Dispose() End Try End Sub
private void button2_Click(object sender, System.EventArgs e) { RasterCodecs.Startup(); // Initialize a new RasterCodecs object RasterCodecs codecs = new RasterCodecs(); // Load a temporary image using (RasterImage rasterImage = codecs.Load(@"C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\ocr1.tif")) { // Some information about the new added image string msg; msg = String.Format("Page Width = {0}\nPage Height = {1}\nPage Bits Per Pixel = {2}", rasterImage.Width, rasterImage.Height, rasterImage.BitsPerPixel); MessageBox.Show(msg); try { // Add the temporary image as a page to the RasterDocument object rasterDocument.AddPage(rasterImage, -1); } catch(Exception ex) { MessageBox.Show("Error: " + ex.Message + ". The engine could not add a new page to the document"); } } }
Add the following code for the button3 control’s click procedure: [Visual Basic]
[C#]Private Sub button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button3.Click ' Get the number of pages of the RasterDocument object Dim pageCount As Integer Dim i As Integer pageCount = rasterDocument.PageCount ' Loop over all the RasterDocument object pages to remove them For i = 0 To pageCount - 1 rasterDocument.RemovePage(0) Next i MessageBox.Show("All the pages has been removed") End Sub
private void button3_Click(object sender, System.EventArgs e) { // Get the number of pages of the RasterDocument object int pageCount = rasterDocument.PageCount; // Loop over all the RasterDocument object pages to remove them for (int i = 0; i < pageCount; i++) rasterDocument.RemovePage(0); MessageBox.Show("All the pages has been removed"); }
Add the following code for the button4 control’s click procedure: [Visual Basic]
[C#]Private Sub button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button4.Click ' Shut down the RasterDocument engine rasterDocument.Shutdown() ' This is used in subsequent tutorials rasterDocumentZoneData = Nothing MessageBox.Show("Ocr object has been shuted down") End Sub
private void button4_Click(object sender, System.EventArgs e) { // Shut down the RasterDocument engine rasterDocument.Shutdown(); // This is used in subsequent tutorials rasterDocumentZoneData = null; MessageBox.Show("Ocr object has been shuted down"); }
Add the following code for the button5 control’s click procedure: [Visual Basic]
[C#]Private Sub button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button5.Click ' Create a temporary image and load it with the First page of the RasterDocument object Dim rasterImage As RasterImage = rasterDocument.ExportPage(0) Try 'Initialize a new FlipCommand object Dim flipCommand As FlipCommand = New FlipCommand(True) 'Flip the exported image flipCommand.Run(rasterImage) Try ' Reput the image in the RasterDocument Object rasterDocument.LockPage(0, True) rasterDocument.UpdatePage(rasterImage, 0) rasterDocument.LockPage(0, False) MessageBox.Show("The specified page is updated successfully") Catch ex As Exception MessageBox.Show("Error: " + ex.Message + " The specified page is updated successfully") End Try rasterDocument.ActivePage = 0 Finally rasterImage.Dispose() End Try End Sub
private void button5_Click(object sender, System.EventArgs e) { // Create a temporary image and load it with the First page of the RasterDocument object using (RasterImage rasterImage = rasterDocument.ExportPage(0)) { // Initialize a new FlipCommand object FlipCommand flipCommand = new FlipCommand(true); // Flip the exported image flipCommand.Run(rasterImage); try { //Reput the image in the RasterDocument Object rasterDocument.LockPage(0, true); rasterDocument.UpdatePage(rasterImage, 0); rasterDocument.LockPage(0, false); MessageBox.Show("The specified page is updated successfully"); } catch(Exception ex) { MessageBox.Show("Error: " + ex.Message + " The specified page is updated successfully"); } rasterDocument.ActivePage = 0; } }
Add the following code for the button6 control’s click procedure: [Visual Basic]
[C#]Private Sub button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button6.Click ' Create An array of one language Dim language As RasterDocumentLanguage() = {RasterDocumentLanguage.English} ' Set the english as the active language rasterDocument.SelectLanguages(language) MessageBox.Show("English is set as the active language") End Sub
private void button6_Click(object sender, System.EventArgs e) { // Create An array of one language RasterDocumentLanguage[] language = {RasterDocumentLanguage.English}; // Set the english as the active language rasterDocument.SelectLanguages(language); MessageBox.Show("English is set as the active language"); }
Build, and Run the program to test it. Save this project to use for testing other code samples.