Send comments on this topic. | Back to Introduction - All Topics | Help Version 15.12.21
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 .NET.
  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 the Templates List.
  4. 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 .
  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 "\LEAD Technologies\LEADTOOLS 15\Bin\DotNet\Win32 " folder and select the following DLLs:
    • Leadtools.dll
    • Leadtools.Codecs.dll
    • Leadtools.Document.dll
    • Leadtools.WinForms.dll
    Click the Select button and then press the OK button to add the above DLLs to the 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:

    [Visual Basic]

     
    Imports Leadtools
    Imports Leadtools.Codecs
    Imports Leadtools.WinForms
    Imports Leadtools.Document
    Imports Leadtools.ImageProcessing
    
    [C#]
     
    using Leadtools;
    using Leadtools.Codecs;
    using Leadtools.WinForms;
    using Leadtools.Document;
    using Leadtools.ImageProcessing;
    
  7. Add the following private variables to the Form1 class:

    [Visual Basic]

    
    Private WithEvents rasterDocument As RasterDocumentEngine
    Private WithEvents rasterDocumentZoneData As rasterDocumentZoneData
    
    [C#]
    
    private RasterDocumentEngine rasterDocument;
    private RasterDocumentZoneData rasterDocumentZoneData;
    
  8. Add an event handler to the Form1 Load event and add the following code:

    [Visual Basic]

    
    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
    
    [C#]
    
    private void Form1_Load(object sender, EventArgs e)
    {
       // Initialize a new RasterDocument object
       rasterDocument = RasterDocumentEngine.Instance;
    
    }
    
  9. Drag and drop six buttons in Form1. Leave all the buttons names as the default "button1, button2 ...", then change the following properties of button1:
    PropertyValue
    Text StartUp
  10. Change the following properties of button2:
    PropertyValue
    Text Add Page
  11. Change the following properties of button3:
    PropertyValue
    Text Remove Pages
  12. Change the following properties of button4:
    PropertyValue
    Text Shutdown
  13. Change the following properties of button5:
    PropertyValue
    Text Flip Page
  14. Change the following properties of button6:
    PropertyValue
    Text Set Language
  15. Add the following code to the Form1 class:

    [Visual Basic]

    
    Private Sub rasterDocument_Verify(ByVal sender As Object, ByVal e As Leadtools.Document.RasterDocumentVerifyEventArgs) Handles rasterDocument.Verify
       e.VerifyCode = RasterDocumentVerifyCode.Sure
    End Sub
    
    [C#]
    
    void rasterDocument_Verify(object sender, RasterDocumentVerifyEventArgs e)
    {
       e.VerifyCode = RasterDocumentVerifyCode.Sure;
    }
    
  16. Add the following code for the button1 control’s click procedure:

    [Visual Basic]

    
    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
    
    [C#]
    
    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");
    }
    
  17. Add the following code for the button2 control’s click procedure:

    [Visual Basic]

     
    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
    
    [C#]
    
    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");
          }
       }
    }
    
  18. Add the following code for the button3 control’s click procedure:

    [Visual Basic]

    
    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
    
    [C#]
     
    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");
    }
    
  19. Add the following code for the button4 control’s click procedure:

    [Visual Basic]

    
    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
    
    [C#]
    
    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");
    }
    
  20. Add the following code for the button5 control’s click procedure:

    [Visual Basic]

    
    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
    
    [C#]
    
    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;
       }
    }
    
  21. Add the following code for the button6 control’s click procedure:

    [Visual Basic]

    
    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
    
    [C#]
    
    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");
    }
    
  22. Build, and Run the program to test it.
  23. Save this project to use for testing other code samples.