Esempio n. 1
0
        void setConditionsStatus()
        {
            if (pages == null)
            {
                return;
            }
            pages.ActiveTemplate = getTemplateFromUI(false);
            foreach (DataGridViewRow r in conditions.Rows)
            {
                setConditionStatus(r);
            }

            List <int> 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));
                }
            }
            foreach (int anchorId in conditionAnchorIds.Distinct())
            {
                _setAnchorStatus(anchorId);
            }
        }
Esempio n. 2
0
 void setConditionStatus(DataGridViewRow r)
 {
     Template.Condition c = (Template.Condition)r.Tag;
     if (c == null)
     {
         return;
     }
     if (!c.IsSet())
     {
         setRowStatus(statuses.NEUTRAL, r, "");
         return;
     }
     try
     {
         if (pages[currentPageI].IsCondition(c.Name))
         {
             setRowStatus(statuses.SUCCESS, r, "True");
         }
         else
         {
             setRowStatus(statuses.ERROR, r, "False");
         }
     }
     catch (Exception e)
     {
         setRowStatus(statuses.WRONG, r, e.Message);
     }
 }
Esempio n. 3
0
 void deleteSelectedCondition()
 {
     try
     {
         if (conditions.SelectedRows.Count < 1)
         {
             return;
         }
         DataGridViewRow r0 = conditions.SelectedRows[conditions.SelectedRows.Count - 1];
         if (r0.Tag == null)
         {
             return;
         }
         Template.Condition c0 = (Template.Condition)r0.Tag;
         if (!Message.YesNo("Proceed with removing condition '" + c0.Name + "'?"))
         {
             return;
         }
         fields.Rows.Remove(r0);
     }
     catch (Exception e)
     {
         Win.LogMessage.Error(e);
     }
     finally
     {
         settingCurrentConditionRow = false;
     }
 }
Esempio n. 4
0
        void setCurrentConditionRow(DataGridViewRow row)
        {
            if (settingCurrentConditionRow)
            {
                return;
            }
            try
            {
                settingCurrentConditionRow = true;

                currentConditionRow = row;

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

                conditions.CurrentCell = conditions[0, row.Index];

                Template.Condition c = (Template.Condition)row.Tag;
                bool firstAnchor     = true;
                foreach (int ai in BooleanEngine.GetAnchorIds(c.Value))
                {
                    Template.Anchor a = getAnchor(ai, out DataGridViewRow r);
                    if (r == null)
                    {
                        continue;
                    }
                    if (firstAnchor)
                    {
                        firstAnchor = false;
                        showAnchorRowAs(ai, rowStates.Linked, true);
                        setCurrentAnchorRow(ai, true);
                        clearImageFromBoxes();
                    }
                    else
                    {
                        showAnchorRowAs(ai, rowStates.Condition, false);
                    }
                    findAndDrawAnchor(ai);
                }
                if (firstAnchor)
                {
                    showAnchorRowAs(null, rowStates.NULL, true);
                    setCurrentAnchorRow(null, true);
                }

                setCurrentFieldRow(null);

                setConditionStatus(currentConditionRow);
            }
            finally
            {
                settingCurrentConditionRow = false;
            }
        }
Esempio n. 5
0
        void createNewCondition()
        {
            Template.Condition c = new Template.Condition {
                Name = ""
            };
            int i = conditions.CurrentRow?.Index >= 0 ? conditions.CurrentRow.Index + 1 : conditions.Rows.Count;

            conditions.Rows.Insert(i, 1);
            setConditionRow(conditions.Rows[i], c);
            conditions.Rows[i].Selected = true;
        }
Esempio n. 6
0
        void setConditionRow(DataGridViewRow row, Template.Condition c)
        {
            row.Tag = c;
            c.Value = BooleanEngine.GetFormatted(c.Value);
            row.Cells["Name2"].Value  = c.Name;
            row.Cells["Value2"].Value = c.Value;

            if (loadingTemplate)
            {
                return;
            }

            if (row == currentConditionRow)
            {
                setCurrentConditionRow(row);
            }
        }
        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);
        }
Esempio n. 8
0
        void initializeConditionsTable()
        {
            conditions.EnableHeadersVisualStyles = false;//needed to set row headers

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

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

            conditions.CellValueChanged += delegate(object sender, DataGridViewCellEventArgs e)
            {
                if (loadingTemplate)
                {
                    return;
                }
                if (e.ColumnIndex < 0)//row's header
                {
                    return;
                }
                DataGridViewRow    row = conditions.Rows[e.RowIndex];
                var                cs  = row.Cells;
                Template.Condition c   = (Template.Condition)row.Tag;
                switch (conditions.Columns[e.ColumnIndex].Name)
                {
                case "Name2":
                {
                    c.Name = (string)row.Cells["Name2"].Value;
                    break;
                }

                case "Value2":
                {
                    c.Value = (string)row.Cells["Value2"].Value;
                    break;
                }
                }
                setConditionRow(row, c);
                setConditionStatus(row);
            };

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

                    if (settingCurrentConditionRow)
                    {
                        return;
                    }

                    if (conditions.SelectedRows.Count < 1)
                    {
                        return;
                    }
                    DataGridViewRow    row = conditions.SelectedRows[0];
                    Template.Condition c   = (Template.Condition)row.Tag;
                    if (c == null)//hacky forcing commit a newly added row and display the blank row
                    {
                        int i = conditions.Rows.Add();
                        row = conditions.Rows[i];
                        c   = templateManager.CreateDefaultCondition();
                        setConditionRow(row, c);
                        row.Selected = true;
                        return;
                    }
                    setCurrentConditionRow(row);
                }
                catch (Exception ex)
                {
                    Message.Error2(ex);
                }
            };
        }
        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);
        }
    }
        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);
        }