public override void Initialize(DataGridViewRow row, Action <DataGridViewRow> onLeft)
        {
            base.Initialize(row, onLeft);

            _object = (Template.Anchor.PdfText)row.Tag;
            if (_object == null)
            {
                _object = new Template.Anchor.PdfText();
            }
            StringBuilder sb = new StringBuilder();

            foreach (var l in Pdf.GetLines(_object.CharBoxs.Select(x => new Pdf.CharBox {
                Char = x.Char, R = x.Rectangle.GetSystemRectangleF()
            }), textAutoInsertSpace))
            {
                foreach (var cb in l.CharBoxs)
                {
                    sb.Append(cb.Char);
                }
                sb.Append("\r\n");
            }
            text.Text = sb.ToString();

            PositionDeviationIsAbsolute.Checked = _object.PositionDeviationIsAbsolute;
            try
            {
                PositionDeviation.Value = (decimal)_object.PositionDeviation;
            }
            catch { }

            SearchRectangleMargin.Value    = _object.SearchRectangleMargin;
            SearchRectangleMargin.Enabled  = cSearchRectangleMargin.Checked;
            cSearchRectangleMargin.Checked = SearchRectangleMargin.Value >= 0;
        }
            List <string> getTextLinesAsTableColumn()
            {
                if (ActualRectangle == null)
                {
                    return(null);
                }
                if (!TableFieldActualInfo.Found)
                {
                    return(null);
                }
                RectangleF         ar  = (RectangleF)ActualRectangle;
                List <Pdf.CharBox> cbs = (List <Pdf.CharBox>)TableFieldActualInfo.GetValue(Template.Field.ValueTypes.PdfCharBoxs);
                List <string>      ls  = new List <string>();

                foreach (Pdf.Line l in Pdf.GetLines(cbs, page.pageCollection.ActiveTemplate.TextAutoInsertSpace))
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (Pdf.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);
            }
            List <string> getPdfTextLines()
            {
                if (ActualRectangle == null)
                {
                    return(null);
                }
                RectangleF          ar = (RectangleF)ActualRectangle;
                TextAutoInsertSpace textAutoInsertSpace = ActualField.TextAutoInsertSpace != null ? ActualField.TextAutoInsertSpace : page.PageCollection.ActiveTemplate.TextAutoInsertSpace;

                if (ActualField.ColumnOfTable == null)
                {
                    return(Pdf.GetTextLinesSurroundedByRectangle(page.PdfCharBoxs, ar, textAutoInsertSpace));
                }

                List <string>      ls  = new List <string>();
                List <Pdf.CharBox> cbs = (List <Pdf.CharBox>)TableFieldActualInfo.GetValue(Template.Field.Types.PdfCharBoxs);

                foreach (Line <Pdf.CharBox> l in GetLines(cbs, textAutoInsertSpace, null))
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (Pdf.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);
            }
        private void ShowPdfText_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (pages == null)
            {
                return;
            }
            //TextForm tf = new TextForm("Pdf Entity Text", PdfTextExtractor.GetTextFromPage(pages.PdfReader, currentPageI), false);
            TextForm tf = new TextForm("Pdf Entity Text", Pdf.GetText(pages[currentPageI].PdfCharBoxs, new TextAutoInsertSpace {
                Threshold = (float)textAutoInsertSpaceThreshold.Value, Representative = Regex.Unescape(textAutoInsertSpaceRepresentative.Text)
            }), false);

            tf.ShowDialog();
        }
            object getValue(Template.Field.ValueTypes valueType)
            {
                RectangleF r = (RectangleF)ActualRectangle;

                switch (valueType)
                {
                case Template.Field.ValueTypes.PdfText:
                case Template.Field.ValueTypes.PdfTextLines:
                    List <string> ls;
                    if (ActualField.ColumnOfTable == null)
                    {
                        ls = Pdf.GetTextLinesSurroundedByRectangle(page.PdfCharBoxs, r, page.pageCollection.ActiveTemplate.TextAutoInsertSpace);
                    }
                    else
                    {
                        ls = getTextLinesAsTableColumn();
                    }
                    if (valueType == Template.Field.ValueTypes.PdfText)
                    {
                        return(string.Join("\r\n", ls));
                    }
                    return(ls);

                case Template.Field.ValueTypes.PdfCharBoxs:
                    return(Pdf.GetCharBoxsSurroundedByRectangle(page.PdfCharBoxs, r));

                case Template.Field.ValueTypes.OcrText:
                    return(Ocr.This.GetTextSurroundedByRectangle(page.ActiveTemplateBitmap, r));

                case Template.Field.ValueTypes.OcrTextLines:
                    throw new Exception("To be implemented.");

                case Template.Field.ValueTypes.OcrCharBoxs:
                    return(Ocr.GetCharBoxsSurroundedByRectangle(page.ActiveTemplateOcrCharBoxs, r));

                case Template.Field.ValueTypes.Image:
                    using (Bitmap b = page.GetRectangleFromActiveTemplateBitmap(r.X / Settings.Constants.Image2PdfResolutionRatio, r.Y / Settings.Constants.Image2PdfResolutionRatio, r.Width / Settings.Constants.Image2PdfResolutionRatio, r.Height / Settings.Constants.Image2PdfResolutionRatio))
                    {
                        return(Page.GetScaledImage2Pdf(b));
                    }

                default:
                    throw new Exception("Unknown option: " + valueType);
                }
            }
            List <Pdf.CharBox> getPdfCharBoxs()
            {
                if (ActualRectangle == null)
                {
                    return(null);
                }
                RectangleF ar = (RectangleF)ActualRectangle;

                if (ActualField.ColumnOfTable == null)
                {
                    return(Pdf.GetCharBoxsSurroundedByRectangle(page.PdfCharBoxs, ar));
                }

                if (!TableFieldActualInfo.Found)
                {
                    return(null);
                }
                List <Pdf.CharBox> cbs = (List <Pdf.CharBox>)TableFieldActualInfo.GetValue(Template.Field.Types.PdfCharBoxs);

                return(cbs.Where(a => a.R.Left >= ar.Left && a.R.Right <= ar.Right && a.R.Top >= ar.Top && a.R.Bottom <= ar.Bottom).ToList());
            }
示例#7
0
        internal List <string> GetTextLinesAsTableColumn(Template.Field field, RectangleF fieldActualR)
        {//can be optimized by caching!
            int fieldDefinitionIndex = pageCollection.ActiveTemplate.Fields.Where(x => x.Name == field.Name).TakeWhile(x => x != field).Count();

            Template.Field tableField;
            try
            {
                tableField = pageCollection.ActiveTemplate.Fields.Where(x => x.Name == field.ColumnOfTable).ElementAt(fieldDefinitionIndex);
            }
            catch (Exception e)
            {
                throw new Exception("Field " + field.ColumnOfTable + " does not have enough definitions to respect definition " + field.Name + "[" + fieldDefinitionIndex + "]", e);
            }
            List <Pdf.CharBox> cbs = (List <Pdf.CharBox>)getValue(tableField, ValueTypes.CharBoxs);

            if (cbs == null)
            {
                return(null);
            }

            List <string> ls = new List <string>();

            foreach (Pdf.Line l in Pdf.GetLines(cbs, pageCollection.ActiveTemplate.TextAutoInsertSpace))
            {
                StringBuilder sb = new StringBuilder();
                foreach (Pdf.CharBox cb in l.CharBoxs)
                {
                    if (cb.R.Left >= fieldActualR.Left && cb.R.Right <= fieldActualR.Right && cb.R.Top >= fieldActualR.Top && cb.R.Bottom <= fieldActualR.Bottom)
                    {
                        sb.Append(cb.Char);
                    }
                }
                ls.Add(sb.ToString());
            }
            return(ls);
        }
示例#8
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].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);
        }
        public TemplateForm(TemplateManager templateManager)
        {
            InitializeComponent();

            Icon = Win.AssemblyRoutines.GetAppIcon();
            Text = Program.FullName + " - Template Editor";

            templateManager.TemplateForm = this;
            this.templateManager         = templateManager;

            this.bitmapPreparationForm = new ScanTemplateForm(this);

            initializeAnchorsTable();
            initializeConditionsTable();
            initializeFieldsTable();

            TesseractPageSegMode.DataSource = Enum.GetValues(typeof(Tesseract.PageSegMode));

            picture.MouseDown += delegate(object sender, MouseEventArgs e)
            {
                if (pages == null)
                {
                    return;
                }

                Point p = new Point((int)(e.X / (float)pictureScale.Value), (int)(e.Y / (float)pictureScale.Value));

                ResizebleBox rb = findResizebleBox(p, out ResizebleBoxSides resizebleBoxSide);
                if (rb != null)
                {
                    drawingMode        = resizebleBoxSide == ResizebleBoxSides.Left || resizebleBoxSide == ResizebleBoxSides.Right ? DrawingModes.resizingSelectionBoxV : DrawingModes.resizingSelectionBoxH;
                    Cursor.Current     = drawingMode == DrawingModes.resizingSelectionBoxV ? Cursors.VSplit : Cursors.HSplit;
                    selectionBoxPoint0 = rb.R.Location;
                    selectionBoxPoint1 = rb.R.Location;
                    selectionBoxPoint2 = new Point(rb.R.Right, rb.R.Bottom);
                }
                else
                {
                    if (ModifierKeys.HasFlag(Keys.Shift))
                    {
                        drawingMode          = DrawingModes.movingImage;
                        Cursor.Current       = Cursors.SizeAll;
                        screenMousePosition0 = Control.MousePosition;
                        imageScrollPostion0  = new Point(splitContainer1.Panel2.HorizontalScroll.Value, splitContainer1.Panel2.VerticalScroll.Value);//to avoid jerking
                    }
                    else
                    {
                        drawingMode        = DrawingModes.drawingSelectionBox;
                        selectionBoxPoint0 = p;
                        selectionBoxPoint1 = p;
                        selectionBoxPoint2 = p;
                    }
                }
                showSelectionCoordinates(selectionBoxPoint1);
            };

            picture.MouseWheel += delegate(object sender, MouseEventArgs e)
            {
                if (pages == null)
                {
                    return;
                }
            };

            picture.MouseMove += delegate(object sender, MouseEventArgs e)
            {
                if (pages == null)
                {
                    return;
                }

                Point p;

                if (drawingMode == DrawingModes.movingImage)
                {
                    p = Control.MousePosition;
                    int h = imageScrollPostion0.X + screenMousePosition0.X - p.X;
                    if (h < splitContainer1.Panel2.HorizontalScroll.Minimum)
                    {
                        h = splitContainer1.Panel2.HorizontalScroll.Minimum;
                    }
                    else if (h > splitContainer1.Panel2.HorizontalScroll.Maximum)
                    {
                        h = splitContainer1.Panel2.HorizontalScroll.Maximum;
                    }
                    splitContainer1.Panel2.HorizontalScroll.Value = h;
                    int v = imageScrollPostion0.Y + screenMousePosition0.Y - p.Y;
                    if (v < splitContainer1.Panel2.VerticalScroll.Minimum)
                    {
                        v = splitContainer1.Panel2.VerticalScroll.Minimum;
                    }
                    else if (v > splitContainer1.Panel2.VerticalScroll.Maximum)
                    {
                        v = splitContainer1.Panel2.VerticalScroll.Maximum;
                    }
                    splitContainer1.Panel2.VerticalScroll.Value = v;
                    return;
                }

                p = new Point((int)(e.X / (float)pictureScale.Value), (int)(e.Y / (float)pictureScale.Value));

                switch (drawingMode)
                {
                case DrawingModes.NULL:
                    showSelectionCoordinates(p);

                    if (findResizebleBox(p, out ResizebleBoxSides resizebleBoxSide) != null)
                    {
                        Cursor.Current = resizebleBoxSide == ResizebleBoxSides.Left || resizebleBoxSide == ResizebleBoxSides.Right ? Cursors.VSplit : Cursors.HSplit;
                    }
                    else
                    {
                        Cursor.Current = Cursors.Default;
                    }
                    return;

                case DrawingModes.drawingSelectionBox:
                    if (selectionBoxPoint0.X < p.X)
                    {
                        selectionBoxPoint1.X = selectionBoxPoint0.X;
                        selectionBoxPoint2.X = p.X;
                    }
                    else
                    {
                        selectionBoxPoint1.X = p.X;
                        selectionBoxPoint2.X = selectionBoxPoint0.X;
                    }
                    if (selectionBoxPoint0.Y < p.Y)
                    {
                        selectionBoxPoint1.Y = selectionBoxPoint0.Y;
                        selectionBoxPoint2.Y = p.Y;
                    }
                    else
                    {
                        selectionBoxPoint1.Y = p.Y;
                        selectionBoxPoint2.Y = selectionBoxPoint0.Y;
                    }
                    break;

                case DrawingModes.resizingSelectionBoxV:
                    if (Math.Abs(selectionBoxPoint2.X - p.X) < Math.Abs(p.X - selectionBoxPoint1.X))
                    {
                        selectionBoxPoint2.X = p.X;
                    }
                    else
                    {
                        selectionBoxPoint1.X = p.X;
                    }
                    break;

                case DrawingModes.resizingSelectionBoxH:
                    if (Math.Abs(selectionBoxPoint2.Y - p.Y) < Math.Abs(p.Y - selectionBoxPoint1.Y))
                    {
                        selectionBoxPoint2.Y = p.Y;
                    }
                    else
                    {
                        selectionBoxPoint1.Y = p.Y;
                    }
                    break;
                }
                showSelectionCoordinates(selectionBoxPoint1, selectionBoxPoint2);
                RectangleF r = new RectangleF(selectionBoxPoint1.X, selectionBoxPoint1.Y, selectionBoxPoint2.X - selectionBoxPoint1.X, selectionBoxPoint2.Y - selectionBoxPoint1.Y);
                clearImageFromBoxes();
                drawBoxes(Settings.Appearance.SelectionBoxColor, Settings.Appearance.SelectionBoxBorderWidth, new List <System.Drawing.RectangleF> {
                    r
                });
            };

            picture.MouseUp += delegate(object sender, MouseEventArgs e)
            {
                try
                {
                    if (pages == null)
                    {
                        return;
                    }

                    if (drawingMode == DrawingModes.NULL)
                    {
                        return;
                    }
                    if (drawingMode == DrawingModes.movingImage)
                    {
                        Cursor.Current = Cursors.Default;
                    }
                    drawingMode = DrawingModes.NULL;

                    Template.RectangleF r = new Template.RectangleF(selectionBoxPoint1.X, selectionBoxPoint1.Y, selectionBoxPoint2.X - selectionBoxPoint1.X, selectionBoxPoint2.Y - selectionBoxPoint1.Y);
                    if (r.Width == 0 || r.Y == 0)//accidental tap
                    {
                        return;
                    }

                    switch (settingMode)
                    {
                    case SettingModes.SetAnchor:
                    {
                        if (currentAnchorControl == null)
                        {
                            break;
                        }

                        //currentAnchorControl.SetTagFromControl();???
                        Template.Anchor a = (Template.Anchor)currentAnchorControl.Row.Tag;

                        if (pages[currentPageI].DetectedImageScale >= 0 && pages[currentPageI].DetectedImageScale < 1 && a.Id == GetTemplateFromUI(false).ScalingAnchorId)
                        {
                            Message.Exclaim("When the detected image scale is not 1, changing coordinates of the scaling anchor must not be done. Either switch off scaling by anchor and reload the page or open a page where the detected image scale is 1.", this);
                            break;
                        }

                        a.Position = new Template.PointF {
                            X = r.X, Y = r.Y
                        };
                        try
                        {
                            switch (a.Type)
                            {
                            case Template.Anchor.Types.PdfText:
                            {
                                Template.Anchor.PdfText pt = (Template.Anchor.PdfText)a;
                                pt.CharBoxs = new List <Template.Anchor.PdfText.CharBox>();
                                foreach (Pdf.CharBox cb in Pdf.GetCharBoxsSurroundedByRectangle(pages[currentPageI].PdfCharBoxs, r.GetSystemRectangleF(), true))
                                {
                                    pt.CharBoxs.Add(new Template.Anchor.PdfText.CharBox
                                            {
                                                Char      = cb.Char,
                                                Rectangle = new Template.RectangleF(cb.R.X, cb.R.Y, cb.R.Width, cb.R.Height),
                                            });
                                }
                                pt.Size = new Template.SizeF {
                                    Width = r.Width, Height = r.Height
                                };
                            }
                            break;

                            case Template.Anchor.Types.OcrText:
                            {
                                Template.Anchor.OcrText ot = (Template.Anchor.OcrText)a;
                                ot.CharBoxs = new List <Template.Anchor.OcrText.CharBox>();
                                var selectedOcrCharBoxs = new List <Ocr.CharBox>();
                                if (ot.OcrEntirePage)
                                {
                                    selectedOcrCharBoxs.AddRange(Ocr.GetCharBoxsSurroundedByRectangle(pages[currentPageI].ActiveTemplateOcrCharBoxs, r.GetSystemRectangleF()));
                                }
                                else
                                {
                                    using (Bitmap b = pages[currentPageI].GetRectangleFromActiveTemplateBitmap(r.X / Settings.Constants.Pdf2ImageResolutionRatio, r.Y / Settings.Constants.Pdf2ImageResolutionRatio, r.Width / Settings.Constants.Pdf2ImageResolutionRatio, r.Height / Settings.Constants.Pdf2ImageResolutionRatio))
                                    {
                                        if (b == null)
                                        {
                                            throw new Exception("Selected image is empty.");
                                        }
                                        foreach (Ocr.CharBox cb in Ocr.This.GetCharBoxs(b, pages.ActiveTemplate.TesseractPageSegMode))
                                        {
                                            cb.R.X += r.X;
                                            cb.R.Y += r.Y;
                                            selectedOcrCharBoxs.Add(cb);
                                        }
                                    }
                                }
                                foreach (Ocr.CharBox cb in selectedOcrCharBoxs)
                                {
                                    ot.CharBoxs.Add(new Template.Anchor.OcrText.CharBox
                                            {
                                                Char      = cb.Char,
                                                Rectangle = new Template.RectangleF(cb.R.X, cb.R.Y, cb.R.Width, cb.R.Height),
                                            });
                                }
                                ot.Size = new Template.SizeF {
                                    Width = r.Width, Height = r.Height
                                };
                            }
                            break;

                            case Template.Anchor.Types.ImageData:
                            {
                                Template.Anchor.ImageData id = (Template.Anchor.ImageData)a;
                                using (Bitmap b = pages[currentPageI].GetRectangleFromActiveTemplateBitmap(r.X / Settings.Constants.Pdf2ImageResolutionRatio, r.Y / Settings.Constants.Pdf2ImageResolutionRatio, r.Width / Settings.Constants.Pdf2ImageResolutionRatio, r.Height / Settings.Constants.Pdf2ImageResolutionRatio))
                                {
                                    if (b == null)
                                    {
                                        throw new Exception("Selected image is empty.");
                                    }
                                    id.Image = new ImageData(b);
                                }
                            }
                            break;

                            case Template.Anchor.Types.CvImage:
                            {
                                Template.Anchor.CvImage ci = (Template.Anchor.CvImage)a;
                                using (Bitmap b = pages[currentPageI].GetRectangleFromActiveTemplateBitmap(r.X / Settings.Constants.Pdf2ImageResolutionRatio, r.Y / Settings.Constants.Pdf2ImageResolutionRatio, r.Width / Settings.Constants.Pdf2ImageResolutionRatio, r.Height / Settings.Constants.Pdf2ImageResolutionRatio))
                                {
                                    if (b == null)
                                    {
                                        throw new Exception("Selected image is empty.");
                                    }
                                    ci.Image = new CvImage(b);
                                }
                            }
                            break;

                            default:
                                throw new Exception("Unknown option: " + a.Type);
                            }
                            setAnchorRow(currentAnchorControl.Row, a);
                            clearImageFromBoxes();
                            findAndDrawAnchor(a.Id);
                        }
                        finally
                        {
                            anchors.EndEdit();
                        }
                    }
                    break;

                    case SettingModes.SetField:
                    {
                        if (fields.SelectedRows.Count < 1)
                        {
                            break;
                        }
                        var            row = fields.SelectedRows[0];
                        Template.Field f   = (Template.Field)row.Tag;
                        f.Rectangle = r;

                        if (f.LeftAnchor != null)
                        {
                            Page.AnchorActualInfo aai = pages[currentPageI].GetAnchorActualInfo(f.LeftAnchor.Id);
                            f.LeftAnchor.Shift = aai.Shift.Width;
                        }
                        if (f.TopAnchor != null)
                        {
                            Page.AnchorActualInfo aai = pages[currentPageI].GetAnchorActualInfo(f.TopAnchor.Id);
                            f.TopAnchor.Shift = aai.Shift.Height;
                        }
                        if (f.RightAnchor != null)
                        {
                            Page.AnchorActualInfo aai = pages[currentPageI].GetAnchorActualInfo(f.RightAnchor.Id);
                            f.RightAnchor.Shift = aai.Shift.Width;
                        }
                        if (f.BottomAnchor != null)
                        {
                            Page.AnchorActualInfo aai = pages[currentPageI].GetAnchorActualInfo(f.BottomAnchor.Id);
                            f.BottomAnchor.Shift = aai.Shift.Height;
                        }

                        setFieldRow(row, f);
                    }
                    break;

                    case SettingModes.NULL:
                        break;

                    default:
                        throw new Exception("Unknown option: " + settingMode);
                    }
                }
                catch (Exception ex)
                {
                    Message.Error2(ex, this);
                }
            };

            Shown += delegate
            {
                Application.DoEvents();//make form be drawn completely
                setUIFromTemplate(templateManager.Template);
            };

            this.EnumControls((Control c) =>
            {
                if (c is SplitContainer s)
                {
                    s.BackColor        = Color.FromArgb(80, 70, 0);
                    s.SplitterWidth    = 2;
                    s.Panel1.BackColor = SystemColors.Control;
                    s.Panel2.BackColor = SystemColors.Control;
                }
            }, true);

            testFile.TextChanged += delegate
            {
                try
                {
                    dispose(false);

                    if (string.IsNullOrWhiteSpace(testFile.Text))
                    {
                        return;
                    }

                    templateManager.LastTestFile = testFile.Text;

                    testFile.SelectionStart = testFile.Text.Length;
                    testFile.ScrollToCaret();

                    if (!File.Exists(testFile.Text))
                    {
                        string m = "File '" + testFile.Text + "' does not exist!";
                        Log.Error(m);
                        Message.Error(m, this);
                        return;
                    }

                    pages            = new PageCollection(testFile.Text, true);
                    totalPageNumber  = pages.TotalCount;
                    lTotalPages.Text = " / " + totalPageNumber;
                    showPage(1);
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    Message.Error(ex, this);
                }
            };

            pictureScale.ValueChanged += delegate
            {
                if (!loadingTemplate)
                {
                    setScaledImage();
                }
            };

            Load += delegate
            {
                //Application.DoEvents();//make form be drawn completely
                //BeginInvoke((Action<Template>)setUIFromTemplate, templateManager.Template);
            };

            FormClosed += delegate
            {
                bitmapPreparationForm.Close();
            };

            bSave.Click           += Save_Click;
            bOK.Click             += OK_Click;
            bCancel.Click         += delegate { Close(); };
            Help.LinkClicked      += Help_LinkClicked;
            Configure.LinkClicked += Configure_LinkClicked;
            About.LinkClicked     += About_LinkClicked;

            bTestFile.Click += delegate(object sender, EventArgs e)
            {
                OpenFileDialog d = new OpenFileDialog();
                if (!string.IsNullOrWhiteSpace(testFile.Text))
                {
                    d.InitialDirectory = PathRoutines.GetFileDir(testFile.Text);
                }
                else
                if (!string.IsNullOrWhiteSpace(templateManager.TestFileDefaultFolder))
                {
                    d.InitialDirectory = templateManager.TestFileDefaultFolder;
                }

                d.Filter = "PDF|*.pdf|"
                           + "All files (*.*)|*.*";
                if (d.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                {
                    return;
                }
                testFile.Text = d.FileName;
            };

            ShowPdfText.LinkClicked += ShowPdfText_LinkClicked;
            ShowOcrText.LinkClicked += ShowOcrText_LinkClicked;
            ShowAsJson.LinkClicked  += showAsJson_LinkClicked;

            tCurrentPage.Leave += delegate
            {
                changeCurrentPage();
            };
            tCurrentPage.KeyDown += delegate(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    changeCurrentPage();
                }
            };
        }
示例#11
0
        public TemplateForm(TemplateManager templateManager)
        {
            InitializeComponent();

            Icon = Win.AssemblyRoutines.GetAppIcon();
            Text = Program.FullName + ": Template Editor";

            this.templateManager = templateManager;

            initializeAnchorsTable();
            initializeConditionsTable();
            initializeFieldsTable();

            picture.MouseDown += delegate(object sender, MouseEventArgs e)
            {
                if (pages == null)
                {
                    return;
                }

                Point p = new Point((int)(e.X / (float)pictureScale.Value), (int)(e.Y / (float)pictureScale.Value));

                ResizebleBox rb = findResizebleBox(p, out ResizebleBoxSides resizebleBoxSide);
                if (rb != null)
                {
                    drawingMode        = resizebleBoxSide == ResizebleBoxSides.Left || resizebleBoxSide == ResizebleBoxSides.Right ? DrawingModes.resizingSelectionBoxV : DrawingModes.resizingSelectionBoxH;
                    Cursor.Current     = drawingMode == DrawingModes.resizingSelectionBoxV ? Cursors.VSplit : Cursors.HSplit;
                    selectionBoxPoint0 = rb.R.Location;
                    selectionBoxPoint1 = rb.R.Location;
                    selectionBoxPoint2 = new Point(rb.R.Right, rb.R.Bottom);
                }
                else
                {
                    drawingMode        = DrawingModes.drawingSelectionBox;
                    selectionBoxPoint0 = p;
                    selectionBoxPoint1 = p;
                    selectionBoxPoint2 = p;
                }
                selectionCoordinates.Text = selectionBoxPoint1.ToString();
            };

            picture.MouseMove += delegate(object sender, MouseEventArgs e)
            {
                if (pages == null)
                {
                    return;
                }

                Point p = new Point((int)(e.X / (float)pictureScale.Value), (int)(e.Y / (float)pictureScale.Value));

                switch (drawingMode)
                {
                case DrawingModes.NULL:
                    selectionCoordinates.Text = p.ToString();

                    if (findResizebleBox(p, out ResizebleBoxSides resizebleBoxSide) != null)
                    {
                        Cursor.Current = resizebleBoxSide == ResizebleBoxSides.Left || resizebleBoxSide == ResizebleBoxSides.Right ? Cursors.VSplit : Cursors.HSplit;
                    }
                    else
                    {
                        Cursor.Current = Cursors.Default;
                    }
                    return;

                case DrawingModes.drawingSelectionBox:
                    if (selectionBoxPoint0.X < p.X)
                    {
                        selectionBoxPoint1.X = selectionBoxPoint0.X;
                        selectionBoxPoint2.X = p.X;
                    }
                    else
                    {
                        selectionBoxPoint1.X = p.X;
                        selectionBoxPoint2.X = selectionBoxPoint0.X;
                    }
                    if (selectionBoxPoint0.Y < p.Y)
                    {
                        selectionBoxPoint1.Y = selectionBoxPoint0.Y;
                        selectionBoxPoint2.Y = p.Y;
                    }
                    else
                    {
                        selectionBoxPoint1.Y = p.Y;
                        selectionBoxPoint2.Y = selectionBoxPoint0.Y;
                    }
                    break;

                case DrawingModes.resizingSelectionBoxV:
                    if (Math.Abs(selectionBoxPoint2.X - p.X) < Math.Abs(p.X - selectionBoxPoint1.X))
                    {
                        selectionBoxPoint2.X = p.X;
                    }
                    else
                    {
                        selectionBoxPoint1.X = p.X;
                    }
                    break;

                case DrawingModes.resizingSelectionBoxH:
                    if (Math.Abs(selectionBoxPoint2.Y - p.Y) < Math.Abs(p.Y - selectionBoxPoint1.Y))
                    {
                        selectionBoxPoint2.Y = p.Y;
                    }
                    else
                    {
                        selectionBoxPoint1.Y = p.Y;
                    }
                    break;
                }
                selectionCoordinates.Text = selectionBoxPoint1.ToString() + ":" + selectionBoxPoint2.ToString();
                RectangleF r = new RectangleF(selectionBoxPoint1.X, selectionBoxPoint1.Y, selectionBoxPoint2.X - selectionBoxPoint1.X, selectionBoxPoint2.Y - selectionBoxPoint1.Y);
                clearImageFromBoxes();
                drawBoxes(Settings.Appearance.SelectionBoxColor, Settings.Appearance.SelectionBoxBorderWidth, new List <System.Drawing.RectangleF> {
                    r
                });
            };

            picture.MouseUp += delegate(object sender, MouseEventArgs e)
            {
                try
                {
                    if (pages == null)
                    {
                        return;
                    }

                    if (drawingMode == DrawingModes.NULL)
                    {
                        return;
                    }
                    drawingMode = DrawingModes.NULL;

                    Template.RectangleF r = new Template.RectangleF(selectionBoxPoint1.X, selectionBoxPoint1.Y, selectionBoxPoint2.X - selectionBoxPoint1.X, selectionBoxPoint2.Y - selectionBoxPoint1.Y);
                    if (r.Width == 0 || r.Y == 0)//accidental tap
                    {
                        return;
                    }

                    switch (settingMode)
                    {
                    case SettingModes.SetAnchor:
                    {
                        if (currentAnchorControl == null)
                        {
                            break;
                        }

                        currentAnchorControl.SetTagFromControl();
                        Template.Anchor a = (Template.Anchor)currentAnchorControl.Row.Tag;
                        a.Position = new Template.PointF {
                            X = r.X, Y = r.Y
                        };
                        try
                        {
                            switch (a.Type)
                            {
                            case Template.Anchor.Types.PdfText:
                            {
                                Template.Anchor.PdfText pt = (Template.Anchor.PdfText)a;
                                pt.CharBoxs = new List <Template.Anchor.PdfText.CharBox>();
                                List <Pdf.Line> lines = Pdf.GetLines(Pdf.GetCharBoxsSurroundedByRectangle(pages[currentPageI].PdfCharBoxs, r.GetSystemRectangleF(), true), null);
                                foreach (Pdf.Line l in lines)
                                {
                                    foreach (Pdf.CharBox cb in l.CharBoxs)
                                    {
                                        pt.CharBoxs.Add(new Template.Anchor.PdfText.CharBox
                                                {
                                                    Char      = cb.Char,
                                                    Rectangle = new Template.RectangleF(cb.R.X, cb.R.Y, cb.R.Width, cb.R.Height),
                                                });
                                    }
                                }
                                pt.Size = new Template.SizeF {
                                    Width = r.Width, Height = r.Height
                                };
                            }
                            break;

                            case Template.Anchor.Types.OcrText:
                            {
                                Template.Anchor.OcrText ot = (Template.Anchor.OcrText)a;
                                ot.CharBoxs = new List <Template.Anchor.OcrText.CharBox>();
                                var selectedOcrCharBoxs = new List <Ocr.CharBox>();
                                if (ot.OcrEntirePage)
                                {
                                    selectedOcrCharBoxs.AddRange(Ocr.GetCharBoxsSurroundedByRectangle(pages[currentPageI].ActiveTemplateOcrCharBoxs, r.GetSystemRectangleF()));
                                }
                                else
                                {
                                    foreach (Ocr.CharBox cb in Ocr.This.GetCharBoxs(pages[currentPageI].GetRectangleFromActiveTemplateBitmap(r.X / Settings.Constants.Image2PdfResolutionRatio, r.Y / Settings.Constants.Image2PdfResolutionRatio, r.Width / Settings.Constants.Image2PdfResolutionRatio, r.Height / Settings.Constants.Image2PdfResolutionRatio)))
                                    {
                                        cb.R.X += r.X;
                                        cb.R.Y += r.Y;
                                        selectedOcrCharBoxs.Add(cb);
                                    }
                                }
                                foreach (Ocr.Line l in Ocr.GetLines(selectedOcrCharBoxs, null))
                                {
                                    foreach (Ocr.CharBox cb in l.CharBoxs)
                                    {
                                        ot.CharBoxs.Add(new Template.Anchor.OcrText.CharBox
                                                {
                                                    Char      = cb.Char,
                                                    Rectangle = new Template.RectangleF(cb.R.X, cb.R.Y, cb.R.Width, cb.R.Height),
                                                });
                                    }
                                }
                                ot.Size = new Template.SizeF {
                                    Width = r.Width, Height = r.Height
                                };
                            }
                            break;

                            case Template.Anchor.Types.ImageData:
                            {
                                Template.Anchor.ImageData id = (Template.Anchor.ImageData)a;
                                using (Bitmap b = pages[currentPageI].GetRectangleFromActiveTemplateBitmap(r.X / Settings.Constants.Image2PdfResolutionRatio, r.Y / Settings.Constants.Image2PdfResolutionRatio, r.Width / Settings.Constants.Image2PdfResolutionRatio, r.Height / Settings.Constants.Image2PdfResolutionRatio))
                                {
                                    id.Image = new ImageData(b);
                                }
                            }
                            break;

                            case Template.Anchor.Types.CvImage:
                            {
                                Template.Anchor.CvImage ci = (Template.Anchor.CvImage)a;
                                using (Bitmap b = pages[currentPageI].GetRectangleFromActiveTemplateBitmap(r.X / Settings.Constants.Image2PdfResolutionRatio, r.Y / Settings.Constants.Image2PdfResolutionRatio, r.Width / Settings.Constants.Image2PdfResolutionRatio, r.Height / Settings.Constants.Image2PdfResolutionRatio))
                                {
                                    ci.Image = new CvImage(b);
                                }
                            }
                            break;

                            default:
                                throw new Exception("Unknown option: " + a.Type);
                            }
                            setAnchorRow(currentAnchorControl.Row, a);
                            clearImageFromBoxes();
                            findAndDrawAnchor(a.Id);
                        }
                        finally
                        {
                            anchors.EndEdit();
                        }
                    }
                    break;

                    case SettingModes.SetField:
                    {
                        if (fields.SelectedRows.Count < 1)
                        {
                            break;
                        }
                        var            row = fields.SelectedRows[0];
                        Template.Field f   = (Template.Field)row.Tag;
                        f.Rectangle = r;

                        if (f.LeftAnchor != null)
                        {
                            Page.AnchorActualInfo aai = pages[currentPageI].GetAnchorActualInfo(f.LeftAnchor.Id);
                            f.LeftAnchor.Shift = aai.Shift.Width;
                        }
                        if (f.TopAnchor != null)
                        {
                            Page.AnchorActualInfo aai = pages[currentPageI].GetAnchorActualInfo(f.TopAnchor.Id);
                            f.TopAnchor.Shift = aai.Shift.Height;
                        }
                        if (f.RightAnchor != null)
                        {
                            Page.AnchorActualInfo aai = pages[currentPageI].GetAnchorActualInfo(f.RightAnchor.Id);
                            f.RightAnchor.Shift = aai.Shift.Width;
                        }
                        if (f.BottomAnchor != null)
                        {
                            Page.AnchorActualInfo aai = pages[currentPageI].GetAnchorActualInfo(f.BottomAnchor.Id);
                            f.BottomAnchor.Shift = aai.Shift.Height;
                        }

                        setFieldRow(row, f);
                        extractFieldAndDrawSelectionBox(f);
                        //owners2resizebleBox[f] = new ResizebleBox(f, f.Rectangle.GetSystemRectangleF(), Settings.Appearance.SelectionBoxBorderWidth);
                    }
                    break;

                    case SettingModes.NULL:
                        break;

                    default:
                        throw new Exception("Unknown option: " + settingMode);
                    }
                }
                catch (Exception ex)
                {
                    Message.Error2(ex);
                }
            };

            Shown += delegate
            {
                Application.DoEvents();//make form be drawn completely
                setUIFromTemplate(templateManager.Template);
            };

            FormClosed += delegate
            {
                if (scaledCurrentPageBitmap != null)
                {
                    scaledCurrentPageBitmap.Dispose();
                    scaledCurrentPageBitmap = null;
                }
                if (pages != null)
                {
                    pages.Dispose();
                    pages = null;
                }

                templateManager.LastTestFile = testFile.Text;
            };

            this.EnumControls((Control c) =>
            {
                if (c is SplitContainer s)
                {
                    s.BackColor        = Color.FromArgb(80, 70, 0);
                    s.SplitterWidth    = 2;
                    s.Panel1.BackColor = SystemColors.Control;
                    s.Panel2.BackColor = SystemColors.Control;
                }
            }, true);

            testFile.TextChanged += delegate
            {
                try
                {
                    if (picture.Image != null)
                    {
                        picture.Image.Dispose();
                        picture.Image = null;
                    }
                    if (scaledCurrentPageBitmap != null)
                    {
                        scaledCurrentPageBitmap.Dispose();
                        scaledCurrentPageBitmap = null;
                    }
                    if (pages != null)
                    {
                        pages.Dispose();
                        pages = null;
                    }

                    if (string.IsNullOrWhiteSpace(testFile.Text))
                    {
                        return;
                    }

                    testFile.SelectionStart = testFile.Text.Length;
                    testFile.ScrollToCaret();

                    if (!File.Exists(testFile.Text))
                    {
                        Win.LogMessage.Error("File '" + testFile.Text + "' does not exist!");
                        return;
                    }

                    pages            = new PageCollection(testFile.Text);
                    totalPageNumber  = pages.PdfReader.NumberOfPages;
                    lTotalPages.Text = " / " + totalPageNumber;
                    showPage(1);
                }
                catch (Exception ex)
                {
                    Win.LogMessage.Error(ex);
                }
            };

            pictureScale.ValueChanged += delegate
            {
                if (!loadingTemplate)
                {
                    setScaledImage();
                }
            };

            pageRotation.SelectedIndexChanged += delegate
            {
                reloadPageBitmaps();
                //showPage(currentPageI);
            };

            autoDeskew.CheckedChanged += delegate
            {
                reloadPageBitmaps();
                //showPage(currentPageI);
            };

            Load += delegate
            {
            };

            save.Click            += Save_Click;
            cancel.Click          += delegate { Close(); };
            Help.LinkClicked      += Help_LinkClicked;
            Configure.LinkClicked += Configure_LinkClicked;
            About.LinkClicked     += About_LinkClicked;

            bTestFile.Click += delegate(object sender, EventArgs e)
            {
                OpenFileDialog d = new OpenFileDialog();
                if (!string.IsNullOrWhiteSpace(testFile.Text))
                {
                    d.InitialDirectory = PathRoutines.GetFileDir(testFile.Text);
                }
                else
                if (!string.IsNullOrWhiteSpace(templateManager.TestFileDefaultFolder))
                {
                    d.InitialDirectory = templateManager.TestFileDefaultFolder;
                }

                d.Filter = "PDF|*.pdf|"
                           + "All files (*.*)|*.*";
                if (d.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                {
                    return;
                }
                testFile.Text = d.FileName;
            };

            ShowPdfText.LinkClicked += ShowPdfText_LinkClicked;
            ShowOcrText.LinkClicked += ShowOcrText_LinkClicked;
            ShowAsJson.LinkClicked  += showAsJson_LinkClicked;

            tCurrentPage.Leave += delegate
            {
                changeCurrentPage();
            };
            tCurrentPage.KeyDown += delegate(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    changeCurrentPage();
                }
            };
        }
示例#12
0
            Dictionary <Template.Field.ValueTypes, object> valueTypes2cachedValue = new Dictionary <Template.Field.ValueTypes, object>();//!!!cache Table field values for internal reuse only!!!
            object getValue_(Template.Field.ValueTypes valueType)
            {
                if (ActualRectangle == null || TableFieldActualInfo?.Found == false)
                {
                    return(null);
                }
                RectangleF r = (RectangleF)ActualRectangle;

                switch (valueType)
                {
                case Template.Field.ValueTypes.PdfText:
                    List <string> ls;
                    if (ActualField.ColumnOfTable == null)
                    {
                        ls = Pdf.GetTextLinesSurroundedByRectangle(page.PdfCharBoxs, r, page.PageCollection.ActiveTemplate.TextAutoInsertSpace);
                    }
                    else
                    {
                        ls = getPdfTextLinesAsTableColumn();
                    }
                    return(string.Join("\r\n", ls));

                case Template.Field.ValueTypes.PdfTextLines:
                    if (ActualField.ColumnOfTable == null)
                    {
                        return(Pdf.GetTextLinesSurroundedByRectangle(page.PdfCharBoxs, r, page.PageCollection.ActiveTemplate.TextAutoInsertSpace));
                    }
                    return(getPdfTextLinesAsTableColumn());

                case Template.Field.ValueTypes.PdfCharBoxs:
                    return(Pdf.GetCharBoxsSurroundedByRectangle(page.PdfCharBoxs, r));

                case Template.Field.ValueTypes.OcrText:
                    if (ActualField.ColumnOfTable == null)
                    {
                        return(Ocr.This.GetTextSurroundedByRectangle(page.ActiveTemplateBitmap, r));
                    }
                    throw new Exception("This code has to be debugged!");
                    return(string.Join("\r\n", getOcrTextLinesAsTableColumn()));

                case Template.Field.ValueTypes.OcrTextLines:
                    throw new Exception("This code has to be debugged!");
                    if (ActualField.ColumnOfTable == null)
                    {
                        return(Regex.Split(Ocr.This.GetTextSurroundedByRectangle(page.ActiveTemplateBitmap, r), "$", RegexOptions.Multiline));
                    }
                    return(getOcrTextLinesAsTableColumn());

                case Template.Field.ValueTypes.OcrCharBoxs:
                    return(Ocr.GetCharBoxsSurroundedByRectangle(page.ActiveTemplateOcrCharBoxs, r));

                case Template.Field.ValueTypes.Image:
                    using (Bitmap b = page.GetRectangleFromActiveTemplateBitmap(r.X / Settings.Constants.Image2PdfResolutionRatio, r.Y / Settings.Constants.Image2PdfResolutionRatio, r.Width / Settings.Constants.Image2PdfResolutionRatio, r.Height / Settings.Constants.Image2PdfResolutionRatio))
                    {
                        return(Page.GetScaledImage2Pdf(b));
                    }

                default:
                    throw new Exception("Unknown option: " + valueType);
                }
            }
        object extractFieldAndDrawSelectionBox(Template.Field field)
        {
            try
            {
                if (pages == null)
                {
                    return(null);
                }

                if (field.Rectangle == null)
                {
                    return(null);
                }

                pages.ActiveTemplate = getTemplateFromUI(false);

                RectangleF r = field.Rectangle.GetSystemRectangleF();
                if (field.LeftAnchor != null)
                {
                    if (!findAndDrawAnchor(field.LeftAnchor.Id))
                    {
                        return(null);
                    }
                    Page.AnchorActualInfo aai = pages[currentPageI].GetAnchorActualInfo(field.LeftAnchor.Id);
                    float right = r.Right;
                    r.X    += aai.Shift.Width - field.LeftAnchor.Shift;
                    r.Width = right - r.X;
                }
                if (field.TopAnchor != null)
                {
                    if (!findAndDrawAnchor(field.TopAnchor.Id))
                    {
                        return(null);
                    }
                    Page.AnchorActualInfo aai = pages[currentPageI].GetAnchorActualInfo(field.TopAnchor.Id);
                    float bottom = r.Bottom;
                    r.Y     += aai.Shift.Height - field.TopAnchor.Shift;
                    r.Height = bottom - r.Y;
                }
                if (field.RightAnchor != null)
                {
                    if (!findAndDrawAnchor(field.RightAnchor.Id))
                    {
                        return(null);
                    }
                    Page.AnchorActualInfo aai = pages[currentPageI].GetAnchorActualInfo(field.RightAnchor.Id);
                    r.Width += aai.Shift.Width - field.RightAnchor.Shift;
                }
                if (field.BottomAnchor != null)
                {
                    if (!findAndDrawAnchor(field.BottomAnchor.Id))
                    {
                        return(null);
                    }
                    Page.AnchorActualInfo aai = pages[currentPageI].GetAnchorActualInfo(field.BottomAnchor.Id);
                    r.Height += aai.Shift.Height - field.BottomAnchor.Shift;
                }
                if (r.Width <= 0 || r.Height <= 0)
                {
                    return(null);
                }
                owners2resizebleBox[field] = new ResizebleBox(field, r, Settings.Appearance.SelectionBoxBorderWidth);
                switch (field.Type)
                {
                case Template.Field.Types.PdfText:
                    string s;
                    if (field.ColumnOfTable != null)
                    {
                        //RectangleF? tr = pages[currentPageI].GetTableActualRectangle((Template.Field.PdfText)field);
                        int            fieldDefinitionIndex = pages.ActiveTemplate.Fields.Where(x => x.Name == field.Name).TakeWhile(x => x != field).Count();
                        Template.Field tableField;
                        try
                        {
                            tableField = pages.ActiveTemplate.Fields.Where(x => x.Name == field.ColumnOfTable).ElementAt(fieldDefinitionIndex);
                        }
                        catch (Exception e)
                        {
                            Message.Error("Field " + field.ColumnOfTable + " does not have enough definitions to respect definition " + field.Name + "[" + fieldDefinitionIndex + "]");
                            return(null);
                        }
                        RectangleF?tr = pages[currentPageI].GetFieldActualRectange(tableField);
                        if (tr == null)
                        {
                            return(null);
                        }
                        drawBoxes(Settings.Appearance.TableBoxColor, Settings.Appearance.TableBoxBorderWidth, new List <RectangleF> {
                            (RectangleF)tr
                        });
                        //return Page.NormalizeText(Pdf.GetTextSurroundedByRectangle(pages[currentPageI].PdfCharBoxs, r, pages.ActiveTemplate.TextAutoInsertSpaceThreshold, pages.ActiveTemplate.TextAutoInsertSpaceSubstitute));
                        s = string.Join("\r\n", pages[currentPageI].GetTextLinesAsTableColumn(field, r));
                    }
                    else
                    {
                        s = Pdf.GetTextSurroundedByRectangle(pages[currentPageI].PdfCharBoxs, r, pages.ActiveTemplate.TextAutoInsertSpace);
                    }
                    drawBoxes(Settings.Appearance.SelectionBoxColor, Settings.Appearance.SelectionBoxBorderWidth, new List <RectangleF> {
                        r
                    });
                    return(Page.NormalizeText(s));

                case Template.Field.Types.OcrText:
                    drawBoxes(Settings.Appearance.SelectionBoxColor, Settings.Appearance.SelectionBoxBorderWidth, new List <RectangleF> {
                        r
                    });
                    return(Page.NormalizeText(Ocr.This.GetTextSurroundedByRectangle(pages[currentPageI].ActiveTemplateBitmap, r)));

                case Template.Field.Types.ImageData:
                    drawBoxes(Settings.Appearance.SelectionBoxColor, Settings.Appearance.SelectionBoxBorderWidth, new List <RectangleF> {
                        r
                    });
                    using (Bitmap rb = pages[currentPageI].GetRectangleFromActiveTemplateBitmap(r.X / Settings.Constants.Image2PdfResolutionRatio, r.Y / Settings.Constants.Image2PdfResolutionRatio, r.Width / Settings.Constants.Image2PdfResolutionRatio, r.Height / Settings.Constants.Image2PdfResolutionRatio))
                    {
                        return(ImageData.GetScaled(rb, Settings.Constants.Image2PdfResolutionRatio));
                    }

                default:
                    throw new Exception("Unknown option: " + field.Type);
                }
            }
            catch (Exception ex)
            {
                //Log.Message.Error("Rectangle", ex);
                Log.Message.Error(ex);
            }
            return(null);
        }
示例#14
0
        object getValue(Template.Field field /*, int fieldDefinitionIndex*/, ValueTypes valueType = ValueTypes.Default)
        {
            RectangleF?r_ = GetFieldActualRectange(field);

            if (r_ == null)
            {
                return(null);
            }
            RectangleF r = (RectangleF)r_;

            switch (field.Type)
            {
            case Template.Field.Types.PdfText:
                Template.Field.PdfText pt = (Template.Field.PdfText)field;
                switch (valueType)
                {
                case ValueTypes.Default:
                    List <string> ls;
                    if (pt.ColumnOfTable == null)
                    {
                        ls = Pdf.GetTextLinesSurroundedByRectangle(PdfCharBoxs, r, pageCollection.ActiveTemplate.TextAutoInsertSpace);
                    }
                    else
                    {
                        ls = GetTextLinesAsTableColumn(pt, r);
                    }
                    return(string.Join("\r\n", ls));

                case ValueTypes.TextLines:
                    if (pt.ColumnOfTable == null)
                    {
                        ls = Pdf.GetTextLinesSurroundedByRectangle(PdfCharBoxs, r, pageCollection.ActiveTemplate.TextAutoInsertSpace);
                    }
                    else
                    {
                        ls = GetTextLinesAsTableColumn(pt, r);
                    }
                    return(ls);

                case ValueTypes.CharBoxs:
                    return(Pdf.GetCharBoxsSurroundedByRectangle(PdfCharBoxs, r));

                default:
                    throw new Exception("Unknown option: " + valueType);
                }

            case Template.Field.Types.OcrText:
                Template.Field.OcrText ot = (Template.Field.OcrText)field;
                switch (valueType)
                {
                case ValueTypes.Default:
                    return(Ocr.This.GetTextSurroundedByRectangle(ActiveTemplateBitmap, r));

                case ValueTypes.TextLines:
                    throw new Exception("To be implemented.");

                case ValueTypes.CharBoxs:
                    return(Ocr.GetCharBoxsSurroundedByRectangle(ActiveTemplateOcrCharBoxs, r));

                default:
                    throw new Exception("Unknown option: " + valueType);
                }

            case Template.Field.Types.ImageData:
                switch (valueType)
                {
                case ValueTypes.Default:
                    using (Bitmap rb = GetRectangleFromActiveTemplateBitmap(r.X / Settings.Constants.Image2PdfResolutionRatio, r.Y / Settings.Constants.Image2PdfResolutionRatio, r.Width / Settings.Constants.Image2PdfResolutionRatio, r.Height / Settings.Constants.Image2PdfResolutionRatio))
                    {
                        return(ImageData.GetScaled(rb, Settings.Constants.Image2PdfResolutionRatio));
                    }

                case ValueTypes.TextLines:
                case ValueTypes.CharBoxs:
                    throw new Exception("Option " + valueType + " cannot be used this Field type.");

                default:
                    throw new Exception("Unknown option: " + valueType);
                }

            default:
                throw new Exception("Unknown option: " + field.Type);
            }
        }