internal static IDocumentDefinition createDocDifinitionFromXML(string docDifPath, IEngine engine) { IDocumentDefinition documentDefinition = engine.CreateDocumentDefinition(); XDocument xDoc = XDocument.Load(docDifPath); var infoGeneral = from x in xDoc.Descendants("_Document_Definition") select new { templateImageName = x.Descendants("_TemplateImageName").First().Value, defaultLanguage = xDoc.Descendants("_DefaultLanguage").First().Value, exportDestinationTypeEnum = xDoc.Descendants("_ExportDestinationTypeEnum").First().Value }; foreach (var i in infoGeneral) { documentDefinition.Sections.AddNew("Section").Pages.AddNew(Config.parentDirectory + i.templateImageName); documentDefinition.DefaultLanguage = engine.PredefinedLanguages.FindLanguage(i.defaultLanguage); documentDefinition.ExportParams = engine.CreateExportParams((ExportDestinationTypeEnum)Enum.Parse(typeof(ExportDestinationTypeEnum), i.exportDestinationTypeEnum)); } var infoRegions = from x in xDoc.Descendants("_Region") select new { Name = x.Descendants("_Name").First().Value, X1 = x.Descendants("_X1").First().Value, Y1 = x.Descendants("_Y1").First().Value, X2 = x.Descendants("_X2").First().Value, Y2 = x.Descendants("_Y2").First().Value, BlockTypeEnum = x.Descendants("_BlockTypeEnum").First().Value }; List <Region> regions = new List <Region>(); foreach (var i in infoRegions) { Region region = new Region(); region.name = i.Name; region.x1 = Convert.ToInt32(i.X1); region.y1 = Convert.ToInt32(i.Y1); region.x2 = Convert.ToInt32(i.X2); region.y2 = Convert.ToInt32(i.Y2); region.blockTypeEnumElem = (BlockTypeEnum)Enum.Parse(typeof(BlockTypeEnum), i.BlockTypeEnum); regions.Add(region); } for (int i = 0; i < regions.Count; i++) { IRegion currentRegion = engine.CreateRegion(); currentRegion.AddRect(regions[i].x1, regions[i].y1, regions[i].x2, regions[i].y2); documentDefinition.Pages[0].Blocks.AddNew(regions[i].blockTypeEnumElem, currentRegion, regions[i].name); } return(documentDefinition); }
// 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"); }