Gets the recognition OCR data for a zone in this IOcrPage as a string.
string GetText(int zoneIndex)
Function GetText( _ByVal zoneIndex As Integer _) As String
string GetText(int zoneIndex)
- (nullable NSString *)textForZoneAtIndex:(NSInteger)index error:(NSError **)error public String getText(int zoneIndex) function Leadtools.Forms.Ocr.IOcrPage.GetText(zoneIndex)
String^ GetText(int zoneIndex)
zoneIndex
0-based index of the zone. Pass -1 for this parameter to get the text for all page zones at once.
A String containing the recognized characters found (or an empty string if zones on the page contains no recognition data).
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:
Create a new IOcrPage object from the form image using IOcrEngine.CreatePage.
Add the name zone manually:
OcrZone nameZone = new OcrZone();nameZone.ZoneType = OcrZoneType.Text;nameZone.Bounds = new LogicalRectangle(100, 100, 400, 120);ocrPage.Zones.Add(nameZone);
Recognize the page (only this one zone will recognized):
ocrPage.Recognize();Get the value of the name field:
string name = ocrPage.GetText(0);Remove the name zone from the page:
ocrPage.Zones.Clear();Repeat the steps from (2) above to get the social security field.
This example gets the values of particular fields from a document.
using Leadtools;using Leadtools.Codecs;using Leadtools.Forms.Ocr;using Leadtools.Forms;using Leadtools.WinForms;using Leadtools.Drawing;public void RecognizeTextExample(){// Get the form file nameConsole.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 filestring[] fieldNames ={"Name","Address","SSN"};LogicalRectangle[] fieldBounds ={new LogicalRectangle(800, 160, 1500, 220, LogicalUnit.Pixel),new LogicalRectangle(800, 560, 1500, 220, LogicalUnit.Pixel),new LogicalRectangle(800, 960, 1500, 220, LogicalUnit.Pixel)};int fieldCount = fieldNames.Length;string[] fieldValues = new string[fieldCount];// Create an instance of the engineusing (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, false)){// Start the engine using default parametersConsole.WriteLine("Starting up the engine...");ocrEngine.Startup(null, null, null, LEAD_VARS.OcrAdvantageRuntimeDir);// Create a page from the image fileConsole.WriteLine("Creating a page...");using (IOcrPage ocrPage = ocrEngine.CreatePage(ocrEngine.RasterCodecsInstance.Load(formFileName, 1), OcrImageSharingMode.AutoDispose)){// Get our fieldsfor (int i = 0; i < fieldCount; i++){// Clear all the zones in the pageocrPage.Zones.Clear();// Add our field zoneOcrZone ocrZone = new OcrZone();ocrZone.ZoneType = OcrZoneType.Text;ocrZone.Bounds = fieldBounds[i];ocrZone.Name = fieldNames[i]; // OptionalConsole.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 addedConsole.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 startedConsole.WriteLine("Shutting down...");ocrEngine.Shutdown();}// We are done, show the fieldsConsole.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 fieldsusing (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 imageIntPtr 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// Nameg.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);// Addressg.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 numberg.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 diskusing (RasterCodecs codecs = new RasterCodecs()){codecs.Save(image, formFileName, RasterImageFormat.CcittGroup4, 1);}}return formFileName;}static class LEAD_VARS{public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";public const string OcrAdvantageRuntimeDir = @"C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime";}
Imports LeadtoolsImports Leadtools.CodecsImports Leadtools.Forms.OcrImports Leadtools.FormsImports Leadtools.WinFormsImports Leadtools.Drawing<TestMethod>Public Sub RecognizeTextExample()' Get the form file nameConsole.WriteLine("Setting up the form...")Dim formFileName As String = GetMyForm()' Assume we get the field informations from an external source such as a database or an XML fileDim fieldNames As String() = {"Name", "Address", "SSN"}Dim fieldBounds As LogicalRectangle() = {New LogicalRectangle(800, 160, 1500, 220, LogicalUnit.Pixel),New LogicalRectangle(800, 560, 1500, 220, LogicalUnit.Pixel),New LogicalRectangle(800, 960, 1500, 220, LogicalUnit.Pixel)}Dim fieldCount As Integer = fieldNames.LengthDim fieldValues As String() = New String(fieldCount - 1) {}' Create an instance of the engineUsing ocrEngine As IOcrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, False)' Start the engine using default parametersConsole.WriteLine("Starting up the engine...")ocrEngine.Startup(Nothing, Nothing, Nothing, LEAD_VARS.OcrAdvantageRuntimeDir)' Create a page from the image fileConsole.WriteLine("Creating a page...")Using ocrPage As IOcrPage = ocrEngine.CreatePage(ocrEngine.RasterCodecsInstance.Load(formFileName, 1), OcrImageSharingMode.AutoDispose)' Get our fieldsFor i As Integer = 0 To fieldCount - 1' Clear all the zones in the pageocrPage.Zones.Clear()' Add our field zoneDim ocrZone As New OcrZone()ocrZone.ZoneType = OcrZoneType.TextocrZone.Bounds = fieldBounds(i)ocrZone.Name = fieldNames(i)' OptionalConsole.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 addedConsole.WriteLine("Recognizing the page...")ocrPage.Recognize(Nothing)fieldValues(i) = ocrPage.GetText(0)NextEnd Using' Shutdown the engine' Note: calling Dispose will also automatically shutdown the engine if it has been startedConsole.WriteLine("Shutting down...")ocrEngine.Shutdown()End Using' We are done, show the fieldsConsole.WriteLine("-------------------------------------")Console.WriteLine("Done, values extracted from the form:")Console.WriteLine("-------------------------------------")For i As Integer = 0 To fieldCount - 1Console.WriteLine("{0} : {1}", fieldNames(i), fieldValues(i))NextEnd SubPrivate Function GetMyForm() As StringDim formFileName As String = 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 fieldsUsing image As New RasterImage(RasterMemoryFlags.Conventional,2544, 3294, 24,RasterByteOrder.Bgr,RasterViewPerspective.BottomLeft,Nothing, IntPtr.Zero, 0)image.XResolution = 300image.YResolution = 300' Draw our fields into this imageDim hdc As IntPtr = RasterImagePainter.CreateLeadDC(image)TryUsing g As Graphics = Graphics.FromHdc(hdc)g.FillRectangle(Brushes.White, 0, 0, image.ImageWidth - 1, image.ImageHeight - 1)Using f As New Font("Times New Roman", 80, FontStyle.Regular)Using p As New Pen(Color.Black, 4)Using sf As New StringFormat()sf.LineAlignment = StringAlignment.Center' Draw the fields' Nameg.DrawString("Name:", f, Brushes.Black, 200, 200)Dim rc As New Rectangle(800, 160, 1500, 220)g.DrawRectangle(p, rc)Dim value As String = "John Doe"g.DrawString(value, f, Brushes.Black, rc, sf)' Addressg.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 numberg.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)End UsingEnd UsingEnd UsingEnd UsingFinallyRasterImagePainter.DeleteLeadDC(hdc)End Try' Save this image to diskUsing codecs As New RasterCodecs()codecs.Save(image, formFileName, RasterImageFormat.CcittGroup4, 1)End UsingEnd UsingReturn formFileNameEnd FunctionPublic NotInheritable Class LEAD_VARSPublic Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"Public Const OcrAdvantageRuntimeDir As String = "C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime"End Class
GetRecognizedCharacters Method
SetRecognizedCharacters Method
|
Products |
Support |
Feedback: GetText Method - Leadtools.Forms.Ocr |
Introduction |
Help Version 19.0.2017.6.6
|

Raster .NET | C API | C++ Class Library | JavaScript HTML5
Document .NET | C API | C++ Class Library | JavaScript HTML5
Medical .NET | C API | C++ Class Library | JavaScript HTML5
Medical Web Viewer .NET
Your email has been sent to support! Someone should be in touch! If your matter is urgent please come back into chat.
Chat Hours:
Monday - Friday, 8:30am to 6pm ET
Thank you for your feedback!
Please fill out the form again to start a new chat.
All agents are currently offline.
Chat Hours:
Monday - Friday
8:30AM - 6PM EST
To contact us please fill out this form and we will contact you via email.