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);
        }
Ejemplo n.º 2
0
        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.DefaultValueType);
                switch (field.DefaultValueType)
                {
                case Template.Field.ValueTypes.PdfText:
                case Template.Field.ValueTypes.PdfTextLines:
                case Template.Field.ValueTypes.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 <Pdf.Line>   lines     = Pdf.GetLines(Pdf.GetCharBoxsSurroundedByRectangle(pages[currentPageI].PdfCharBoxs, tableAR), pages.ActiveTemplate.TextAutoInsertSpace).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 <Pdf.Line>   lines     = Pdf.GetLines(Pdf.GetCharBoxsSurroundedByRectangle(pages[currentPageI].PdfCharBoxs, r), pages.ActiveTemplate.TextAutoInsertSpace).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
                    });
                    if (field.DefaultValueType == Template.Field.ValueTypes.PdfText)
                    {
                        return(Page.NormalizeText((string)v));
                    }
                    if (field.DefaultValueType == Template.Field.ValueTypes.PdfTextLines)
                    {
                        return(Page.NormalizeText(string.Join("\r\n", (List <string>)v)));
                    }
                    //if (field.DefaultValueType == Template.Field.ValueTypes.PdfTextCharBoxs)
                    return(Page.NormalizeText(Serialization.Json.Serialize(v)));

                case Template.Field.ValueTypes.OcrText:
                case Template.Field.ValueTypes.OcrTextLines:
                case Template.Field.ValueTypes.OcrCharBoxs:
                    drawBoxes(Settings.Appearance.SelectionBoxColor, Settings.Appearance.SelectionBoxBorderWidth, new List <RectangleF> {
                        r
                    });
                    if (field.DefaultValueType == Template.Field.ValueTypes.OcrText)
                    {
                        return(Page.NormalizeText((string)v));
                    }
                    if (field.DefaultValueType == Template.Field.ValueTypes.OcrTextLines)
                    {
                        return(Page.NormalizeText(string.Join("\r\n", (List <string>)v)));
                    }
                    //if (field.DefaultValueType == Template.Field.ValueTypes.OcrTextCharBoxs)
                    return(Page.NormalizeText(Serialization.Json.Serialize(v)));

                case Template.Field.ValueTypes.Image:
                    drawBoxes(Settings.Appearance.SelectionBoxColor, Settings.Appearance.SelectionBoxBorderWidth, new List <RectangleF> {
                        r
                    });
                    return(v);

                default:
                    throw new Exception("Unknown option: " + field.DefaultValueType);
                }
            }
            catch (Exception ex)
            {
                //Win.LogMessage.Error("Rectangle", ex);
                Win.LogMessage.Error(ex);
            }
            return(null);
        }
        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].GetNonCachedFieldActualInfo(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.DefaultValueType);
                switch (field.DefaultValueType)
                {
                case Template.Field.ValueTypes.PdfText:
                case Template.Field.ValueTypes.PdfTextLines:
                case Template.Field.ValueTypes.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
                        });
                    }
                    drawBoxes(Settings.Appearance.SelectionBoxColor, Settings.Appearance.SelectionBoxBorderWidth, new List <RectangleF> {
                        r
                    });
                    if (field.DefaultValueType == Template.Field.ValueTypes.PdfText)
                    {
                        return(Page.NormalizeText((string)v));
                    }
                    if (field.DefaultValueType == Template.Field.ValueTypes.PdfTextLines)
                    {
                        return(Page.NormalizeText(string.Join("\r\n", (List <string>)v)));
                    }
                    //if (field.DefaultValueType == Template.Field.ValueTypes.PdfTextCharBoxs)
                    return(Page.NormalizeText(Serialization.Json.Serialize(v)));

                case Template.Field.ValueTypes.OcrText:
                case Template.Field.ValueTypes.OcrTextLines:
                case Template.Field.ValueTypes.OcrCharBoxs:
                    drawBoxes(Settings.Appearance.SelectionBoxColor, Settings.Appearance.SelectionBoxBorderWidth, new List <RectangleF> {
                        r
                    });
                    if (field.DefaultValueType == Template.Field.ValueTypes.OcrText)
                    {
                        return(Page.NormalizeText((string)v));
                    }
                    if (field.DefaultValueType == Template.Field.ValueTypes.OcrTextLines)
                    {
                        return(Page.NormalizeText(string.Join("\r\n", (List <string>)v)));
                    }
                    //if (field.DefaultValueType == Template.Field.ValueTypes.OcrTextCharBoxs)
                    return(Page.NormalizeText(Serialization.Json.Serialize(v)));

                case Template.Field.ValueTypes.Image:
                    drawBoxes(Settings.Appearance.SelectionBoxColor, Settings.Appearance.SelectionBoxBorderWidth, new List <RectangleF> {
                        r
                    });
                    return(v);

                default:
                    throw new Exception("Unknown option: " + field.DefaultValueType);
                }
            }
            catch (Exception ex)
            {
                //Win.LogMessage.Error("Rectangle", ex);
                Win.LogMessage.Error(ex);
            }
            return(null);
        }