internal FieldActualInfo(Page page, Template.Field actualfield, RectangleF?actualRectangle, FieldActualInfo tableFieldActualInfo)
 {
     this.page            = page;
     ActualField          = actualfield;
     ActualRectangle      = actualRectangle;
     TableFieldActualInfo = tableFieldActualInfo;
 }
 internal FieldActualInfo(Page page, Template.Field actualfield, RectangleF?actualRectangle, FieldActualInfo tableFieldActualInfo)
 {
     this.page            = page;
     ActualField          = actualfield;
     ActualRectangle      = actualRectangle;
     TableFieldActualInfo = tableFieldActualInfo;
     Found             = ActualRectangle != null;
     types2cachedValue = new HandyDictionary <Template.Field.Types, object>(
         disposeValue: (object v) =>
     {
         if (v == null)
         {
             return;
         }
         if (v is Bitmap)
         {
             ((Bitmap)v).Dispose();
         }
         else if (v is List <Bitmap> )
         {
             foreach (Bitmap b in (List <Bitmap>)v)
             {
                 b?.Dispose();
             }
         }
     }
         );
 }
        RectangleF?getFieldActualRectangle(Template.Field field)
        {
            if (!field.IsSet())
            {
                throw new Exception("Field is not set.");
            }
            if (field.Rectangle.Width <= Settings.Constants.CoordinateDeviationMargin || field.Rectangle.Height <= Settings.Constants.CoordinateDeviationMargin)
            {
                throw new Exception("Rectangle is malformed.");
            }
            RectangleF r = field.Rectangle.GetSystemRectangleF();

            if (field.LeftAnchor != null)
            {
                Page.AnchorActualInfo aai = GetAnchorActualInfo(field.LeftAnchor.Id);
                if (!aai.Found)
                {
                    return(null);
                }
                float right = r.Right;
                r.X    += aai.Shift.Width - field.LeftAnchor.Shift;
                r.Width = right - r.X;
            }
            if (field.TopAnchor != null)
            {
                Page.AnchorActualInfo aai = GetAnchorActualInfo(field.TopAnchor.Id);
                if (!aai.Found)
                {
                    return(null);
                }
                float bottom = r.Bottom;
                r.Y     += aai.Shift.Height - field.TopAnchor.Shift;
                r.Height = bottom - r.Y;
            }
            if (field.RightAnchor != null)
            {
                Page.AnchorActualInfo aai = GetAnchorActualInfo(field.RightAnchor.Id);
                if (!aai.Found)
                {
                    return(null);
                }
                r.Width += aai.Shift.Width - field.RightAnchor.Shift;
            }
            if (field.BottomAnchor != null)
            {
                Page.AnchorActualInfo aai = GetAnchorActualInfo(field.BottomAnchor.Id);
                if (!aai.Found)
                {
                    return(null);
                }
                r.Height += aai.Shift.Height - field.BottomAnchor.Shift;
            }
            if (r.Width <= 0 || r.Height <= 0)
            {
                return(null);
            }
            return(r);
        }
Exemple #4
0
        bool setFieldRowValue(DataGridViewRow row, bool setEmpty)
        {
            Template.Field f = (Template.Field)row.Tag;
            if (f == null)
            {
                return(false);
            }
            if (!f.IsSet())
            {
                setRowStatus(statuses.WARNING, row, "Not set");
                return(false);
            }
            DataGridViewCell c = row.Cells["Value"];

            if (c is DataGridViewImageCell && c.Value != null)
            {
                ((Bitmap)c.Value).Dispose();
            }
            if (setEmpty)
            {
                c.Value = null;
                setRowStatus(statuses.NEUTRAL, row, "");
                return(false);
            }
            clearImageFromBoxes();
            object v = extractFieldAndDrawSelectionBox(f);

            if (f.Type == Template.Field.Types.ImageData)
            {
                if (!(c is DataGridViewImageCell))
                {
                    c.Dispose();
                    c = new DataGridViewImageCell();
                    row.Cells["Value"] = c;
                }
            }
            else
            {
                if (c is DataGridViewImageCell)
                {
                    c.Dispose();
                    c = new DataGridViewTextBoxCell();
                    row.Cells["Value"] = c;
                }
            }
            c.Value = v;
            if (c.Value != null)
            {
                setRowStatus(statuses.SUCCESS, row, "Found");
            }
            else
            {
                setRowStatus(statuses.ERROR, row, "Error");
            }
            return(v != null);
        }
        void createNewField()
        {
            Template.Field f = new Template.Field {
                Name = ""
            };
            int i = fields.CurrentRow?.Index >= 0 ? fields.CurrentRow.Index + 1 : fields.Rows.Count;

            fields.Rows.Insert(i, 1);
            setFieldRow(fields.Rows[i], f);
            fields.Rows[i].Selected = true;
        }
        /// <summary>
        /// Tries field definitions of the given name in turn until some is found on the page.
        /// </summary>
        /// <param name="fieldName">field is referenced by name because there may be several field-definitions for the same name</param>
        /// <param name="actualField">actual field definition which was found on the page</param>
        /// <param name="type">if not set then DefaultValueType is used</param>
        /// <returns></returns>
        public object GetValue(string fieldName, out Template.Field actualField, Template.Field.Types?type = null)
        {
            FieldActualInfo fai = getFoundFieldActualInfo(fieldName);

            if (!fai.Found)
            {
                actualField = null;
                return(null);
            }
            actualField = fai.ActualField;
            return(fai.GetValue(type == null ? fai.ActualField.Type : (Template.Field.Types)type));
        }
        ///// <summary>
        ///// !!!passing Template.Field would be deceitful for 2 reasons:
        ///// - it may belong to another template than ActiveTemplate;
        ///// - it implies that a Template.Field object is equivalent to a field while it is just one of its defintions;
        ///// </summary>
        ///// <param name="field"></param>
        ///// <returns></returns>
        //public object GetValue(Template.Field exactField, Template.Field.ValueTypes? valueType = null)
        //{
        //    RectangleF? ar = getFieldActualRectangle(exactField);
        //    FieldActualInfo fai = new FieldActualInfo(this, exactField, ar, exactField.ColumnOfTable != null ? getFoundFieldActualInfo(exactField.ColumnOfTable) : null);
        //    if (!fai.Found)
        //        return null;
        //    return fai.GetValue(valueType == null ? fai.ActualField.DefaultValueType : (Template.Field.ValueTypes)valueType);
        //}

        internal FieldActualInfo GetFieldActualInfo(Template.Field field)
        {
            List <FieldActualInfo> fais = getFieldActualInfos(field.Name);
            FieldActualInfo        fai  = fais.Find(a => a.ActualField == field);

            if (fai == null)
            {
                RectangleF?ar = getFieldActualRectangle(field);
                fai = new FieldActualInfo(this, field, ar, field.ColumnOfTable != null ? getFoundFieldActualInfo(field.ColumnOfTable) : null);
                fais.Add(fai);
            }
            return(fai);
        }
        /// <summary>
        /// It is helpful when a field has more than 1 definition and its image is required.
        /// !!!Only must be used for the field returned by GetValue(string fieldName, out Template.Field actualField, Template.Field.Types? type = null)
        /// ATTENTION: using Template.Field as a parameter may be deceitful for these reasons:
        /// - it may belong to another template than ActiveTemplate;
        /// - it implies that a Template.Field object is equivalent to a field while it is just one of its defintions;
        /// - it may be the same by logic while being another as an object;
        /// </summary>
        /// <param name="exactField"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        //public Bitmap GetImage(Template.Field exactField)
        //{
        //    RectangleF? ar = getFieldActualRectangle(exactField);
        //    FieldActualInfo fai = new FieldActualInfo(this, exactField, ar, exactField.ColumnOfTable != null ? getFoundFieldActualInfo(exactField.ColumnOfTable) : null);
        //    if (!fai.Found)
        //        return null;
        //    return (Bitmap)fai.GetValue(Template.Field.Types.Image);
        //}

        internal FieldActualInfo GetFieldActualInfo(Template.Field field)
        {
            List <FieldActualInfo> fais = getFieldActualInfos(field.Name);
            //FieldActualInfo fai = fais.Find(a => a.ActualField == field);!!!while the fields can be same by logic, they are different objects!
            string          fs  = Serialization.Json.Serialize(field, false, true);
            FieldActualInfo fai = fais.Find(a => Serialization.Json.Serialize(a.ActualField, false, true) == fs);

            if (fai == null)
            {
                RectangleF?ar = getFieldActualRectangle(field);
                fai = new FieldActualInfo(this, field, ar, field.ColumnOfTable != null ? getFoundFieldActualInfo(field.ColumnOfTable) : null);
                fais.Add(fai);
            }
            return(fai);
        }
Exemple #9
0
        void setFieldRow(DataGridViewRow row, Template.Field f)
        {
            row.Tag = f;
            row.Cells["Name_"].Value          = f.Name;
            row.Cells["Rectangle"].Value      = Serialization.Json.Serialize(f.Rectangle);
            row.Cells["Type"].Value           = f.DefaultValueType;
            row.Cells["LeftAnchorId"].Value   = f.LeftAnchor?.Id;
            row.Cells["TopAnchorId"].Value    = f.TopAnchor?.Id;
            row.Cells["RightAnchorId"].Value  = f.RightAnchor?.Id;
            row.Cells["BottomAnchorId"].Value = f.BottomAnchor?.Id;

            if (loadingTemplate)
            {
                return;
            }

            if (row == currentFieldRow)
            {
                setCurrentFieldRow(row);
            }
        }
Exemple #10
0
 void deleteSelectedField()
 {
     try
     {
         if (fields.SelectedRows.Count < 1)
         {
             return;
         }
         DataGridViewRow r0 = fields.SelectedRows[fields.SelectedRows.Count - 1];
         if (r0.Tag == null)
         {
             return;
         }
         Template.Field f0     = (Template.Field)r0.Tag;
         bool           unique = true;
         foreach (DataGridViewRow rr in fields.Rows)
         {
             if (rr != r0 && rr.Tag != null && ((Template.Field)rr.Tag).Name == f0.Name)
             {
                 unique = false;
                 break;
             }
         }
         if (unique && !Message.YesNo("This field '" + f0.Name + "' will be removed completely because it is the last definition for it.\r\nProceed?", this, Message.Icons.Exclamation))
         {
             //Message.Inform("This field definition cannot be deleted because it is the last of the field.");
             return;
         }
         fields.Rows.Remove(r0);
     }
     catch (Exception e)
     {
         Log.Error(e);
         Message.Error(e, this);
     }
     finally
     {
         settingCurrentFieldRow = false;
     }
 }
Exemple #11
0
 void deleteSelectedField()
 {
     try
     {
         if (fields.SelectedRows.Count < 1)
         {
             return;
         }
         DataGridViewRow r0 = fields.SelectedRows[fields.SelectedRows.Count - 1];
         if (r0.Tag == null)
         {
             return;
         }
         Template.Field f0     = (Template.Field)r0.Tag;
         bool           unique = true;
         foreach (DataGridViewRow rr in fields.Rows)
         {
             if (rr != r0 && rr.Tag != null && ((Template.Field)rr.Tag).Name == f0.Name)
             {
                 unique = false;
                 break;
             }
         }
         if (unique)
         {
             Message.Inform("This field definition cannot be deleted because it is the last of the field.");
             return;
         }
         fields.Rows.Remove(r0);
     }
     catch (Exception e)
     {
         Win.LogMessage.Error(e);
     }
     finally
     {
         settingCurrentFieldRow = false;
     }
 }
Exemple #12
0
        void setFieldRow(DataGridViewRow row, Template.Field f)
        {
            row.Tag = f;
            row.Cells["Name_"].Value     = f.Name;
            row.Cells["Rectangle"].Value = Serialization.Json.Serialize(f.Rectangle);
            switch (f.Type)
            {
            case Template.Field.Types.PdfText:
                row.Cells["Type"].Value = Template.Field.Types.PdfText;
                break;

            case Template.Field.Types.OcrText:
                row.Cells["Type"].Value = Template.Field.Types.OcrText;
                break;

            case Template.Field.Types.ImageData:
                row.Cells["Type"].Value = Template.Field.Types.ImageData;
                break;

            default:
                throw new Exception("Unknown option: " + f.Type);
            }
            row.Cells["LeftAnchorId"].Value   = f.LeftAnchor?.Id;
            row.Cells["TopAnchorId"].Value    = f.TopAnchor?.Id;
            row.Cells["RightAnchorId"].Value  = f.RightAnchor?.Id;
            row.Cells["BottomAnchorId"].Value = f.BottomAnchor?.Id;

            if (loadingTemplate)
            {
                return;
            }

            if (row == currentFieldRow)
            {
                setCurrentFieldRow(row);
            }
        }
Exemple #13
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);
        }
 FieldActualInfo getFoundFieldActualInfo(string fieldName)
 {
     if (!fieldNames2fieldActualInfo.TryGetValue(fieldName, out FieldActualInfo fai))
     {
         RectangleF?    ar = null;
         Template.Field af = null;
         foreach (Template.Field f in PageCollection.ActiveTemplate.Fields.Where(x => x.Name == fieldName))
         {
             ar = getFieldActualRectangle(f);
             af = f;
             if (ar != null)
             {
                 break;
             }
         }
         if (af == null)
         {
             throw new Exception("Field[name=" + fieldName + "] does not exist.");
         }
         fai = new FieldActualInfo(this, af, ar, af.ColumnOfTable != null ? getFoundFieldActualInfo(af.ColumnOfTable) : null);
         fieldNames2fieldActualInfo[fieldName] = fai;
     }
     return(fai);
 }
Exemple #15
0
        void initializeFieldsTable()
        {
            LeftAnchorId.ValueType     = typeof(int);
            LeftAnchorId.ValueMember   = "Id";
            LeftAnchorId.DisplayMember = "Name";

            TopAnchorId.ValueType     = typeof(int);
            TopAnchorId.ValueMember   = "Id";
            TopAnchorId.DisplayMember = "Name";

            RightAnchorId.ValueType     = typeof(int);
            RightAnchorId.ValueMember   = "Id";
            RightAnchorId.DisplayMember = "Name";

            BottomAnchorId.ValueType     = typeof(int);
            BottomAnchorId.ValueMember   = "Id";
            BottomAnchorId.DisplayMember = "Name";

            Type.ValueType  = typeof(Template.Field.Types);
            Type.DataSource = Enum.GetValues(typeof(Template.Field.Types));

            fields.EnableHeadersVisualStyles = false;//needed to set row headers

            fields.DataError += delegate(object sender, DataGridViewDataErrorEventArgs e)
            {
                DataGridViewRow r = fields.Rows[e.RowIndex];
                Message.Error("fields[" + r.Index + "] has unacceptable value of " + fields.Columns[e.ColumnIndex].HeaderText + ":\r\n" + e.Exception.Message);
            };

            fields.UserDeletingRow += delegate(object sender, DataGridViewRowCancelEventArgs e)
            {
                if (fields.Rows.Count < 3 && fields.SelectedRows.Count > 0)
                {
                    fields.SelectedRows[0].Selected = false;//to avoid auto-creating row
                }
            };

            fields.RowsAdded += delegate(object sender, DataGridViewRowsAddedEventArgs e)
            {
            };

            fields.CellValueChanged += delegate(object sender, DataGridViewCellEventArgs e)
            {
                try
                {
                    if (loadingTemplate)
                    {
                        return;
                    }
                    if (e.ColumnIndex < 0)//row's header
                    {
                        return;
                    }
                    DataGridViewRow row = fields.Rows[e.RowIndex];
                    var             cs  = row.Cells;
                    Template.Field  f   = (Template.Field)row.Tag;
                    switch (fields.Columns[e.ColumnIndex].Name)
                    {
                    //case "Rectangle":
                    //    {
                    //        cs["Value"].Value = extractValueAndDrawSelectionBox(f.AnchorId, f.Rectangle, f.Type);
                    //        break;
                    //    }
                    case "Type":
                    {
                        Template.Field.Types t2 = (Template.Field.Types)row.Cells["Type"].Value;
                        if (t2 == f.Type)
                        {
                            break;
                        }
                        Template.Field f2;
                        switch (t2)
                        {
                        case Template.Field.Types.PdfText:
                            f2 = new Template.Field.PdfText();
                            break;

                        case Template.Field.Types.OcrText:
                            f2 = new Template.Field.OcrText();
                            break;

                        case Template.Field.Types.ImageData:
                            f2 = new Template.Field.ImageData();
                            break;

                        default:
                            throw new Exception("Unknown option: " + t2);
                        }
                        f2.Name         = f.Name;
                        f2.LeftAnchor   = f.LeftAnchor;
                        f2.TopAnchor    = f.TopAnchor;
                        f2.RightAnchor  = f.RightAnchor;
                        f2.BottomAnchor = f.BottomAnchor;
                        f2.Rectangle    = f.Rectangle;
                        f = f2;
                        setFieldRow(row, f);
                        break;
                    }

                    case "LeftAnchorId":
                    {
                        int?ai = (int?)cs["LeftAnchorId"].Value;
                        if (ai == null)
                        {
                            f.LeftAnchor = null;
                        }
                        else
                        {
                            Page.AnchorActualInfo aai = pages[currentPageI].GetAnchorActualInfo((int)ai);
                            f.LeftAnchor = new Template.Field.SideAnchor
                            {
                                Id    = (int)ai,
                                Shift = aai.Shift.Width,
                            };
                        }
                        setFieldRow(row, f);
                    }
                    break;

                    case "TopAnchorId":
                    {
                        int?ai = (int?)cs["TopAnchorId"].Value;
                        if (ai == null)
                        {
                            f.TopAnchor = null;
                        }
                        else
                        {
                            Page.AnchorActualInfo aai = pages[currentPageI].GetAnchorActualInfo((int)ai);
                            f.TopAnchor = new Template.Field.SideAnchor
                            {
                                Id    = (int)ai,
                                Shift = aai.Shift.Height,
                            };
                        }
                        setFieldRow(row, f);
                    }
                    break;

                    case "RightAnchorId":
                    {
                        int?ai = (int?)cs["RightAnchorId"].Value;
                        if (ai == null)
                        {
                            f.RightAnchor = null;
                        }
                        else
                        {
                            Page.AnchorActualInfo aai = pages[currentPageI].GetAnchorActualInfo((int)ai);
                            f.RightAnchor = new Template.Field.SideAnchor
                            {
                                Id    = (int)ai,
                                Shift = aai.Shift.Width,
                            };
                        }
                        setFieldRow(row, f);
                    }
                    break;

                    case "BottomAnchorId":
                    {
                        int?ai = (int?)cs["BottomAnchorId"].Value;
                        if (ai == null)
                        {
                            f.BottomAnchor = null;
                        }
                        else
                        {
                            Page.AnchorActualInfo aai = pages[currentPageI].GetAnchorActualInfo((int)ai);
                            f.BottomAnchor = new Template.Field.SideAnchor
                            {
                                Id    = (int)ai,
                                Shift = aai.Shift.Height,
                            };
                        }
                        setFieldRow(row, f);
                    }
                    break;

                    case "Name_":
                        f.Name = (string)row.Cells["Name_"].Value;
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Message.Error2(ex);
                }
            };

            fields.CurrentCellDirtyStateChanged += delegate
            {
                if (fields.IsCurrentCellDirty)
                {
                    fields.CommitEdit(DataGridViewDataErrorContexts.Commit);
                }
            };

            fields.RowValidating += delegate(object sender, DataGridViewCellCancelEventArgs e)
            {
                DataGridViewRow r = fields.Rows[e.RowIndex];
                try
                {
                    if (r.Tag != null)
                    {
                        string n = FieldPreparation.Normalize((string)r.Cells["Name_"].Value);
                        if (string.IsNullOrWhiteSpace(n))
                        {
                            throw new Exception("Name cannot be empty!");
                        }
                        //foreach (DataGridViewRow rr in fields.Rows)
                        //{
                        //    if (r == rr)
                        //        continue;
                        //    Template.Field f = (Template.Field)rr.Tag;
                        //    if (f != null && n == f.Name)
                        //        throw new Exception("Name '" + n + "' is duplicated!");
                        //}
                        r.Cells["Name_"].Value = n;
                    }
                }
                catch (Exception ex)
                {
                    //Log.Message.Error("Name", ex);
                    Message.Error2(ex);
                    e.Cancel = true;
                }
            };

            fields.DefaultValuesNeeded += delegate(object sender, DataGridViewRowEventArgs e)
            {
            };

            fields.CellContentClick += delegate(object sender, DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex < 0)//row's header
                {
                    return;
                }
                switch (fields.Columns[e.ColumnIndex].Name)
                {
                case "Ocr":
                    fields.EndEdit();
                    break;
                }
            };

            fields.SelectionChanged += delegate(object sender, EventArgs e)
            {
                try
                {
                    if (loadingTemplate)
                    {
                        return;
                    }

                    if (settingCurrentFieldRow)
                    {
                        return;
                    }

                    if (fields.SelectedRows.Count < 1)
                    {
                        return;
                    }
                    DataGridViewRow row = fields.SelectedRows[0];
                    Template.Field  f   = (Template.Field)row.Tag;
                    if (f == null)//hacky forcing commit a newly added row and display the blank row
                    {
                        int i = fields.Rows.Add();
                        row = fields.Rows[i];
                        f   = templateManager.CreateDefaultField();
                        setFieldRow(row, f);
                        row.Selected = true;
                        return;
                    }
                    setCurrentFieldRow(row);
                }
                catch (Exception ex)
                {
                    Log.Message.Error(ex);
                }
            };

            copy2ClipboardField.LinkClicked += delegate
            {
                if (fields.SelectedRows.Count < 1)
                {
                    return;
                }
                DataGridViewRow r = fields.SelectedRows[fields.SelectedRows.Count - 1];
                if (r.Tag == null)
                {
                    return;
                }
                Template.Field f = (Template.Field)r.Tag;
                object         o = pages[currentPageI].GetValue(f.Name);
                switch (f.Type)
                {
                case Template.Field.Types.ImageData:
                    Clipboard.SetData(f.Type.ToString(), (Image)o);
                    break;

                case Template.Field.Types.PdfText:
                case Template.Field.Types.OcrText:
                    Clipboard.SetText((string)o);
                    break;

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

            duplicateField.LinkClicked += delegate
            {
                try
                {
                    if (fields.SelectedRows.Count < 1)
                    {
                        return;
                    }
                    DataGridViewRow r0 = fields.SelectedRows[fields.SelectedRows.Count - 1];
                    if (r0.Tag == null)
                    {
                        return;
                    }
                    Template.Field         f0 = (Template.Field)r0.Tag;
                    List <DataGridViewRow> cloningFieldRows = new List <DataGridViewRow> {
                        r0
                    };
                    //if((f0 as Template.Field.PdfText)?.ColumnOfTable!=null)
                    //{
                    //    Message.Exclaim("This field is a column of "+ (f0 as Template.Field.PdfText)?.ColumnOfTable + " so you should create a new definition of it.");
                    //    return;
                    //}
                    //foreach (DataGridViewRow r in fields.Rows)
                    //    if ((r.Tag as Template.Field.PdfText)?.ColumnOfTable == f0.Name)
                    //        cloningFieldRows.Add(r);
                    if (f0.ColumnOfTable != null)
                    {
                        if (!Message.YesNo("This field is a column of table " + f0.ColumnOfTable + " and so new definions will be added to the rest column fields as well. Proceed?"))
                        {
                            return;
                        }
                        foreach (DataGridViewRow r in fields.Rows)
                        {
                            if (r != r0 && (r.Tag as Template.Field)?.ColumnOfTable == f0.ColumnOfTable)
                            {
                                string fn = (r.Tag as Template.Field)?.Name;
                                if (cloningFieldRows.Find(x => (x.Tag as Template.Field)?.Name == fn) == null)
                                {
                                    cloningFieldRows.Add(r);
                                }
                            }
                        }
                    }
                    settingCurrentFieldRow = true;//required due to fields-column error calculation when selected row changes
                    foreach (DataGridViewRow row in cloningFieldRows)
                    {
                        Template.Field f = (Template.Field)Serialization.Json.Clone(((Template.Field)row.Tag).GetType(), row.Tag);
                        f.LeftAnchor   = null;
                        f.TopAnchor    = null;
                        f.RightAnchor  = null;
                        f.BottomAnchor = null;
                        int             i = fields.Rows.Add();
                        DataGridViewRow r = fields.Rows[i];
                        setFieldRow(r, f);
                        fields.Rows.Remove(r);
                        fields.Rows.Insert(row.Index + 1, r);
                    }
                }
                catch (Exception e)
                {
                    Log.Message.Error(e);
                }
                finally
                {
                    settingCurrentFieldRow = false;
                }
            };
            //duplicateField.LinkClicked += delegate
            //{
            //    if (fields.SelectedRows.Count < 1)
            //        return;
            //    DataGridViewRow r0 = fields.SelectedRows[fields.SelectedRows.Count - 1];
            //    if (r0.Tag == null)
            //        return;
            //    Template.Field f0 = (Template.Field)r0.Tag;
            //    if (f0.ColumnOfTable != null)
            //    {
            //        Message.Exclaim("This field is a column of table " + f0.ColumnOfTable + " so you should create a new definition of it.");
            //        return;
            //    }
            //    List<DataGridViewRow> cloningFieldRows = new List<DataGridViewRow> { r0 };
            //    //foreach (DataGridViewRow r in fields.Rows)
            //    //    if (r != r0 && (r.Tag as Template.Field)?.ColumnOfTable == f0.Name)
            //    //    {
            //    //        string fn = (r.Tag as Template.Field)?.Name;
            //    //        if (cloningFieldRows.Find(x => (x.Tag as Template.Field)?.Name == fn) == null)
            //    //            cloningFieldRows.Add(r);
            //    //    }

            //    settingCurrentFieldRow = true;//required due to fields-column error calculation when selected row changes
            //    foreach (DataGridViewRow row in cloningFieldRows)
            //    {
            //        Template.Field f = (Template.Field)Serialization.Json.Clone(((Template.Field)row.Tag).GetType(), row.Tag);
            //        f.LeftAnchor = null;
            //        f.TopAnchor = null;
            //        f.RightAnchor = null;
            //        f.BottomAnchor = null;
            //        int i = fields.Rows.Add();
            //        DataGridViewRow r = fields.Rows[i];
            //        setFieldRow(r, f);
            //        fields.Rows.Remove(r);
            //        fields.Rows.Insert(row.Index + 1, r);
            //    }
            //    settingCurrentFieldRow = false;
            //};

            deleteField.LinkClicked += delegate
            {
                try
                {
                    if (fields.SelectedRows.Count < 1)
                    {
                        return;
                    }
                    DataGridViewRow r0 = fields.SelectedRows[fields.SelectedRows.Count - 1];
                    if (r0.Tag == null)
                    {
                        return;
                    }
                    Template.Field f0     = (Template.Field)r0.Tag;
                    bool           unique = true;
                    foreach (DataGridViewRow rr in fields.Rows)
                    {
                        if (rr != r0 && rr.Tag != null && ((Template.Field)rr.Tag).Name == f0.Name)
                        {
                            unique = false;
                            break;
                        }
                    }
                    if (unique)
                    {
                        Message.Inform("This field definition cannot be deleted because it is the last of the field.");
                        return;
                    }
                    List <DataGridViewRow> deletingFieldRows = new List <DataGridViewRow> {
                        r0
                    };
                    if (f0.ColumnOfTable != null)
                    {
                        if (!Message.YesNo("This field is a column of table " + f0.ColumnOfTable + " and so the respective definitions of the rest column fields will be deleted as well. Proceed?"))
                        {
                            return;
                        }
                        Dictionary <string, List <DataGridViewRow> > fieldName2orderedRows = new Dictionary <string, List <DataGridViewRow> >();
                        foreach (DataGridViewRow r in fields.Rows)
                        {
                            if ((r.Tag as Template.Field.PdfText)?.ColumnOfTable == f0.ColumnOfTable)
                            {
                                List <DataGridViewRow> rs;
                                string fn = (r.Tag as Template.Field)?.Name;
                                if (!fieldName2orderedRows.TryGetValue(fn, out rs))
                                {
                                    rs = new List <DataGridViewRow>();
                                    fieldName2orderedRows[fn] = rs;
                                }
                                rs.Add(r);
                            }
                        }
                        int definitionIndex = fieldName2orderedRows[f0.Name].IndexOf(r0);
                        fieldName2orderedRows.Remove(f0.Name);
                        foreach (List <DataGridViewRow> rs in fieldName2orderedRows.Values)
                        {
                            deletingFieldRows.Add(rs[definitionIndex]);
                        }
                    }
                    settingCurrentFieldRow = true;//required due to fields-column error calculation when selected row changes
                    foreach (DataGridViewRow row in deletingFieldRows)
                    {
                        fields.Rows.Remove(row);
                    }
                }
                catch (Exception e)
                {
                    Log.Message.Error(e);
                }
                finally
                {
                    settingCurrentFieldRow = false;
                }
            };
            //deleteField.LinkClicked += delegate
            //{
            //    if (fields.SelectedRows.Count < 1)
            //        return;
            //    DataGridViewRow r0 = fields.SelectedRows[fields.SelectedRows.Count - 1];
            //    if (r0.Tag == null)
            //        return;
            //    Template.Field f0 = (Template.Field)r0.Tag;
            //    bool unique = true;
            //    foreach (DataGridViewRow rr in fields.Rows)
            //        if (rr != r0 && rr.Tag != null && ((Template.Field)rr.Tag).Name == f0.Name)
            //        {
            //            unique = false;
            //            break;
            //        }
            //    if (unique)
            //    {
            //        Message.Inform("This field definition cannot be deleted because it is the last of the field.");
            //        return;
            //    }
            //    if (f0.ColumnOfTable != null)
            //    {
            //        Message.Exclaim("This field is a column of table " + f0.ColumnOfTable + " so you should delete the respectivea definition of it.");
            //        return;
            //    }
            //    Dictionary<string, List<DataGridViewRow>> fieldName2orderedRows = new Dictionary<string, List<DataGridViewRow>>();
            //    foreach (DataGridViewRow r in fields.Rows)
            //        if ((r.Tag as Template.Field.PdfText)?.ColumnOfTable == f0.Name)
            //        {
            //            List<DataGridViewRow> rs;
            //            string fn = (r.Tag as Template.Field)?.Name;
            //            if (!fieldName2orderedRows.TryGetValue(fn, out rs))
            //            {
            //                rs = new List<DataGridViewRow>();
            //                fieldName2orderedRows[fn] = rs;
            //            }
            //            rs.Add(r);
            //        }

            //    int definitionIndex = fieldName2orderedRows[f0.Name].IndexOf(r0);
            //    fieldName2orderedRows.Remove(f0.Name);
            //    List<DataGridViewRow> deletingFieldRows = new List<DataGridViewRow> { r0 };
            //    foreach (List<DataGridViewRow> rs in fieldName2orderedRows.Values)
            //        deletingFieldRows.Add(rs[definitionIndex]);

            //    settingCurrentFieldRow = true;//required due to fields-column error calculation when selected row changes
            //    foreach (DataGridViewRow row in deletingFieldRows)
            //        fields.Rows.Remove(row);
            //    settingCurrentFieldRow = false;
            //};

            moveUpField.LinkClicked += delegate
            {
                try
                {
                    if (fields.SelectedRows.Count < 1)
                    {
                        return;
                    }
                    DataGridViewRow r  = fields.SelectedRows[fields.SelectedRows.Count - 1];
                    int             i2 = r.Index - 1;
                    if (i2 < 0)
                    {
                        return;
                    }
                    int minI = int.MaxValue;
                    foreach (DataGridViewRow rr in fields.Rows)
                    {
                        if (rr != r && rr.Tag != null && ((Template.Field)rr.Tag).Name == ((Template.Field)r.Tag).Name && rr.Index < minI)
                        {
                            minI = rr.Index;
                        }
                    }
                    if (i2 < minI)
                    {
                        return;
                    }
                    settingCurrentFieldRow = true;//required due to fields-column error calculation when selected row changes
                    fields.Rows.Remove(r);
                    fields.Rows.Insert(i2, r);
                    settingCurrentFieldRow = false;
                    r.Selected             = true;
                }
                catch (Exception e)
                {
                    Log.Message.Error(e);
                }
                finally
                {
                    settingCurrentFieldRow = false;
                }
            };

            moveDownField.LinkClicked += delegate
            {
                try
                {
                    if (fields.SelectedRows.Count < 1)
                    {
                        return;
                    }
                    DataGridViewRow r  = fields.SelectedRows[fields.SelectedRows.Count - 1];
                    int             i2 = r.Index + 1;
                    if (i2 > fields.Rows.Count - 1)
                    {
                        return;
                    }
                    int maxI = 0;
                    foreach (DataGridViewRow rr in fields.Rows)
                    {
                        if (rr != r && rr.Tag != null && ((Template.Field)rr.Tag).Name == ((Template.Field)r.Tag).Name && rr.Index > maxI)
                        {
                            maxI = rr.Index;
                        }
                    }
                    if (i2 > maxI + 1)
                    {
                        return;
                    }
                    settingCurrentFieldRow = true;//required due to fields-column error calculation when selected row changes
                    fields.Rows.Remove(r);
                    fields.Rows.Insert(i2, r);
                    settingCurrentFieldRow = false;
                    r.Selected             = true;
                }
                catch (Exception e)
                {
                    Log.Message.Error(e);
                }
                finally
                {
                    settingCurrentFieldRow = false;
                }
            };
        }
        internal Template GetTemplateFromUI(bool saving)
        {
            Template t = new Template();

            if (saving && string.IsNullOrWhiteSpace(name.Text))
            {
                throw new Exception("Name is empty!");
            }

            t.Name = name.Text.Trim();

            t.TextAutoInsertSpace = new TextAutoInsertSpace
            {
                Threshold      = (float)textAutoInsertSpaceThreshold.Value,
                Representative = Regex.Unescape(textAutoInsertSpaceRepresentative.Text),
            };

            t.CvImageScalePyramidStep = (int)CvImageScalePyramidStep.Value;
            t.TesseractPageSegMode    = (Tesseract.PageSegMode)TesseractPageSegMode.SelectedItem;

            if (SingleFieldFromFieldImage.Checked)
            {
                t.FieldOcrMode |= Template.FieldOcrModes.SingleFieldFromFieldImage;
            }
            else
            {
                t.FieldOcrMode &= ~Template.FieldOcrModes.SingleFieldFromFieldImage;
            }
            if (ColumnFieldFromFieldImage.Checked)
            {
                t.FieldOcrMode |= Template.FieldOcrModes.ColumnFieldFromFieldImage;
            }
            else
            {
                t.FieldOcrMode &= ~Template.FieldOcrModes.ColumnFieldFromFieldImage;
            }

            bitmapPreparationForm.SetTemplate(t);

            bool?      removeNotLinkedAnchors = null;
            List <int> conditionAnchorIds     = null;

            if (saving)
            {
                conditionAnchorIds = new List <int>();
                foreach (DataGridViewRow r in conditions.Rows)
                {
                    Template.Condition c = (Template.Condition)r.Tag;
                    if (c != null && c.IsSet())
                    {
                        conditionAnchorIds.AddRange(BooleanEngine.GetAnchorIds(c.Value));
                    }
                }
                conditionAnchorIds = conditionAnchorIds.Distinct().ToList();
            }
            t.Anchors = new List <Template.Anchor>();
            foreach (DataGridViewRow r in anchors.Rows)
            {
                Template.Anchor a = (Template.Anchor)r.Tag;
                if (a == null)
                {
                    continue;
                }

                if (saving)
                {
                    if (!a.IsSet())
                    {
                        throw new Exception("Anchor[Id=" + a.Id + "] is not set!");
                    }

                    if (!conditionAnchorIds.Contains(a.Id))
                    {
                        bool engaged = false;
                        foreach (DataGridViewRow rr in anchors.Rows)
                        {
                            Template.Anchor a_ = (Template.Anchor)rr.Tag;
                            if (a_ == null)
                            {
                                continue;
                            }
                            if (a_.ParentAnchorId == a.Id)
                            {
                                engaged = true;
                                break;
                            }
                        }
                        if (!engaged)
                        {
                            foreach (DataGridViewRow rr in fields.Rows)
                            {
                                Template.Field m = (Template.Field)rr.Tag;
                                if (m != null && (m.LeftAnchor?.Id == a.Id || m.TopAnchor?.Id == a.Id || m.RightAnchor?.Id == a.Id || m.BottomAnchor?.Id == a.Id))
                                {
                                    engaged = true;
                                    break;
                                }
                            }
                            if (!engaged)
                            {
                                if (a.Id != t.ScalingAnchorId)
                                {
                                    if (removeNotLinkedAnchors == null)
                                    {
                                        removeNotLinkedAnchors = Message.YesNo("The template contains not linked anchor[s]. Remove them?");
                                    }
                                    if (removeNotLinkedAnchors == true)
                                    {
                                        continue;
                                    }
                                }
                            }
                        }
                    }
                }
                t.Anchors.Add((Template.Anchor)Serialization.Json.Clone2(a));
            }
            t.Anchors = t.Anchors.OrderBy(a => a.Id).ToList();

            if (saving)
            {
                t.GetScalingAnchor();//check is it is correct;
            }
            t.Conditions = new List <Template.Condition>();
            foreach (DataGridViewRow r in conditions.Rows)
            {
                Template.Condition c = (Template.Condition)r.Tag;
                if (c == null)
                {
                    continue;
                }
                if (saving)
                {
                    if (!c.IsSet())
                    {
                        throw new Exception("Condition[name=" + c.Name + "] is not set!");
                    }
                    BooleanEngine.Check(c.Value, t.Anchors.Select(x => x.Id));
                }
                t.Conditions.Add(Serialization.Json.Clone(c));
            }
            if (saving)
            {
                var dcs = t.Conditions.GroupBy(x => x.Name).Where(x => x.Count() > 1).FirstOrDefault();
                if (dcs != null)
                {
                    throw new Exception("Condition '" + dcs.First().Name + "' is duplicated!");
                }
            }

            t.Fields = new List <Template.Field>();
            foreach (DataGridViewRow r in fields.Rows)
            {
                Template.Field f = (Template.Field)r.Tag;
                if (f == null)
                {
                    continue;
                }
                if (saving && !f.IsSet())
                {
                    throw new Exception("Field[" + r.Index + "] is not set!");
                }
                if (saving)
                {
                    foreach (int?ai in new List <int?> {
                        f.LeftAnchor?.Id, f.TopAnchor?.Id, f.RightAnchor?.Id, f.BottomAnchor?.Id
                    })
                    {
                        if (ai != null && t.Anchors.FirstOrDefault(x => x.Id == ai) == null)
                        {
                            throw new Exception("Anchor[Id=" + ai + " does not exist.");
                        }
                    }
                }
                t.Fields.Add(Serialization.Json.Clone(f));
            }
            if (saving)
            {
                if (t.Fields.Count < 1)
                {
                    throw new Exception("Fields is empty!");
                }
                //var dfs = t.Fields.GroupBy(x => x.Name).Where(x => x.Count() > 1).FirstOrDefault();
                //if (dfs != null)
                //    throw new Exception("Field '" + dfs.First().Name + "' is duplicated!");

                //foreach (string columnOfTable in t.Fields.Where(x => x is Template.Field.PdfText).Select(x => (Template.Field.PdfText)x).Where(x => x.ColumnOfTable != null).Select(x => x.ColumnOfTable))
                //{
                //    Dictionary<string, List<Template.Field>> fieldName2orderedFields = new Dictionary<string, List<Template.Field>>();
                //    foreach (Template.Field.PdfText pt in t.Fields.Where(x => x is Template.Field.PdfText).Select(x => (Template.Field.PdfText)x).Where(x => x.ColumnOfTable == columnOfTable))
                //    {
                //        List<Template.Field> fs;
                //        if (!fieldName2orderedFields.TryGetValue(pt.Name, out fs))
                //        {
                //            fs = new List<Template.Field>();
                //            fieldName2orderedFields[pt.Name] = fs;
                //        }
                //        fs.Add(pt);
                //    }
                //    int definitionCount = fieldName2orderedFields.Max(x => x.Value.Count());
                //    foreach (string fn in fieldName2orderedFields.Keys)
                //        if (definitionCount > fieldName2orderedFields[fn].Count)
                //            throw new Exception("Field '" + fn + "' is column of table " + columnOfTable + " and so it must have the same number of definitions as the rest column fields!");
                //}

                //foreach (Template.Field f0 in t.Fields)
                //{
                //    int tableDefinitionsCount = t.Fields.Where(x => x.Name == f0.Name).Count();
                //    string fn = t.Fields.Where(x => x.ColumnOfTable == f0.Name).GroupBy(x => x.Name).Where(x => x.Count() > tableDefinitionsCount).FirstOrDefault()?.First().Name;
                //    if (fn != null)
                //        throw new Exception("Field '" + fn + "' is a column of table field " + f0.Name + " so " + f0.Name + " must have number of definitions not less then that of " + fn + ".");
                //}

                //it is done only to avoid unneeded OCR
                //string fn = t.Fields.Where(x => x.ColumnOfTable != null && (x.DefaultValueType != Template.Field.ValueTypes.PdfText && x.DefaultValueType != Template.Field.ValueTypes.PdfTextLines)).FirstOrDefault()?.Name;
                //if (fn != null)
                //    throw new Exception("Field '" + fn + "' is a column of a table field. Its default value type can be either " + Template.Field.ValueTypes.PdfText + " or " + Template.Field.ValueTypes.PdfTextLines);
            }

            if (saving)
            {
                t.Editor = new Template.EditorSettings
                {
                    TestFile         = testFile.Text,
                    TestPictureScale = pictureScale.Value,
                    ExtractFieldsAutomaticallyWhenPageChanged = ExtractFieldsAutomaticallyWhenPageChanged.Checked,
                    ShowFieldTextLineSeparators = ShowFieldTextLineSeparators.Checked,
                    CheckConditionsAutomaticallyWhenPageChanged = CheckConditionsAutomaticallyWhenPageChanged.Checked,
                };
            }

            return(t);
        }
        void onAnchorsChanged()
        {
            SortedSet <int>        ais = new SortedSet <int>();
            List <Template.Anchor> as_ = new List <Template.Anchor>();

            foreach (DataGridViewRow r in anchors.Rows)
            {
                if (r.Tag != null)
                {
                    Template.Anchor a = (Template.Anchor)r.Tag;
                    as_.Add(a);
                    if (a.Id > 0)
                    {
                        ais.Add(a.Id);
                    }
                }
            }

            foreach (DataGridViewRow r in anchors.Rows)
            {
                if (r.Tag == null)
                {
                    continue;
                }
                Template.Anchor a = (Template.Anchor)r.Tag;
                if (/*a.IsSet() &&*/ a.Id <= 0)
                {
                    a.Id = 1;
                    //if (ais.Count > 0)
                    //    anchorId = ais.Max() + 1;
                    foreach (int i in ais)
                    {
                        if (a.Id < i)
                        {
                            break;
                        }
                        if (a.Id == i)
                        {
                            a.Id++;
                        }
                    }
                    ais.Add(a.Id);
                    r.Cells["Id3"].Value = a.Id;
                }
            }

            foreach (DataGridViewRow r in anchors.Rows)
            {
                if (r.Tag == null)
                {
                    continue;
                }
                Template.Anchor a = (Template.Anchor)r.Tag;
                if (a.ParentAnchorId == null)
                {
                    continue;
                }
                if (!ais.Contains((int)a.ParentAnchorId))
                {
                    r.Cells["ParentAnchorId3"].Value = null;
                }
            }

            foreach (DataGridViewRow r in fields.Rows)
            {
                if (r.Tag == null)
                {
                    continue;
                }
                Template.Field f = (Template.Field)r.Tag;
                if (f.LeftAnchor != null && !ais.Contains(f.LeftAnchor.Id))
                {
                    r.Cells["LeftAnchorId"].Value = null;
                }
                if (f.TopAnchor != null && !ais.Contains(f.TopAnchor.Id))
                {
                    r.Cells["TopAnchorId"].Value = null;
                }
                if (f.RightAnchor != null && !ais.Contains(f.RightAnchor.Id))
                {
                    r.Cells["RightAnchorId"].Value = null;
                }
                if (f.BottomAnchor != null && !ais.Contains(f.BottomAnchor.Id))
                {
                    r.Cells["BottomAnchorId"].Value = null;
                }
            }

            setConditionsStatus();

            {
                List <dynamic> ais_ = ais.Select(x => new { Id = x, Name = x.ToString() }).ToList <dynamic>();
                ais_.Insert(0, new { Id = -1, Name = string.Empty });//commbobox returns value null for -1 (and throws an unclear expection if Id=null)
                LeftAnchorId.DataSource   = ais_;
                TopAnchorId.DataSource    = ais_;
                RightAnchorId.DataSource  = ais_;
                BottomAnchorId.DataSource = ais_;
            }
        }
Exemple #18
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);
        }
        ///// <summary>
        ///// !!!passing Template.Field is deceitful for 2 reasons:
        ///// - it may belong to another template than ActiveTemplate;
        ///// - it implies that a Template.Field object is equivalent to a field while it is a defintion;
        ///// </summary>
        ///// <param name="field"></param>
        ///// <returns></returns>
        //public object GetValue(Template.Field exactField, Template.Field.ValueTypes? valueType = null)
        //{
        //    RectangleF? ar = getFieldActualRectangle(exactField);
        //    FieldActualInfo fai = new FieldActualInfo(this, exactField, ar, exactField.ColumnOfTable != null ? getFoundFieldActualInfo(exactField.ColumnOfTable) : null);
        //    if (!fai.Found)
        //        return null;
        //    return fai.GetValue(valueType == null ? fai.ActualField.DefaultValueType : (Template.Field.ValueTypes)valueType);
        //}

        internal FieldActualInfo GetFieldActualInfo(Template.Field field)
        {
            return(new FieldActualInfo(this, field, getFieldActualRectangle(field), field.ColumnOfTable != null ? getFoundFieldActualInfo(field.ColumnOfTable) : null));
        }
Exemple #20
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();
                }
            };
        }
Exemple #21
0
 void duplicateSelectedField()
 {
     try
     {
         if (fields.SelectedRows.Count < 1)
         {
             return;
         }
         DataGridViewRow r0 = fields.SelectedRows[fields.SelectedRows.Count - 1];
         if (r0.Tag == null)
         {
             return;
         }
         Template.Field         f0 = (Template.Field)r0.Tag;
         List <DataGridViewRow> cloningFieldRows = new List <DataGridViewRow> {
             r0
         };
         //if((f0 as Template.Field.PdfText)?.ColumnOfTable!=null)
         //{
         //    Message.Exclaim("This field is a column of "+ (f0 as Template.Field.PdfText)?.ColumnOfTable + " so you should create a new definition of it.");
         //    return;
         //}
         //foreach (DataGridViewRow r in fields.Rows)
         //    if ((r.Tag as Template.Field.PdfText)?.ColumnOfTable == f0.Name)
         //        cloningFieldRows.Add(r);
         if (f0.ColumnOfTable != null &&
             Message.YesNo("This field is a column of table " + f0.ColumnOfTable + ".\r\nWould you like new definions to be created for all the column fields of the table?")
             )
         {
             foreach (DataGridViewRow r in fields.Rows)
             {
                 if (r != r0 && (r.Tag as Template.Field)?.ColumnOfTable == f0.ColumnOfTable)
                 {
                     string fn = (r.Tag as Template.Field)?.Name;
                     if (cloningFieldRows.Find(x => (x.Tag as Template.Field)?.Name == fn) == null)
                     {
                         cloningFieldRows.Add(r);
                     }
                 }
             }
         }
         settingCurrentFieldRow = true;//required due to fields-column error calculation when selected row changes
         foreach (DataGridViewRow row in cloningFieldRows)
         {
             Template.Field f = (Template.Field)Serialization.Json.Clone(((Template.Field)row.Tag).GetType(), row.Tag);
             f.LeftAnchor   = null;
             f.TopAnchor    = null;
             f.RightAnchor  = null;
             f.BottomAnchor = null;
             int             i = fields.Rows.Add();
             DataGridViewRow r = fields.Rows[i];
             setFieldRow(r, f);
             fields.Rows.Remove(r);
             fields.Rows.Insert(row.Index + 1, r);
         }
     }
     catch (Exception e)
     {
         Win.LogMessage.Error(e);
     }
     finally
     {
         settingCurrentFieldRow = false;
     }
 }
        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();
                }
            };
        }
Exemple #24
0
        object setFieldRowValue(DataGridViewRow row, bool setEmpty)
        {
            Template.Field f = (Template.Field)row.Tag;
            if (f == null)
            {
                return(null);
            }
            if (!f.IsSet())
            {
                setRowStatus(statuses.WARNING, row, "Not set");
                return(null);
            }
            DataGridViewCell valueCell = row.Cells["Value"];

            if (valueCell.Value != null && valueCell.Value is IDisposable)
            {
                ((IDisposable)valueCell.Value).Dispose();
            }
            if (f is Template.Field.Image || f is Template.Field.OcrTextLineImages)
            {
                if (!(valueCell is DataGridViewImageCell))
                {
                    valueCell.Dispose();
                    valueCell          = new DataGridViewImageCell();
                    row.Cells["Value"] = valueCell;
                }
            }
            else
            {
                if (valueCell is DataGridViewImageCell)
                {
                    valueCell.Dispose();
                    valueCell          = new DataGridViewTextBoxCell();
                    row.Cells["Value"] = valueCell;
                }
            }
            if (setEmpty)
            {
                valueCell.Value = null;
                setRowStatus(statuses.NEUTRAL, row, "");
                return(null);
            }
            clearImageFromBoxes();
            object v = extractFieldAndDrawSelectionBox(f);

            if (v != null)
            {
                switch (f.Type)
                {
                case Template.Field.Types.PdfText:
                    valueCell.Value = Page.NormalizeText((string)v);
                    break;

                case Template.Field.Types.PdfTextLines:
                    valueCell.Value = Page.NormalizeText(string.Join("\r\n", (List <string>)v));
                    break;

                case Template.Field.Types.PdfCharBoxs:
                    valueCell.Value = Page.NormalizeText(Serialization.Json.Serialize(v));
                    break;

                case Template.Field.Types.OcrText:
                    valueCell.Value = Page.NormalizeText((string)v);
                    break;

                case Template.Field.Types.OcrTextLines:
                    valueCell.Value = Page.NormalizeText(string.Join("\r\n", (List <string>)v));
                    break;

                case Template.Field.Types.OcrCharBoxs:
                    valueCell.Value = Page.NormalizeText(Serialization.Json.Serialize(v));
                    break;

                case Template.Field.Types.Image:
                {
                    Bitmap b = (Bitmap)v;
                    Size   s = valueCell.Size;
                    if (s.Height < b.Height * pictureScale.Value)
                    {
                        s.Width = int.MaxValue;
                        b       = Win.ImageRoutines.GetScaled(b, s);
                    }
                    else if (pictureScale.Value != 1)
                    {
                        b = Win.ImageRoutines.GetScaled(b, (float)pictureScale.Value);
                    }
                    valueCell.Value = b;
                    break;
                }

                case Template.Field.Types.OcrTextLineImages:
                {
                    List <Bitmap> bs = (List <Bitmap>)v;
                    if (bs.Count < 1)
                    {
                        break;
                    }
                    Bitmap b = bs[0];
                    Size   s = valueCell.Size;
                    if (s.Height < b.Height * pictureScale.Value)
                    {
                        s.Width = int.MaxValue;
                        b       = Win.ImageRoutines.GetScaled(b, s);
                    }
                    else if (pictureScale.Value != 1)
                    {
                        b = Win.ImageRoutines.GetScaled(b, (float)pictureScale.Value);
                    }
                    valueCell.Value = b;
                    break;
                }

                default:
                    throw new Exception("Unknown option: " + f.Type);
                }
            }
            else
            {
                valueCell.Value = v;
            }

            if (valueCell.Value != null)
            {
                setRowStatus(statuses.SUCCESS, row, "Found");
            }
            else
            {
                setRowStatus(statuses.ERROR, row, "Not found");
            }

            return(v);
        }
Exemple #25
0
        void setCurrentFieldRow(DataGridViewRow row)
        {
            if (settingCurrentFieldRow)
            {
                return;
            }
            try
            {
                settingCurrentFieldRow = true;
                //if (row == currentFieldRow)
                //    return;
                currentFieldRow = row;

                if (row == null)
                {
                    fields.ClearSelection();
                    fields.CurrentCell  = null;
                    currentFieldControl = null;
                    return;
                }

                if (fields.CurrentCell?.RowIndex != row.Index)
                {
                    fields.CurrentCell = fields[0, row.Index];
                }
                //Template.Field f = (Template.Field)row.Tag;
                //setCurrentAnchorRow(f.LeftAnchorId, true);
                //setCurrentAnchorRow(f.TopAnchorId, false);
                //setCurrentAnchorRow(f.RightAnchorId, false);
                //setCurrentAnchorRow(f.BottomAnchorId, false);
                setCurrentAnchorRow(null, true);
                setCurrentConditionRow(null);
                object         v = setFieldRowValue(row, false);
                Template.Field f = (Template.Field)row.Tag;

                //List<Template.Field> fs = new List<Template.Field>();
                //foreach (DataGridViewRow r in fields.Rows)
                //{
                //    if (r.Tag == null)
                //        continue;
                //    fs.Add((Template.Field)r.Tag);
                //}

                Template.Field.Types t = ((Template.Field)row.Tag).Type;
                switch (t)
                {
                case Template.Field.Types.PdfText:
                    currentFieldControl = new FieldPdfTextControl();
                    break;

                case Template.Field.Types.PdfTextLines:
                    currentFieldControl = new FieldPdfTextLinesControl();
                    break;

                case Template.Field.Types.PdfCharBoxs:
                    currentFieldControl = new FieldPdfCharBoxsControl();
                    break;

                case Template.Field.Types.OcrText:
                    currentFieldControl = new FieldOcrTextControl();
                    break;

                case Template.Field.Types.OcrTextLines:
                    currentFieldControl = new FieldOcrTextLinesControl();
                    break;

                case Template.Field.Types.OcrCharBoxs:
                    currentFieldControl = new FieldOcrCharBoxsControl();
                    break;

                case Template.Field.Types.OcrTextLineImages:
                    currentFieldControl = new FieldOcrTextLineImagesControl((float)pictureScale.Value);
                    break;

                case Template.Field.Types.Image:
                    currentFieldControl = new FieldImageControl((float)pictureScale.Value);
                    break;

                default:
                    throw new Exception("Unknown option: " + t);
                }
                currentFieldControl.Initialize(row, v, GetTemplateFromUI(false), (DataGridViewRow r) => { setFieldRow(r, f); });
                settingsControlHeader.Text = "Field [" + f?.Name + "]:";
            }
            finally
            {
                settingCurrentFieldRow = false;
            }
        }
        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);
        }
Exemple #27
0
        void initializeFieldsTable()
        {
            LeftAnchorId.ValueType     = typeof(int);
            LeftAnchorId.ValueMember   = "Id";
            LeftAnchorId.DisplayMember = "Name";

            TopAnchorId.ValueType     = typeof(int);
            TopAnchorId.ValueMember   = "Id";
            TopAnchorId.DisplayMember = "Name";

            RightAnchorId.ValueType     = typeof(int);
            RightAnchorId.ValueMember   = "Id";
            RightAnchorId.DisplayMember = "Name";

            BottomAnchorId.ValueType     = typeof(int);
            BottomAnchorId.ValueMember   = "Id";
            BottomAnchorId.DisplayMember = "Name";

            Type.ValueType  = typeof(Template.Field.Types);
            Type.DataSource = Enum.GetValues(typeof(Template.Field.Types));

            Value.DefaultCellStyle.NullValue = null;  //to avoid error when changing cell type to image

            fields.EnableHeadersVisualStyles = false; //needed to set row headers

            fields.DataError += delegate(object sender, DataGridViewDataErrorEventArgs e)
            {
                DataGridViewRow r = fields.Rows[e.RowIndex];
                Message.Error("Field[row=" + r.Index + "] has unacceptable value of " + fields.Columns[e.ColumnIndex].HeaderText + ":\r\n" + e.Exception.Message, this);
            };

            fields.UserDeletingRow += delegate(object sender, DataGridViewRowCancelEventArgs e)
            {
                if (fields.Rows.Count < 3 && fields.SelectedRows.Count > 0)
                {
                    fields.SelectedRows[0].Selected = false;//to avoid auto-creating row
                }
            };

            fields.UserDeletedRow += delegate(object sender, DataGridViewRowEventArgs e)
            {
            };

            fields.PreviewKeyDown += delegate(object sender, PreviewKeyDownEventArgs e)
            {
                switch (e.KeyCode)
                {
                case Keys.Add:
                case Keys.Oemplus:
                    duplicateSelectedField();
                    break;

                case Keys.Delete:
                case Keys.OemMinus:
                    deleteSelectedField();
                    break;

                case Keys.Up:
                    if (e.Modifiers == Keys.Control)
                    {
                        moveUpSelectedField();
                    }
                    break;

                case Keys.Down:
                    if (e.Modifiers == Keys.Control)
                    {
                        moveDownSelectedField();
                    }
                    break;
                }
            };

            fields.RowsAdded += delegate(object sender, DataGridViewRowsAddedEventArgs e)
            {
            };

            fields.CellValueChanged += delegate(object sender, DataGridViewCellEventArgs e)
            {
                try
                {
                    if (loadingTemplate)
                    {
                        return;
                    }
                    if (e.ColumnIndex < 0)//row's header
                    {
                        return;
                    }
                    DataGridViewRow row = fields.Rows[e.RowIndex];
                    var             cs  = row.Cells;
                    Template.Field  f   = (Template.Field)row.Tag;
                    switch (fields.Columns[e.ColumnIndex].Name)
                    {
                    case "Type":
                    {
                        Template.Field.Types t2 = (Template.Field.Types)row.Cells["Type"].Value;
                        if (t2 == f.Type)
                        {
                            break;
                        }
                        string s = Serialization.Json.Serialize(f);
                        switch (t2)
                        {
                        case Template.Field.Types.PdfText:
                            f = Serialization.Json.Deserialize <Template.Field.PdfText>(s);
                            break;

                        case Template.Field.Types.PdfTextLines:
                            f = Serialization.Json.Deserialize <Template.Field.PdfTextLines>(s);
                            break;

                        case Template.Field.Types.PdfCharBoxs:
                            f = Serialization.Json.Deserialize <Template.Field.PdfCharBoxs>(s);
                            break;

                        case Template.Field.Types.OcrText:
                            f = Serialization.Json.Deserialize <Template.Field.OcrText>(s);
                            break;

                        case Template.Field.Types.OcrTextLines:
                            f = Serialization.Json.Deserialize <Template.Field.OcrTextLines>(s);
                            break;

                        case Template.Field.Types.OcrCharBoxs:
                            f = Serialization.Json.Deserialize <Template.Field.OcrCharBoxs>(s);
                            break;

                        case Template.Field.Types.OcrTextLineImages:
                            f = Serialization.Json.Deserialize <Template.Field.OcrTextLineImages>(s);
                            break;

                        case Template.Field.Types.Image:
                            f = Serialization.Json.Deserialize <Template.Field.Image>(s);
                            break;

                        default:
                            throw new Exception("Unknown option: " + t2);
                        }
                        setFieldRow(row, f);

                        foreach (DataGridViewRow rr in fields.Rows)
                        {
                            if (rr != row && rr.Tag != null && ((Template.Field)rr.Tag).Name == f.Name)
                            {
                                rr.Cells["Type"].Value = t2;
                            }
                        }
                        break;
                    }

                    case "LeftAnchorId":
                    {
                        int?ai = (int?)cs["LeftAnchorId"].Value;
                        if (ai == null)
                        {
                            f.LeftAnchor = null;
                        }
                        else
                        {
                            Page.AnchorActualInfo aai = pages[currentPageI].GetAnchorActualInfo((int)ai);
                            f.LeftAnchor = new Template.Field.SideAnchor
                            {
                                Id    = (int)ai,
                                Shift = aai.Shift.Width,
                            };
                        }
                        setFieldRow(row, f);
                    }
                    break;

                    case "TopAnchorId":
                    {
                        int?ai = (int?)cs["TopAnchorId"].Value;
                        if (ai == null)
                        {
                            f.TopAnchor = null;
                        }
                        else
                        {
                            Page.AnchorActualInfo aai = pages[currentPageI].GetAnchorActualInfo((int)ai);
                            f.TopAnchor = new Template.Field.SideAnchor
                            {
                                Id    = (int)ai,
                                Shift = aai.Shift.Height,
                            };
                        }
                        setFieldRow(row, f);
                    }
                    break;

                    case "RightAnchorId":
                    {
                        int?ai = (int?)cs["RightAnchorId"].Value;
                        if (ai == null)
                        {
                            f.RightAnchor = null;
                        }
                        else
                        {
                            Page.AnchorActualInfo aai = pages[currentPageI].GetAnchorActualInfo((int)ai);
                            f.RightAnchor = new Template.Field.SideAnchor
                            {
                                Id    = (int)ai,
                                Shift = aai.Shift.Width,
                            };
                        }
                        setFieldRow(row, f);
                    }
                    break;

                    case "BottomAnchorId":
                    {
                        int?ai = (int?)cs["BottomAnchorId"].Value;
                        if (ai == null)
                        {
                            f.BottomAnchor = null;
                        }
                        else
                        {
                            Page.AnchorActualInfo aai = pages[currentPageI].GetAnchorActualInfo((int)ai);
                            f.BottomAnchor = new Template.Field.SideAnchor
                            {
                                Id    = (int)ai,
                                Shift = aai.Shift.Height,
                            };
                        }
                        setFieldRow(row, f);
                    }
                    break;

                    case "Name_":
                        f.Name = (string)row.Cells["Name_"].Value;
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Message.Error2(ex, this);
                }
            };

            fields.CurrentCellDirtyStateChanged += delegate
            {
                if (fields.IsCurrentCellDirty)
                {
                    fields.CommitEdit(DataGridViewDataErrorContexts.Commit);
                }
            };

            fields.RowValidating += delegate(object sender, DataGridViewCellCancelEventArgs e)
            {
                DataGridViewRow r = fields.Rows[e.RowIndex];
                try
                {
                    if (r.Tag != null)
                    {
                        string n = FieldPreparation.Normalize((string)r.Cells["Name_"].Value);
                        if (string.IsNullOrWhiteSpace(n))
                        {
                            throw new Exception("Field name cannot be empty!");
                        }
                        //foreach (DataGridViewRow rr in fields.Rows)
                        //{
                        //    if (r == rr)
                        //        continue;
                        //    Template.Field f = (Template.Field)rr.Tag;
                        //    if (f != null && n == f.Name)
                        //        throw new Exception("Name '" + n + "' is duplicated!");
                        //}
                        r.Cells["Name_"].Value = n;
                    }
                }
                catch (Exception ex)
                {
                    Message.Error2(ex, this);
                    e.Cancel = true;
                }
            };

            fields.DefaultValuesNeeded += delegate(object sender, DataGridViewRowEventArgs e)
            {
            };

            fields.CellContentClick += delegate(object sender, DataGridViewCellEventArgs e)
            {
            };

            fields.SelectionChanged += delegate(object sender, EventArgs e)
            {
                try
                {
                    if (loadingTemplate)
                    {
                        return;
                    }

                    if (settingCurrentFieldRow)
                    {
                        return;
                    }

                    if (fields.SelectedRows.Count < 1)
                    {
                        return;
                    }
                    DataGridViewRow row = fields.SelectedRows[0];
                    Template.Field  f   = (Template.Field)row.Tag;
                    if (f == null)//hacky forcing to commit a newly added row and display the blank row
                    {
                        int i = fields.Rows.Add();
                        row = fields.Rows[i];
                        f   = templateManager.CreateDefaultField();
                        setFieldRow(row, f);
                        row.Selected = true;
                        return;
                    }
                    setCurrentFieldRow(row);
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    Message.Error(ex, this);
                }
            };

            copy2ClipboardField.LinkClicked += delegate
            {
                if (pages == null)
                {
                    return;
                }
                if (fields.SelectedRows.Count < 1)
                {
                    return;
                }
                DataGridViewRow r = fields.SelectedRows[fields.SelectedRows.Count - 1];
                if (r.Tag == null)
                {
                    return;
                }
                Template.Field f    = (Template.Field)r.Tag;
                Type           type = f.GetType();
                switch (type)
                {
                case Type _ when f is Template.Field.Text:
                    Clipboard.SetText(pages[currentPageI].GetText(f.Name));
                    break;

                case Type _ when f is Template.Field.TextLines:
                    Clipboard.SetText(string.Join("\r\n", pages[currentPageI].GetTextLines(f.Name)));
                    break;

                case Type _ when f is Template.Field.CharBoxs:
                    Clipboard.SetText(Serialization.Json.Serialize(pages[currentPageI].GetCharBoxes(f.Name)));
                    break;

                case Type _ when f is Template.Field.Image:
                    Clipboard.SetData(DataFormats.Bitmap, pages[currentPageI].GetImage(f.Name));
                    break;

                case Type _ when f is Template.Field.OcrTextLineImages:
                    Clipboard.SetData(typeof(List <Bitmap>).ToString(), pages[currentPageI].GetOcrTextLineImages(f.Name));
                    break;

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

            duplicateField.LinkClicked += delegate
            {
                duplicateSelectedField();
            };
            //duplicateField.LinkClicked += delegate
            //{
            //    if (fields.SelectedRows.Count < 1)
            //        return;
            //    DataGridViewRow r0 = fields.SelectedRows[fields.SelectedRows.Count - 1];
            //    if (r0.Tag == null)
            //        return;
            //    Template.Field f0 = (Template.Field)r0.Tag;
            //    if (f0.ColumnOfTable != null)
            //    {
            //        Message.Exclaim("This field is a column of table " + f0.ColumnOfTable + " so you should create a new definition of it.");
            //        return;
            //    }
            //    List<DataGridViewRow> cloningFieldRows = new List<DataGridViewRow> { r0 };
            //    //foreach (DataGridViewRow r in fields.Rows)
            //    //    if (r != r0 && (r.Tag as Template.Field)?.ColumnOfTable == f0.Name)
            //    //    {
            //    //        string fn = (r.Tag as Template.Field)?.Name;
            //    //        if (cloningFieldRows.Find(x => (x.Tag as Template.Field)?.Name == fn) == null)
            //    //            cloningFieldRows.Add(r);
            //    //    }

            //    settingCurrentFieldRow = true;//required due to fields-column error calculation when selected row changes
            //    foreach (DataGridViewRow row in cloningFieldRows)
            //    {
            //        Template.Field f = (Template.Field)Serialization.Json.Clone(((Template.Field)row.Tag).GetType(), row.Tag);
            //        f.LeftAnchor = null;
            //        f.TopAnchor = null;
            //        f.RightAnchor = null;
            //        f.BottomAnchor = null;
            //        int i = fields.Rows.Add();
            //        DataGridViewRow r = fields.Rows[i];
            //        setFieldRow(r, f);
            //        fields.Rows.Remove(r);
            //        fields.Rows.Insert(row.Index + 1, r);
            //    }
            //    settingCurrentFieldRow = false;
            //};

            deleteField.LinkClicked += delegate
            {
                deleteSelectedField();
            };
            //deleteField.LinkClicked += delegate
            //{
            //    if (fields.SelectedRows.Count < 1)
            //        return;
            //    DataGridViewRow r0 = fields.SelectedRows[fields.SelectedRows.Count - 1];
            //    if (r0.Tag == null)
            //        return;
            //    Template.Field f0 = (Template.Field)r0.Tag;
            //    bool unique = true;
            //    foreach (DataGridViewRow rr in fields.Rows)
            //        if (rr != r0 && rr.Tag != null && ((Template.Field)rr.Tag).Name == f0.Name)
            //        {
            //            unique = false;
            //            break;
            //        }
            //    if (unique)
            //    {
            //        Message.Inform("This field definition cannot be deleted because it is the last of the field.");
            //        return;
            //    }
            //    if (f0.ColumnOfTable != null)
            //    {
            //        Message.Exclaim("This field is a column of table " + f0.ColumnOfTable + " so you should delete the respectivea definition of it.");
            //        return;
            //    }
            //    Dictionary<string, List<DataGridViewRow>> fieldName2orderedRows = new Dictionary<string, List<DataGridViewRow>>();
            //    foreach (DataGridViewRow r in fields.Rows)
            //        if ((r.Tag as Template.Field.PdfText)?.ColumnOfTable == f0.Name)
            //        {
            //            List<DataGridViewRow> rs;
            //            string fn = (r.Tag as Template.Field)?.Name;
            //            if (!fieldName2orderedRows.TryGetValue(fn, out rs))
            //            {
            //                rs = new List<DataGridViewRow>();
            //                fieldName2orderedRows[fn] = rs;
            //            }
            //            rs.Add(r);
            //        }

            //    int definitionIndex = fieldName2orderedRows[f0.Name].IndexOf(r0);
            //    fieldName2orderedRows.Remove(f0.Name);
            //    List<DataGridViewRow> deletingFieldRows = new List<DataGridViewRow> { r0 };
            //    foreach (List<DataGridViewRow> rs in fieldName2orderedRows.Values)
            //        deletingFieldRows.Add(rs[definitionIndex]);

            //    settingCurrentFieldRow = true;//required due to fields-column error calculation when selected row changes
            //    foreach (DataGridViewRow row in deletingFieldRows)
            //        fields.Rows.Remove(row);
            //    settingCurrentFieldRow = false;
            //};

            moveUpField.LinkClicked += delegate
            {
                moveUpSelectedField();
            };

            moveDownField.LinkClicked += delegate
            {
                moveDownSelectedField();
            };

            newField.LinkClicked += delegate
            {
                createNewField();
            };
        }
        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);
        }
        Template getTemplateFromUI(bool saving)
        {
            Template t = new Template();

            if (saving && string.IsNullOrWhiteSpace(name.Text))
            {
                throw new Exception("Name is empty!");
            }

            t.Name = name.Text.Trim();

            t.TextAutoInsertSpace = new TextAutoInsertSpace
            {
                Threshold      = (float)textAutoInsertSpaceThreshold.Value,
                Representative = textAutoInsertSpaceRepresentative.Text,
            };

            t.PageRotation        = (Template.PageRotations)pageRotation.SelectedIndex;
            t.AutoDeskew          = autoDeskew.Checked;
            t.AutoDeskewThreshold = (int)autoDeskewThreshold.Value;

            bool?removeNotLinkedAnchors = null;

            t.Anchors = new List <Template.Anchor>();
            List <int> conditionAnchorIds = null;

            if (saving)
            {
                conditionAnchorIds = new List <int>();
                foreach (DataGridViewRow r in conditions.Rows)
                {
                    Template.Condition c = (Template.Condition)r.Tag;
                    if (c != null && c.IsSet())
                    {
                        conditionAnchorIds.AddRange(BooleanEngine.GetAnchorIds(c.Value));
                    }
                }
                conditionAnchorIds = conditionAnchorIds.Distinct().ToList();
            }
            foreach (DataGridViewRow r in anchors.Rows)
            {
                Template.Anchor a = (Template.Anchor)r.Tag;
                if (a == null)
                {
                    continue;
                }

                if (saving)
                {
                    if (!a.IsSet())
                    {
                        throw new Exception("Anchor[Id=" + a.Id + "] is not set!");
                    }

                    bool engaged = false;
                    if (conditionAnchorIds.Contains(a.Id))
                    {
                        engaged = true;
                    }
                    if (!engaged)
                    {
                        foreach (DataGridViewRow rr in anchors.Rows)
                        {
                            Template.Anchor a_ = (Template.Anchor)rr.Tag;
                            if (a_ == null)
                            {
                                continue;
                            }
                            if (a_.ParentAnchorId == a.Id)
                            {
                                engaged = true;
                                break;
                            }
                        }
                    }
                    if (!engaged)
                    {
                        foreach (DataGridViewRow rr in fields.Rows)
                        {
                            Template.Field m = (Template.Field)rr.Tag;
                            if (m != null && (m.LeftAnchor?.Id == a.Id || m.TopAnchor?.Id == a.Id || m.RightAnchor?.Id == a.Id || m.BottomAnchor?.Id == a.Id))
                            {
                                engaged = true;
                                break;
                            }
                        }
                    }
                    if (!engaged)
                    {
                        if (removeNotLinkedAnchors == null)
                        {
                            removeNotLinkedAnchors = Message.YesNo("The template contains not linked anchor[s]. Should they be removed?");
                        }
                        if (removeNotLinkedAnchors == true)
                        {
                            continue;
                        }
                    }
                }

                t.Anchors.Add(a);
            }
            t.Anchors = t.Anchors.OrderBy(a => a.Id).ToList();

            t.Conditions = new List <Template.Condition>();
            foreach (DataGridViewRow r in conditions.Rows)
            {
                Template.Condition c = (Template.Condition)r.Tag;
                if (c == null)
                {
                    continue;
                }
                if (saving)
                {
                    if (!c.IsSet())
                    {
                        throw new Exception("Condition[name=" + c.Name + "] is not set!");
                    }
                    BooleanEngine.Check(c.Value, t.Anchors.Select(x => x.Id));
                }
                t.Conditions.Add(c);
            }
            if (saving)
            {
                var dcs = t.Conditions.GroupBy(x => x.Name).Where(x => x.Count() > 1).FirstOrDefault();
                if (dcs != null)
                {
                    throw new Exception("Condition '" + dcs.First().Name + "' is duplicated!");
                }
            }

            t.Fields = new List <Template.Field>();
            foreach (DataGridViewRow r in fields.Rows)
            {
                Template.Field f = (Template.Field)r.Tag;
                if (f == null)
                {
                    continue;
                }
                if (saving && !f.IsSet())
                {
                    throw new Exception("Field[" + r.Index + "] is not set!");
                }
                if (saving)
                {
                    foreach (int?ai in new List <int?> {
                        f.LeftAnchor?.Id, f.TopAnchor?.Id, f.RightAnchor?.Id, f.BottomAnchor?.Id
                    })
                    {
                        if (ai != null && t.Anchors.FirstOrDefault(x => x.Id == ai) == null)
                        {
                            throw new Exception("Anchor[Id=" + ai + " does not exist.");
                        }
                    }
                }
                t.Fields.Add(f);
            }
            if (saving)
            {
                if (t.Fields.Count < 1)
                {
                    throw new Exception("Fields is empty!");
                }
                //var dfs = t.Fields.GroupBy(x => x.Name).Where(x => x.Count() > 1).FirstOrDefault();
                //if (dfs != null)
                //    throw new Exception("Field '" + dfs.First().Name + "' is duplicated!");

                //foreach (string columnOfTable in t.Fields.Where(x => x is Template.Field.PdfText).Select(x => (Template.Field.PdfText)x).Where(x => x.ColumnOfTable != null).Select(x => x.ColumnOfTable))
                //{
                //    Dictionary<string, List<Template.Field>> fieldName2orderedFields = new Dictionary<string, List<Template.Field>>();
                //    foreach (Template.Field.PdfText pt in t.Fields.Where(x => x is Template.Field.PdfText).Select(x => (Template.Field.PdfText)x).Where(x => x.ColumnOfTable == columnOfTable))
                //    {
                //        List<Template.Field> fs;
                //        if (!fieldName2orderedFields.TryGetValue(pt.Name, out fs))
                //        {
                //            fs = new List<Template.Field>();
                //            fieldName2orderedFields[pt.Name] = fs;
                //        }
                //        fs.Add(pt);
                //    }
                //    int definitionCount = fieldName2orderedFields.Max(x => x.Value.Count());
                //    foreach (string fn in fieldName2orderedFields.Keys)
                //        if (definitionCount > fieldName2orderedFields[fn].Count)
                //            throw new Exception("Field '" + fn + "' is column of table " + columnOfTable + " and so it must have the same number of definitions as the rest column fields!");
                //}
                foreach (Template.Field f0 in t.Fields)
                {
                    int    tableDefinitionsCount = t.Fields.Where(x => x.Name == f0.Name).Count();
                    string fn = t.Fields.Where(x => x.ColumnOfTable == f0.Name).GroupBy(x => x.Name).Where(x => x.Count() > tableDefinitionsCount).FirstOrDefault()?.First().Name;
                    if (fn != null)
                    {
                        throw new Exception("Field '" + fn + "' is a column of table field " + f0.Name + " so " + f0.Name + " must have number of definitions not less then that of " + fn + ".");
                    }
                }
            }

            if (saving)
            {
                t.Editor = new Template.EditorSettings
                {
                    TestFile         = testFile.Text,
                    TestPictureScale = pictureScale.Value,
                    ExtractFieldsAutomaticallyWhenPageChanged   = ExtractFieldsAutomaticallyWhenPageChanged.Checked,
                    CheckConditionsAutomaticallyWhenPageChanged = CheckConditionsAutomaticallyWhenPageChanged.Checked,
                };
            }

            return(t);
        }
        internal Template GetTemplateFromUI(bool saving)
        {
            Template t = new Template();

            if (saving && string.IsNullOrWhiteSpace(name.Text))
            {
                throw new Exception("Name is empty!");
            }

            t.Name = name.Text.Trim();

            t.TextAutoInsertSpace = new TextAutoInsertSpace
            {
                Threshold          = (float)textAutoInsertSpace_Threshold.Value,
                IgnoreSourceSpaces = textAutoInsertSpace_IgnoreSourceSpaces.Checked,
                //Representative//default
            };

            t.TesseractPageSegMode      = (Tesseract.PageSegMode)TesseractPageSegMode.SelectedItem;
            t.AdjustLineBorders         = AdjustLineBorders.Checked;
            t.SingleFieldFromFieldImage = SingleFieldFromFieldImage.Checked;
            t.ColumnCellFromCellImage   = ColumnCellFromCellImage.Checked;
            if (CharSizeFilterMinWidth.Value > 0 || CharSizeFilterMaxWidth.Value > 0 || CharSizeFilterMinHeight.Value > 0 || CharSizeFilterMaxHeight.Value > 0)
            {
                t.CharFilter = new CharFilter {
                    MinWidth = (float)CharSizeFilterMinWidth.Value, MaxWidth = (float)CharSizeFilterMaxWidth.Value, MinHeight = (float)CharSizeFilterMinHeight.Value, MaxHeight = (float)CharSizeFilterMaxHeight.Value
                }
            }
            ;
            if (LinePaddingY.Value > 0)
            {
                t.LinePaddingY = (int)LinePaddingY.Value;
            }

            bitmapPreparationForm.SetTemplate(t);

            bool?      removeNotLinkedAnchors = null;
            List <int> conditionAnchorIds     = null;

            if (saving)
            {
                conditionAnchorIds = new List <int>();
                foreach (DataGridViewRow r in conditions.Rows)
                {
                    Template.Condition c = (Template.Condition)r.Tag;
                    if (c != null && c.IsSet())
                    {
                        conditionAnchorIds.AddRange(BooleanEngine.GetAnchorIds(c.Value));
                    }
                }
                conditionAnchorIds = conditionAnchorIds.Distinct().ToList();
            }
            t.Anchors = new List <Template.Anchor>();
            foreach (DataGridViewRow r in anchors.Rows)
            {
                Template.Anchor a = (Template.Anchor)r.Tag;
                if (a == null)
                {
                    continue;
                }

                if (saving)
                {
                    if (!a.IsSet())
                    {
                        throw new Exception("Anchor[Id=" + a.Id + "] is not set!");
                    }

                    if (!conditionAnchorIds.Contains(a.Id))
                    {
                        bool engaged = false;
                        foreach (DataGridViewRow rr in anchors.Rows)
                        {
                            Template.Anchor a_ = (Template.Anchor)rr.Tag;
                            if (a_ == null)
                            {
                                continue;
                            }
                            if (a_.ParentAnchorId == a.Id)
                            {
                                engaged = true;
                                break;
                            }
                        }
                        if (!engaged)
                        {
                            foreach (DataGridViewRow rr in fields.Rows)
                            {
                                Template.Field m = (Template.Field)rr.Tag;
                                if (m != null && (m.LeftAnchor?.Id == a.Id || m.TopAnchor?.Id == a.Id || m.RightAnchor?.Id == a.Id || m.BottomAnchor?.Id == a.Id))
                                {
                                    engaged = true;
                                    break;
                                }
                            }
                            if (!engaged)
                            {
                                if (a.Id != t.ScalingAnchorId)
                                {
                                    if (removeNotLinkedAnchors == null)
                                    {
                                        removeNotLinkedAnchors = Message.YesNo("The template contains not linked anchor[s]. Remove them?", this);
                                    }
                                    if (removeNotLinkedAnchors == true)
                                    {
                                        continue;
                                    }
                                }
                            }
                        }
                    }
                }
                t.Anchors.Add((Template.Anchor)Serialization.Json.Clone2(a));
            }
            t.Anchors = t.Anchors.OrderBy(a => a.Id).ToList();

            if (saving)
            {
                t.GetScalingAnchor();//check is it is correct;
            }
            t.Conditions = new List <Template.Condition>();
            foreach (DataGridViewRow r in conditions.Rows)
            {
                Template.Condition c = (Template.Condition)r.Tag;
                if (c == null)
                {
                    continue;
                }
                if (saving)
                {
                    if (!c.IsSet())
                    {
                        throw new Exception("Condition[name=" + c.Name + "] is not set!");
                    }
                    BooleanEngine.Check(c.Value, t.Anchors.Select(x => x.Id));
                }
                t.Conditions.Add((Template.Condition)Serialization.Json.Clone2(c));
            }
            if (saving)
            {
                var dcs = t.Conditions.GroupBy(x => x.Name).Where(x => x.Count() > 1).FirstOrDefault();
                if (dcs != null)
                {
                    throw new Exception("Condition '" + dcs.First().Name + "' is duplicated!");
                }
            }

            t.Fields = new List <Template.Field>();
            foreach (DataGridViewRow r in fields.Rows)
            {
                Template.Field f = (Template.Field)r.Tag;
                if (f == null)
                {
                    continue;
                }
                if (saving && !f.IsSet())
                {
                    throw new Exception("Field[" + r.Index + "] is not set!");
                }
                if (saving)
                {
                    foreach (int?ai in new List <int?> {
                        f.LeftAnchor?.Id, f.TopAnchor?.Id, f.RightAnchor?.Id, f.BottomAnchor?.Id
                    })
                    {
                        if (ai != null && t.Anchors.FirstOrDefault(x => x.Id == ai) == null)
                        {
                            throw new Exception("Anchor[Id=" + ai + " does not exist.");
                        }
                    }
                }
                t.Fields.Add((Template.Field)Serialization.Json.Clone2(f));
            }
            if (saving)
            {
                if (t.Fields.Count < 1)
                {
                    throw new Exception("Fields is empty!");
                }

                var fs = t.Fields.GroupBy(x => x.Name).Where(x => x.GroupBy(a => a.GetType()).Count() > 1).FirstOrDefault();
                if (fs != null)
                {
                    throw new Exception("Definitions of the field '" + fs.First().Name + "' are not of the same type!");
                }
            }

            if (saving)
            {
                t.Editor = new Template.EditorSettings
                {
                    //TestFile = testFile.Text,
                    TestPictureScale = pictureScale.Value,
                    ExtractFieldsAutomaticallyWhenPageChanged = ExtractFieldsAutomaticallyWhenPageChanged.Checked,
                    ShowFieldTextLineSeparators = ShowFieldTextLineSeparators.Checked,
                    CheckConditionsAutomaticallyWhenPageChanged = CheckConditionsAutomaticallyWhenPageChanged.Checked,
                };
            }

            return(t);
        }
    }