// USE CASE: Single click field text extraction public static void Single_click_field_text_extraction(IEngine engine) { // We want to implement a feature allowing the user of our application to // match fields on the page by simply clicking on the text in the image. trace("Create a sample document..."); IDocument document = PrepareNewRecognizedDocument(engine); trace("Extract all background text regions on a page..."); IPage page = document.Pages[0]; ITextRegions textRegions = page.ExtractTextRegions(); traceBegin("Create a list of found text regions..."); for (int i = 0; i < textRegions.Count; i++) { ITextRegion textRegion = textRegions[i]; string text = textRegion.Text.Text; IRectangle r = textRegion.Region.BoundingRectangle; trace(string.Format("'{0}' - [{1},{2},{3},{4}]", text, r.Left, r.Top, r.Right, r.Bottom)); } traceEnd(""); // Now it is quite simple to implement the desired behavior. // Suppose the user 'sees' the word 'ENGLAND' and clicks on it. // In this sample we find the clicked region by text, in a real // application we would find it as containing the clicked point ITextRegion clickedRegion = null; for (int i = 0; i < textRegions.Count; i++) { ITextRegion textRegion = textRegions[i]; if (textRegion.Text.Text == "ENGLAND") { clickedRegion = textRegion; break; } } assert(clickedRegion != null); // In our implementation we can either use the prerecognized text for the region (which is // fast and usually quite accurate) or set the required field region to enclose the found text // region and rerecognize the field to apply field-specific recognition parameters: IField theField = document.Sections[0].Children[2]; assert(theField.Name == "DeliveryAddress"); IBlock theBlock = theField.Blocks[0]; IRegion newRegion = engine.CreateRegion(); IRectangle br = clickedRegion.Region.BoundingRectangle; newRegion.AddRect(br.Left, br.Top, br.Right, br.Bottom); theBlock.Region = newRegion; IBlocksCollection blocksToRerecognize = engine.CreateBlocksCollection(); blocksToRerecognize.Add(theBlock); document.RecognizeBlocks(blocksToRerecognize); assert(theField.Value.AsString == "ENGLAND"); }