List <Ocr.CharBox> getOcrCharBoxs() { if (ActualRectangle == null) { return(null); } RectangleF ar = (RectangleF)ActualRectangle; Template.Field.Ocr aof = ActualField as Template.Field.Ocr; if (ActualField.ColumnOfTable == null) { if (aof?.SingleFieldFromFieldImage ?? page.PageCollection.ActiveTemplate.SingleFieldFromFieldImage) { return(Ocr.This.GetCharBoxsSurroundedByRectangle(page.ActiveTemplateBitmap, ar, aof?.TesseractPageSegMode ?? page.PageCollection.ActiveTemplate.TesseractPageSegMode)); } else { return(Ocr.GetCharBoxsSurroundedByRectangle(page.ActiveTemplateOcrCharBoxs, ar)); } } if (!TableFieldActualInfo.Found) { return(null); } if (aof?.ColumnCellFromCellImage ?? page.PageCollection.ActiveTemplate.ColumnCellFromCellImage) { float x = ar.X > TableFieldActualInfo.ActualRectangle.Value.X ? ar.X : TableFieldActualInfo.ActualRectangle.Value.X; float y = ar.Top > TableFieldActualInfo.ActualRectangle.Value.Top ? ar.Top : TableFieldActualInfo.ActualRectangle.Value.Top; RectangleF r = new RectangleF( x, y, (ar.Right < TableFieldActualInfo.ActualRectangle.Value.Right ? ar.Right : TableFieldActualInfo.ActualRectangle.Value.Right) - x, (ar.Bottom < TableFieldActualInfo.ActualRectangle.Value.Bottom ? ar.Bottom : TableFieldActualInfo.ActualRectangle.Value.Bottom) - y ); return(Ocr.This.GetCharBoxsSurroundedByRectangle(page.ActiveTemplateBitmap, ar, aof?.TesseractPageSegMode ?? page.PageCollection.ActiveTemplate.TesseractPageSegMode)); } else { List <Ocr.CharBox> cbs = (List <Ocr.CharBox>)TableFieldActualInfo.GetValue(Template.Field.Types.OcrCharBoxs); return(cbs.Where(a => /*!check: ar.Contains(a.R)*/ a.R.Left >= ar.Left && a.R.Right <= ar.Right && a.R.Top >= ar.Top && a.R.Bottom <= ar.Bottom).ToList()); } }
object extractFieldAndDrawSelectionBox(Template.Field field) { try { if (pages == null) { return(null); } if (field.Rectangle == null) { return(null); } pages.ActiveTemplate = GetTemplateFromUI(false); if (field.LeftAnchor != null && !findAndDrawAnchor(field.LeftAnchor.Id)) { return(null); } if (field.TopAnchor != null && !findAndDrawAnchor(field.TopAnchor.Id)) { return(null); } if (field.RightAnchor != null && !findAndDrawAnchor(field.RightAnchor.Id)) { return(null); } if (field.BottomAnchor != null && !findAndDrawAnchor(field.BottomAnchor.Id)) { return(null); } Page.FieldActualInfo fai = pages[currentPageI].GetFieldActualInfo(field); if (!fai.Found) { return(null); } RectangleF r = (RectangleF)fai.ActualRectangle; owners2resizebleBox[field] = new ResizebleBox(field, r, Settings.Appearance.SelectionBoxBorderWidth); object v = fai.GetValue(field.Type); switch (field.Type) { case Template.Field.Types.PdfText: case Template.Field.Types.PdfTextLines: case Template.Field.Types.PdfCharBoxs: if (field.ColumnOfTable != null) { if (!fai.TableFieldActualInfo.Found) { return(null); } drawBoxes(Settings.Appearance.TableBoxColor, Settings.Appearance.TableBoxBorderWidth, new List <RectangleF> { (RectangleF)fai.TableFieldActualInfo.ActualRectangle }); if (ShowFieldTextLineSeparators.Checked) { RectangleF tableAR = (RectangleF)fai.TableFieldActualInfo.ActualRectangle; List <Page.Line <Pdf.CharBox> > lines = Page.GetLines(Pdf.GetCharBoxsSurroundedByRectangle(pages[currentPageI].PdfCharBoxs, tableAR), null, null).ToList(); List <RectangleF> lineBoxes = new List <RectangleF>(); for (int i = 1; i < lines.Count; i++) { if (lines[i].Bottom <tableAR.Top || lines[i].Top> tableAR.Bottom || lines[i].Bottom <r.Top || lines[i].Top> r.Bottom ) { continue; } lineBoxes.Add(new RectangleF { X = r.X, Y = lines[i].Top, Width = r.Width, Height = 0 }); } drawBoxes(Settings.Appearance.SelectionBoxColor, Settings.Appearance.TextLineSeparatorWidth, lineBoxes); } } else { if (ShowFieldTextLineSeparators.Checked) { List <Page.Line <Pdf.CharBox> > lines = Page.GetLines(Pdf.GetCharBoxsSurroundedByRectangle(pages[currentPageI].PdfCharBoxs, r), null, null).ToList(); List <RectangleF> lineBoxes = new List <RectangleF>(); for (int i = 1; i < lines.Count; i++) { lineBoxes.Add(new RectangleF { X = r.X, Y = lines[i].Top, Width = r.Width, Height = 0 }); } drawBoxes(Settings.Appearance.SelectionBoxColor, Settings.Appearance.TextLineSeparatorWidth, lineBoxes); } } drawBoxes(Settings.Appearance.SelectionBoxColor, Settings.Appearance.SelectionBoxBorderWidth, new List <RectangleF> { r }); return(v); case Template.Field.Types.OcrText: case Template.Field.Types.OcrTextLines: case Template.Field.Types.OcrCharBoxs: Template.Field.Ocr of = field as Template.Field.Ocr; if (field.ColumnOfTable != null) { if (!fai.TableFieldActualInfo.Found) { return(null); } drawBoxes(Settings.Appearance.TableBoxColor, Settings.Appearance.TableBoxBorderWidth, new List <RectangleF> { (RectangleF)fai.TableFieldActualInfo.ActualRectangle }); if (ShowFieldTextLineSeparators.Checked) { List <Page.Line <Ocr.CharBox> > ols = Page.GetLines((List <Ocr.CharBox>)fai.TableFieldActualInfo.GetValue(Template.Field.Types.OcrCharBoxs), null, field.CharFilter ?? pages.ActiveTemplate.CharFilter); if (of.AdjustLineBorders ?? pages.ActiveTemplate.AdjustLineBorders) { Page.AdjustBorders(ols, fai.TableFieldActualInfo.ActualRectangle.Value); } else { Page.PadLines(ols, field.LinePaddingY ?? pages.ActiveTemplate.LinePaddingY); } if (ols.Count > 0) { ols.RemoveAt(0); } List <RectangleF> lineBoxes = new List <RectangleF>(); foreach (Page.Line <Ocr.CharBox> l in ols) { lineBoxes.Add(new RectangleF { X = r.X, Y = l.Top, Width = r.Width, Height = 0 }); } drawBoxes(Settings.Appearance.SelectionBoxColor, Settings.Appearance.TextLineSeparatorWidth, lineBoxes); } } else { if (ShowFieldTextLineSeparators.Checked) { List <Ocr.CharBox> cbs; if (of.SingleFieldFromFieldImage ?? pages.ActiveTemplate.SingleFieldFromFieldImage) { cbs = Ocr.This.GetCharBoxsSurroundedByRectangle(pages[currentPageI].ActiveTemplateBitmap, r, of.TesseractPageSegMode ?? pages.ActiveTemplate.TesseractPageSegMode); } else { cbs = Ocr.GetCharBoxsSurroundedByRectangle(pages[currentPageI].ActiveTemplateOcrCharBoxs, r); } if (cbs != null) { List <Page.Line <Ocr.CharBox> > ols = Page.GetLines(cbs, null, field.CharFilter ?? pages.ActiveTemplate.CharFilter); if (of.AdjustLineBorders ?? pages.ActiveTemplate.AdjustLineBorders) { Page.AdjustBorders(ols, r); } else { Page.PadLines(ols, field.LinePaddingY ?? pages.ActiveTemplate.LinePaddingY); } if (ols.Count > 0) { ols.RemoveAt(0); } List <RectangleF> lineBoxes = new List <RectangleF>(); foreach (Page.Line <Ocr.CharBox> l in ols) { lineBoxes.Add(new RectangleF { X = r.X, Y = l.Top, Width = r.Width, Height = 0 }); } drawBoxes(Settings.Appearance.SelectionBoxColor, Settings.Appearance.TextLineSeparatorWidth, lineBoxes); } } } drawBoxes(Settings.Appearance.SelectionBoxColor, Settings.Appearance.SelectionBoxBorderWidth, new List <RectangleF> { r }); return(v); case Template.Field.Types.Image: drawBoxes(Settings.Appearance.SelectionBoxColor, Settings.Appearance.SelectionBoxBorderWidth, new List <RectangleF> { r }); return(v); case Template.Field.Types.OcrTextLineImages: drawBoxes(Settings.Appearance.SelectionBoxColor, Settings.Appearance.SelectionBoxBorderWidth, new List <RectangleF> { r }); return(v); default: throw new Exception("Unknown option: " + field.Type); } } catch (Exception ex) { Log.Error(ex); Message.Error(ex, this); } return(null); }
List <Bitmap> getOcrTextLineImages() { if (ActualRectangle == null) { return(null); } RectangleF ar = (RectangleF)ActualRectangle; Template.Field.Ocr aof = ActualField as Template.Field.Ocr; List <Line <Ocr.CharBox> > ols; float left, width; if (ActualField.ColumnOfTable == null) { if (aof?.SingleFieldFromFieldImage ?? page.PageCollection.ActiveTemplate.SingleFieldFromFieldImage) { List <Ocr.CharBox> cs = Ocr.This.GetCharBoxsSurroundedByRectangle(page.ActiveTemplateBitmap, ar, aof?.TesseractPageSegMode ?? page.PageCollection.ActiveTemplate.TesseractPageSegMode); if (cs == null) { return(null); } ols = GetLines(cs, null, ActualField.CharFilter ?? page.PageCollection.ActiveTemplate.CharFilter); } else { ols = Ocr.GetLinesSurroundedByRectangle(page.ActiveTemplateOcrCharBoxs, ar, null, ActualField.CharFilter ?? page.PageCollection.ActiveTemplate.CharFilter); } if (aof?.AdjustLineBorders ?? page.PageCollection.ActiveTemplate.AdjustLineBorders) { AdjustBorders(ols, ar); } else { PadLines(ols, ActualField.LinePaddingY ?? page.PageCollection.ActiveTemplate.LinePaddingY); } left = ar.Left; width = ar.Width; } else { if (!TableFieldActualInfo.Found) { return(null); } List <Ocr.CharBox> cbs = (List <Ocr.CharBox>)TableFieldActualInfo.GetValue(Template.Field.Types.OcrCharBoxs); if (aof?.ColumnCellFromCellImage ?? page.PageCollection.ActiveTemplate.ColumnCellFromCellImage) { ols = GetLines(cbs, null, ActualField.CharFilter ?? page.PageCollection.ActiveTemplate.CharFilter); } else { ols = GetLines(cbs, null, ActualField.CharFilter ?? page.PageCollection.ActiveTemplate.CharFilter); } if (aof?.AdjustLineBorders ?? page.PageCollection.ActiveTemplate.AdjustLineBorders) { AdjustBorders(ols, TableFieldActualInfo.ActualRectangle.Value); } else { PadLines(ols, ActualField.LinePaddingY ?? page.PageCollection.ActiveTemplate.LinePaddingY); } left = ar.X > TableFieldActualInfo.ActualRectangle.Value.X ? ar.X : TableFieldActualInfo.ActualRectangle.Value.X; width = (ar.Right < TableFieldActualInfo.ActualRectangle.Value.Right ? ar.Right : TableFieldActualInfo.ActualRectangle.Value.Right) - left; } List <Bitmap> ls = new List <Bitmap>(); foreach (Line <Ocr.CharBox> l in ols) { RectangleF r = new RectangleF(left, l.Top, width, l.Bottom - l.Top); using (Bitmap b = page.GetRectangleFromActiveTemplateBitmap(r.X / Settings.Constants.Pdf2ImageResolutionRatio, r.Y / Settings.Constants.Pdf2ImageResolutionRatio, r.Width / Settings.Constants.Pdf2ImageResolutionRatio, r.Height / Settings.Constants.Pdf2ImageResolutionRatio)) { ls.Add(b == null ? b : GetImageScaled2Pdf(b)); } } return(ls); }
List <string> getOcrTextLines() { if (ActualRectangle == null) { return(null); } RectangleF ar = (RectangleF)ActualRectangle; Template.Field.Ocr aof = ActualField as Template.Field.Ocr; TextAutoInsertSpace textAutoInsertSpace = aof?.TextAutoInsertSpace != null ? aof?.TextAutoInsertSpace : page.PageCollection.ActiveTemplate.TextAutoInsertSpace; if (ActualField.ColumnOfTable == null) { if (aof?.SingleFieldFromFieldImage ?? page.PageCollection.ActiveTemplate.SingleFieldFromFieldImage) { List <Ocr.CharBox> cs = Ocr.This.GetCharBoxsSurroundedByRectangle(page.ActiveTemplateBitmap, ar, aof?.TesseractPageSegMode ?? page.PageCollection.ActiveTemplate.TesseractPageSegMode); if (cs == null) { return(null); } return(GetTextLines(cs, textAutoInsertSpace, ActualField.CharFilter ?? page.PageCollection.ActiveTemplate.CharFilter)); } else { return(Ocr.GetTextLinesSurroundedByRectangle(page.ActiveTemplateOcrCharBoxs, ar, textAutoInsertSpace, ActualField.CharFilter ?? page.PageCollection.ActiveTemplate.CharFilter)); } } if (!TableFieldActualInfo.Found) { return(null); } List <Ocr.CharBox> cbs = (List <Ocr.CharBox>)TableFieldActualInfo.GetValue(Template.Field.Types.OcrCharBoxs); List <string> ls = new List <string>(); if (aof?.ColumnCellFromCellImage ?? page.PageCollection.ActiveTemplate.ColumnCellFromCellImage) { List <Line <Ocr.CharBox> > ols = GetLines(cbs, null, ActualField.CharFilter ?? page.PageCollection.ActiveTemplate.CharFilter); if (aof?.AdjustLineBorders ?? page.PageCollection.ActiveTemplate.AdjustLineBorders) { AdjustBorders(ols, TableFieldActualInfo.ActualRectangle.Value); } else { PadLines(ols, ActualField.LinePaddingY ?? page.PageCollection.ActiveTemplate.LinePaddingY); } foreach (Line <Ocr.CharBox> l in ols) { float x = ar.X > TableFieldActualInfo.ActualRectangle.Value.X ? ar.X : TableFieldActualInfo.ActualRectangle.Value.X; RectangleF r = new RectangleF( x, l.Top, (ar.Right < TableFieldActualInfo.ActualRectangle.Value.Right ? ar.Right : TableFieldActualInfo.ActualRectangle.Value.Right) - x, l.Bottom - l.Top ); List <Ocr.CharBox> cs = Ocr.This.GetCharBoxsSurroundedByRectangle(page.ActiveTemplateBitmap, r, aof?.TesseractPageSegMode ?? page.PageCollection.ActiveTemplate.TesseractPageSegMode); ls.Add(cs != null ? string.Join("", GetTextLines(cs, textAutoInsertSpace, ActualField.CharFilter ?? page.PageCollection.ActiveTemplate.CharFilter)) : ""); } } else { foreach (Line <Ocr.CharBox> l in GetLines(cbs, textAutoInsertSpace, ActualField.CharFilter ?? page.PageCollection.ActiveTemplate.CharFilter)) { StringBuilder sb = new StringBuilder(); foreach (Ocr.CharBox cb in l.CharBoxs) { if (cb.R.Left >= ar.Left && cb.R.Right <= ar.Right && cb.R.Top >= ar.Top && cb.R.Bottom <= ar.Bottom) { sb.Append(cb.Char); } } ls.Add(sb.ToString()); } } return(ls); }