private List <TableBorder> GetHorizontalIntersectBorders(TableBorder vBorder)
        {
            List <TableBorder> hIntersectBorders = new List <TableBorder>();
            Rectangle          mRect             = new Rectangle(vBorder.Border.GetX() - 1, vBorder.Border.GetY() - 1,
                                                                 vBorder.Border.GetWidth() + 2, vBorder.Border.GetHeight() + 2);

            hIntersectBorders = hBorders.Where(l => l.Border.Overlaps(mRect)).ToList();

            return(hIntersectBorders);
        }
        private int GetTableIdForRectangle(TableBorder vBorder1, TableBorder vBorder2)
        {
            int tblId = (vBorder1 != null && vBorder1.TableId > 0) ? vBorder1.TableId :
                        (vBorder2 != null && vBorder2.TableId > 0 ? vBorder2.TableId : ++tableCounter);

            if (tableDS.Tables.Count < tableCounter)
            {
                tableDS.Tables.Add(new System.Data.DataTable());
            }
            return(tblId);
        }
 private void PopulateTable(TableBorder vBorder, List <string> cols)
 {
     if (!(tableDS.Tables.Count < vBorder.TableId) && tableDS.Tables.Count > 0 && vBorder.TableId > 0)
     {
         if (tableDS.Tables[vBorder.TableId - 1].Rows.Count > 0)
         {
             tableDS.Tables[vBorder.TableId - 1].Columns.Add();
             cols.Reverse();
             int columIndex = tableDS.Tables[vBorder.TableId - 1].Columns.Count - 1;
             for (int rowIndex = 0; rowIndex < cols.Count(); rowIndex++)
             {
                 if (rowIndex < tableDS.Tables[vBorder.TableId - 1].Rows.Count)
                 {
                     tableDS.Tables[vBorder.TableId - 1].Rows[rowIndex][columIndex] = cols[rowIndex];
                 }
                 else
                 {
                     System.Data.DataRow newRow = tableDS.Tables[vBorder.TableId - 1].NewRow();
                     newRow[columIndex] = cols[rowIndex];
                     tableDS.Tables[vBorder.TableId - 1].Rows.Add(newRow);
                 }
             }
         }
         else
         {
             tableDS.Tables[vBorder.TableId - 1].Columns.Add();
             cols.Reverse();
             foreach (string col in cols)
             {
                 System.Data.DataRow newRow = tableDS.Tables[vBorder.TableId - 1].NewRow();
                 newRow[0] = col;
                 tableDS.Tables[vBorder.TableId - 1].Rows.Add(newRow);
             }
         }
     }
 }
        private void GetTablesFromborders()
        {
            vBorders.Sort(delegate(TableBorder border1, TableBorder border2)
            {
                if (border1 == null && border2 == null)
                {
                    return(0);
                }
                else if (border1 == null)
                {
                    return(-1);
                }
                else if (border2 == null)
                {
                    return(1);
                }
                else
                {
                    int comparerInt = border1.Border.GetX().CompareTo(border2.Border.GetX());
                    if (comparerInt == 0)
                    {
                        comparerInt = border1.Border.GetY().CompareTo(border2.Border.GetY());
                    }
                    return(comparerInt);
                }
            });
            hBorders.Sort(delegate(TableBorder border1, TableBorder border2)
            {
                if (border1 == null && border2 == null)
                {
                    return(0);
                }
                else if (border1 == null)
                {
                    return(-1);
                }
                else if (border2 == null)
                {
                    return(1);
                }
                else
                {
                    int comparerInt = border1.Border.GetY().CompareTo(border2.Border.GetY());
                    if (comparerInt == 0)
                    {
                        comparerInt = border1.Border.GetX().CompareTo(border2.Border.GetX());
                    }
                    return(comparerInt);
                }
            });

            foreach (TableBorder vBorder in vBorders)
            {
                List <TableBorder> hIntersectBorders = GetHorizontalIntersectBorders(vBorder);
                hIntersectBorders.Sort(delegate(TableBorder border1, TableBorder border2)
                {
                    if (border1 == null && border2 == null)
                    {
                        return(0);
                    }
                    else if (border1 == null)
                    {
                        return(-1);
                    }
                    else if (border2 == null)
                    {
                        return(1);
                    }
                    else
                    {
                        return(border1.Border.GetY().CompareTo(border2.Border.GetY()));
                    }
                });

                List <string> cols  = new List <string>();
                int           front = 0;

                for (int rear = 0; rear < hIntersectBorders.Count(); rear++)
                {
                    hIntersectBorders[rear].AddIntersectBorder(vBorder.BorderId, vBorder.Border);

                    if (front < rear)
                    {
                        List <KeyValuePair <int, Rectangle> > frontIntersects = hIntersectBorders[front].IntersectBorders;
                        List <KeyValuePair <int, Rectangle> > rearIntersects  = hIntersectBorders[rear].IntersectBorders;
                        frontIntersects.Sort(delegate(KeyValuePair <int, Rectangle> rect1, KeyValuePair <int, Rectangle> rect2)
                        {
                            if (rect1.Value == null && rect2.Value == null)
                            {
                                return(0);
                            }
                            else if (rect1.Value == null)
                            {
                                return(-1);
                            }
                            else if (rect2.Value == null)
                            {
                                return(1);
                            }
                            else
                            {
                                int comparerInt = rect1.Value.GetX().CompareTo(rect2.Value.GetX()) * -1;
                                if (comparerInt == 0)
                                {
                                    comparerInt = rect1.Value.GetY().CompareTo(rect2.Value.GetY()) * -1;
                                }
                                return(comparerInt);
                            }
                        });
                        rearIntersects.Sort(delegate(KeyValuePair <int, Rectangle> rect1, KeyValuePair <int, Rectangle> rect2)
                        {
                            if (rect1.Value == null && rect2.Value == null)
                            {
                                return(0);
                            }
                            else if (rect1.Value == null)
                            {
                                return(-1);
                            }
                            else if (rect2.Value == null)
                            {
                                return(1);
                            }
                            else
                            {
                                int comparerInt = rect1.Value.GetX().CompareTo(rect2.Value.GetX()) * -1;
                                if (comparerInt == 0)
                                {
                                    comparerInt = rect1.Value.GetY().CompareTo(rect2.Value.GetY()) * -1;
                                }
                                return(comparerInt);
                            }
                        });

                        bool isMatchFlag = false;
                        for (int fIndex = 0; fIndex < frontIntersects.Count(); fIndex++)
                        {
                            for (int rIndex = 0; rIndex < rearIntersects.Count(); rIndex++)
                            {
                                if (frontIntersects[fIndex].Key == rearIntersects[rIndex].Key &&
                                    frontIntersects[fIndex].Key != vBorder.BorderId)
                                {
                                    Rectangle rectangle = GetSingleRectangleFromBorder(
                                        vBorder.Border, hIntersectBorders[front].Border,
                                        frontIntersects[fIndex].Value, hIntersectBorders[rear].Border);
                                    if (rectangle != null)
                                    {
                                        cols.Add(GetTextFromRectangle(rectangle));
                                        TableBorder firstVBorder = vBorders.FirstOrDefault(l => l.BorderId == frontIntersects[fIndex].Key);
                                        vBorder.TableId = firstVBorder.TableId = GetTableIdForRectangle(firstVBorder, vBorder);
                                    }
                                    front       = rear;
                                    isMatchFlag = true;
                                    break;
                                }
                            }
                            if (isMatchFlag)
                            {
                                break;
                            }
                        }
                    }
                }
                PopulateTable(vBorder, cols);
            }
        }
        private void ProcessBorder(Rectangle rect)
        {
            Rectangle mRect = new Rectangle(rect.GetX() - 1, rect.GetY() - 1, rect.GetWidth() + 2, rect.GetHeight() + 2);

            //////////////////////////VB///////////////////
            if (rect.GetHeight() > rect.GetWidth() * 1.5)
            {
                List <TableBorder> sqList     = sqBorders.Where(sq => mRect.Overlaps(sq.Border)).ToList();
                List <TableBorder> tempSqList = new List <TableBorder>();

                foreach (TableBorder sq in sqList)
                {
                    rect = VJoinRectangle(rect, sq.Border);
                    //TODO m SQ
                    TableBorder tempSq = sq;
                    sqBorders.Remove(sq);

                    if (IsHInside(rect, tempSq.Border))
                    {
                        tempSq.Border = HJoinRectangle(tempSq.Border, rect);
                        tempSq.Border = ProcessPartialyJoinRect(tempSq.Border, ref tempSqList, "F", "Sq");
                        tempSqList.Add(tempSq);
                    }
                }
                ////////hl///////
                mRect = new Rectangle(rect.GetX() - 1, rect.GetY() - 1, rect.GetWidth() + 2, rect.GetHeight() + 2);
                List <TableBorder> hList      = hBorders.Where(hb => mRect.Overlaps(hb.Border)).ToList();
                List <TableBorder> tempHlList = new List <TableBorder>();

                foreach (TableBorder hl in hList)
                {
                    rect = VJoinRectangle(rect, hl.Border);
                    //TODO m hl
                    TableBorder tempHl = hl;
                    hBorders.Remove(hl);

                    if (IsHInside(rect, tempHl.Border))
                    {
                        tempHl.Border = HJoinRectangle(tempHl.Border, rect);
                        tempHl.Border = ProcessPartialyJoinRect(tempHl.Border, ref tempHlList, "F", "HL");
                        tempHl.Border = ProcessPartialyJoinRect(tempHl.Border, ref tempSqList, "H", "Sq");
                        tempHlList.Add(tempHl);
                    }
                }
                ///////////VL////////////
                mRect = new Rectangle(rect.GetX() - 1, rect.GetY() - 1, rect.GetWidth() + 2, rect.GetHeight() + 2);
                List <TableBorder> vList = vBorders.Where(vb => mRect.Overlaps(vb.Border)).ToList();
                AddPBorder(ref rect, ref vList, ref tempHlList, ref tempSqList, false);
            }
            //////////////////////////HB///////////////////
            else if (rect.GetWidth() > rect.GetHeight() * 1.5)
            {
                /////SQ/////
                List <TableBorder> sqList     = sqBorders.Where(sq => mRect.Overlaps(sq.Border)).ToList();
                List <TableBorder> tempSqList = new List <TableBorder>();

                foreach (TableBorder sq in sqList)
                {
                    rect = HJoinRectangle(rect, sq.Border);
                    //TODO m SQ
                    TableBorder tempSq = sq;
                    sqBorders.Remove(sq);

                    if (IsVInside(rect, tempSq.Border))
                    {
                        tempSq.Border = VJoinRectangle(tempSq.Border, rect);
                        tempSq.Border = ProcessPartialyJoinRect(tempSq.Border, ref tempSqList, "F", "Sq");
                        tempSqList.Add(tempSq);
                    }
                }
                /////VL///////
                mRect = new Rectangle(rect.GetX() - 1, rect.GetY() - 1, rect.GetWidth() + 2, rect.GetHeight() + 2);
                List <TableBorder> vList      = vBorders.Where(vb => mRect.Overlaps(vb.Border)).ToList();
                List <TableBorder> tempVlList = new List <TableBorder>();

                foreach (TableBorder vl in vList)
                {
                    rect = HJoinRectangle(rect, vl.Border);
                    //TODO m hl
                    TableBorder tempVl = vl;
                    vBorders.Remove(vl);

                    if (IsVInside(rect, tempVl.Border))
                    {
                        tempVl.Border = VJoinRectangle(tempVl.Border, rect);
                        tempVl.Border = ProcessPartialyJoinRect(tempVl.Border, ref tempVlList, "F", "VL");
                        tempVl.Border = ProcessPartialyJoinRect(tempVl.Border, ref tempSqList, "V", "Sq");
                        tempVlList.Add(tempVl);
                    }
                }
                //////HL////////
                mRect = new Rectangle(rect.GetX() - 1, rect.GetY() - 1, rect.GetWidth() + 2, rect.GetHeight() + 2);
                List <TableBorder> hList = hBorders.Where(hb => mRect.Overlaps(hb.Border)).ToList();

                AddPBorder(ref rect, ref hList, ref tempVlList, ref tempSqList, true);
            }
            /////////////////////////SqB///////////////////
            else
            {
                ProcessSQBorder(ref rect, mRect);
            }
        }