←Select platform

GetText Method

Summary
Gets the recognition OCR data for a zone in this IOcrPage as a string.
Syntax
C#
Objective-C
C++/CLI
Java
Python
public string GetText( 
   int zoneIndex 
) 
- (nullable NSString *)textForZoneAtIndex:(NSInteger)index error:(NSError **)error NS_SWIFT_NAME(zoneText(at:)); 
public String getText(int zoneIndex) 
String^ GetText(  
   int zoneIndex 
)  
def GetText(self,zoneIndex): 

Parameters

zoneIndex
0-based index of the zone. Pass -1 for this parameter to get the text for all page zones at once.

Return Value

A String containing the recognized characters found (or an empty string if zones on the page contains no recognition data).

Remarks

Use this method to get the document result in a simple String object. Getting the result as text is helpful in situations when adding zones manually for form processing. For example, suppose the form you are processing has two areas of interests, a name field at coordinates 100, 100, 400, 120 and a social security number at coordinates 100, 200, 400, 220. You can structure your application as follows:

  1. Create a new IOcrPage object from the form image using IOcrEngine.CreatePage.

  2. Add the name zone manually:

    OcrZone nameZone = new OcrZone(); 
    nameZone.ZoneType = OcrZoneType.Text; 
    nameZone.Bounds = new LeadRect(100, 100, 400, 120); 
    ocrPage.Zones.Add(nameZone); 
  3. Recognize the page (only this one zone will recognized):

    ocrPage.Recognize();
  4. Get the value of the name field:

    string name = ocrPage.GetText(0);
  5. Remove the name zone from the page:

    ocrPage.Zones.Clear();
  6. Repeat the steps from (2) above to get the social security field.

Example

This example gets the values of particular fields from a document.

C#
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Ocr; 
using Leadtools.Forms.Common; 
using Leadtools.WinForms; 
using Leadtools.Drawing; 
 
public void RecognizeTextExample() 
{ 
   // Get the form file name 
   Console.WriteLine("Setting up the form..."); 
   string formFileName = GetMyForm(); 
 
   // Assume we get the field information from an external source such as a database or an XML file 
   string[] fieldNames = 
   { 
      "Name", 
      "Address", 
      "SSN" 
   }; 
 
   LeadRect[] fieldBounds = 
   { 
      new LeadRect(800, 160, 1500, 220), 
      new LeadRect(800, 560, 1500, 220), 
      new LeadRect(800, 960, 1500, 220) 
   }; 
 
   int fieldCount = fieldNames.Length; 
   string[] fieldValues = new string[fieldCount]; 
 
   // Create an instance of the engine 
   using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD)) 
   { 
      // Start the engine using default parameters 
      Console.WriteLine("Starting up the engine..."); 
      ocrEngine.Startup(null, null, null, LEAD_VARS.OcrLEADRuntimeDir); 
 
      // Create a page from the image file 
      Console.WriteLine("Creating a page..."); 
      using (IOcrPage ocrPage = ocrEngine.CreatePage(ocrEngine.RasterCodecsInstance.Load(formFileName, 1), OcrImageSharingMode.AutoDispose)) 
      { 
         // Get our fields 
         for (int i = 0; i < fieldCount; i++) 
         { 
            // Clear all the zones in the page 
            ocrPage.Zones.Clear(); 
 
            // Add our field zone 
            OcrZone ocrZone = new OcrZone(); 
            ocrZone.ZoneType = OcrZoneType.Text; 
            ocrZone.Bounds = fieldBounds[i]; 
            ocrZone.Name = fieldNames[i]; // Optional 
 
            Console.WriteLine("Adding the zone for field {0} to the page...", ocrZone.Name); 
            ocrPage.Zones.Add(ocrZone); 
 
            // Recognize the page. This will only recognize the zone we added 
            Console.WriteLine("Recognizing the page..."); 
            ocrPage.Recognize(null); 
            fieldValues[i] = ocrPage.GetText(0); 
         } 
      } 
 
      // Shutdown the engine 
      // Note: calling Dispose will also automatically shutdown the engine if it has been started 
      Console.WriteLine("Shutting down..."); 
      ocrEngine.Shutdown(); 
   } 
 
   // We are done, show the fields 
   Console.WriteLine("-------------------------------------"); 
   Console.WriteLine("Done, values extracted from the form:"); 
   Console.WriteLine("-------------------------------------"); 
   for (int i = 0; i < fieldCount; i++) 
      Console.WriteLine("{0} : {1}", fieldNames[i], fieldValues[i]); 
} 
 
private string GetMyForm() 
{ 
   string formFileName = LEAD_VARS.ImagesDir + "MyForm.tif"; 
 
   // In this example we will create the form every time 
   // This will be a TIF file 11 by 8.5 inches in size containing a name, address and social security fields 
   using (RasterImage image = new RasterImage( 
      RasterMemoryFlags.Conventional, 
      2544, 
      3294, 
      24, 
      RasterByteOrder.Bgr, 
      RasterViewPerspective.BottomLeft, 
      null, 
      IntPtr.Zero, 
      0)) 
   { 
      image.XResolution = 300; 
      image.YResolution = 300; 
 
      // Draw our fields into this image 
      IntPtr hdc = RasterImagePainter.CreateLeadDC(image); 
      try 
      { 
         using (Graphics g = Graphics.FromHdc(hdc)) 
         { 
            g.FillRectangle(Brushes.White, 0, 0, image.ImageWidth - 1, image.ImageHeight - 1); 
 
            using (Font f = new Font("Times New Roman", 80, FontStyle.Regular)) 
            { 
               using (Pen p = new Pen(Color.Black, 4)) 
               { 
                  using (StringFormat sf = new StringFormat()) 
                  { 
                     sf.LineAlignment = StringAlignment.Center; 
                     // Draw the fields 
 
                     // Name 
                     g.DrawString("Name:", f, Brushes.Black, 200, 200); 
                     Rectangle rc = new Rectangle(800, 160, 1500, 220); 
                     g.DrawRectangle(p, rc); 
                     string value = "John Doe"; 
                     g.DrawString(value, f, Brushes.Black, rc, sf); 
 
                     // Address 
                     g.DrawString("Address:", f, Brushes.Black, 200, 600); 
                     rc = new Rectangle(800, 560, 1500, 220); 
                     g.DrawRectangle(p, rc); 
                     value = "1234 Main Street, USA"; 
                     g.DrawString(value, f, Brushes.Black, rc, sf); 
 
                     // Social security number 
                     g.DrawString("SSN:", f, Brushes.Black, 200, 1000); 
                     rc = new Rectangle(800, 960, 1500, 220); 
                     g.DrawRectangle(p, rc); 
                     value = "123-45-6789"; 
                     g.DrawString(value, f, Brushes.Black, rc, sf); 
                  } 
               } 
            } 
         } 
      } 
      finally 
      { 
         RasterImagePainter.DeleteLeadDC(hdc); 
      } 
 
      // Save this image to disk 
      using (RasterCodecs codecs = new RasterCodecs()) 
      { 
         codecs.Save(image, formFileName, RasterImageFormat.CcittGroup4, 1); 
      } 
   } 
 
   return formFileName; 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS22\Resources\Images"; 
   public const string OcrLEADRuntimeDir = @"C:\LEADTOOLS22\Bin\Common\OcrLEADRuntime"; 
} 
Requirements

Target Platforms

Help Version 22.0.2023.3.31
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Ocr Assembly

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.