Ejemplo n.º 1
0
        public Border GetBorder(TableCellBorderType borderType)
        {
            Border   border   = new Border();
            XElement xElement = base.Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));

            if (xElement == null)
            {
            }
            XElement xElement2 = xElement.Element(XName.Get("tcBorders", DocX.w.NamespaceName));

            if (xElement2 == null)
            {
            }
            string text = borderType.ToString();
            string a    = text;

            text = ((a == "TopLeftToBottomRight") ? "tl2br" : ((!(a == "TopRightToBottomLeft")) ? (text.Substring(0, 1).ToLower() + text.Substring(1)) : "tr2bl"));
            XElement xElement3 = xElement2.Element(XName.Get(text, DocX.w.NamespaceName));

            if (xElement3 == null)
            {
            }
            XAttribute xAttribute = xElement3.Attribute(XName.Get("val", DocX.w.NamespaceName));

            if (xAttribute != null)
            {
                try
                {
                    string value = "Tcbs_" + xAttribute.Value;
                    border.Tcbs = (BorderStyle)Enum.Parse(typeof(BorderStyle), value);
                }
                catch
                {
                    xAttribute.Remove();
                }
            }
            XAttribute xAttribute2 = xElement3.Attribute(XName.Get("sz", DocX.w.NamespaceName));

            if (xAttribute2 != null)
            {
                if (int.TryParse(xAttribute2.Value, out int result))
                {
                    switch (result)
                    {
                    case 2:
                        border.Size = BorderSize.one;
                        break;

                    case 4:
                        border.Size = BorderSize.two;
                        break;

                    case 6:
                        border.Size = BorderSize.three;
                        break;

                    case 8:
                        border.Size = BorderSize.four;
                        break;

                    case 12:
                        border.Size = BorderSize.five;
                        break;

                    case 18:
                        border.Size = BorderSize.six;
                        break;

                    case 24:
                        border.Size = BorderSize.seven;
                        break;

                    case 36:
                        border.Size = BorderSize.eight;
                        break;

                    case 48:
                        border.Size = BorderSize.nine;
                        break;

                    default:
                        border.Size = BorderSize.one;
                        break;
                    }
                }
                else
                {
                    xAttribute2.Remove();
                }
            }
            XAttribute xAttribute3 = xElement3.Attribute(XName.Get("space", DocX.w.NamespaceName));

            if (xAttribute3 != null)
            {
                if (!int.TryParse(xAttribute3.Value, out int result2))
                {
                    xAttribute3.Remove();
                }
                else
                {
                    border.Space = result2;
                }
            }
            XAttribute xAttribute4 = xElement3.Attribute(XName.Get("color", DocX.w.NamespaceName));

            if (xAttribute4 != null)
            {
                try
                {
                    border.Color = ColorTranslator.FromHtml($"#{xAttribute4.Value}");
                }
                catch
                {
                    xAttribute4.Remove();
                }
            }
            return(border);
        }
Ejemplo n.º 2
0
        public void SetBorder(TableCellBorderType borderType, Border border)
        {
            XElement xElement = base.Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));

            if (xElement == null)
            {
                base.Xml.SetElementValue(XName.Get("tcPr", DocX.w.NamespaceName), string.Empty);
                xElement = base.Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
            }
            XElement xElement2 = xElement.Element(XName.Get("tcBorders", DocX.w.NamespaceName));

            if (xElement2 == null)
            {
                xElement.SetElementValue(XName.Get("tcBorders", DocX.w.NamespaceName), string.Empty);
                xElement2 = xElement.Element(XName.Get("tcBorders", DocX.w.NamespaceName));
            }
            string text;

            switch (borderType)
            {
            case TableCellBorderType.TopLeftToBottomRight:
                text = "tl2br";
                break;

            case TableCellBorderType.TopRightToBottomLeft:
                text = "tr2bl";
                break;

            default:
                text = borderType.ToString();
                text = text.Substring(0, 1).ToLower() + text.Substring(1);
                break;
            }
            XElement xElement3 = xElement2.Element(XName.Get(borderType.ToString(), DocX.w.NamespaceName));

            if (xElement3 == null)
            {
                xElement2.SetElementValue(XName.Get(text, DocX.w.NamespaceName), string.Empty);
                xElement3 = xElement2.Element(XName.Get(text, DocX.w.NamespaceName));
            }
            string text2 = border.Tcbs.ToString().Substring(5);

            text2 = text2.Substring(0, 1).ToLower() + text2.Substring(1);
            xElement3.SetAttributeValue(XName.Get("val", DocX.w.NamespaceName), text2);
            int num;

            switch (border.Size)
            {
            case BorderSize.one:
                num = 2;
                break;

            case BorderSize.two:
                num = 4;
                break;

            case BorderSize.three:
                num = 6;
                break;

            case BorderSize.four:
                num = 8;
                break;

            case BorderSize.five:
                num = 12;
                break;

            case BorderSize.six:
                num = 18;
                break;

            case BorderSize.seven:
                num = 24;
                break;

            case BorderSize.eight:
                num = 36;
                break;

            case BorderSize.nine:
                num = 48;
                break;

            default:
                num = 2;
                break;
            }
            xElement3.SetAttributeValue(XName.Get("sz", DocX.w.NamespaceName), num.ToString());
            xElement3.SetAttributeValue(XName.Get("space", DocX.w.NamespaceName), border.Space.ToString());
            xElement3.SetAttributeValue(XName.Get("color", DocX.w.NamespaceName), border.Color.ToHex());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Set the table cell border
        /// Added by lckuiper @ 20101117
        /// </summary>
        /// <example>
        /// <code>
        /// // Create a new document.
        ///using (DocX document = DocX.Create("Test.docx"))
        ///{
        ///    // Insert a table into this document.
        ///    Table t = document.InsertTable(3, 3);
        ///
        ///    // Get the center cell.
        ///    Cell center = t.Rows[1].Cells[1];
        ///
        ///    // Create a large blue border.
        ///    Border b = new Border(BorderStyle.Tcbs_single, BorderSize.seven, 0, Color.Blue);
        ///
        ///    // Set the center cells Top, Bottom, Left and Right Borders to b.
        ///    center.SetBorder(TableCellBorderType.Top, b);
        ///    center.SetBorder(TableCellBorderType.Bottom, b);
        ///    center.SetBorder(TableCellBorderType.Left, b);
        ///    center.SetBorder(TableCellBorderType.Right, b);
        ///
        ///    // Save the document.
        ///    document.Save();
        ///}
        /// </code>
        /// </example>
        /// <param name="borderType">Table Cell border to set</param>
        /// <param name="border">Border object to set the table cell border</param>
        public void SetBorder(TableCellBorderType borderType, Border border)
        {
            /*
             * Get the tcPr (table cell properties) element for this Cell,
             * null will be return if no such element exists.
             */
            XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
            if (tcPr == null)
            {
                Xml.SetElementValue(XName.Get("tcPr", DocX.w.NamespaceName), string.Empty);
                tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
            }

            /*
             * Get the tblBorders (table cell borders) element for this Cell,
             * null will be return if no such element exists.
             */
            XElement tcBorders = tcPr.Element(XName.Get("tcBorders", DocX.w.NamespaceName));
            if (tcBorders == null)
            {
                tcPr.SetElementValue(XName.Get("tcBorders", DocX.w.NamespaceName), string.Empty);
                tcBorders = tcPr.Element(XName.Get("tcBorders", DocX.w.NamespaceName));
            }

            /*
             * Get the 'borderType' (table cell border) element for this Cell,
             * null will be return if no such element exists.
             */
            string tcbordertype;
            switch (borderType)
            {
                case TableCellBorderType.TopLeftToBottomRight:
                    tcbordertype = "tl2br";
                    break;
                case TableCellBorderType.TopRightToBottomLeft:
                    tcbordertype = "tr2bl";
                    break;
                default:
                    // enum to string
                    tcbordertype = borderType.ToString();
                    // only lower the first char of string (because of insideH and insideV)
                    tcbordertype = tcbordertype.Substring(0, 1).ToLower() + tcbordertype.Substring(1);
                    break;
            }

            XElement tcBorderType = tcBorders.Element(XName.Get(borderType.ToString(), DocX.w.NamespaceName));
            if (tcBorderType == null)
            {
                tcBorders.SetElementValue(XName.Get(tcbordertype, DocX.w.NamespaceName), string.Empty);
                tcBorderType = tcBorders.Element(XName.Get(tcbordertype, DocX.w.NamespaceName));
            }

            // get string value of border style
            string borderstyle = border.Tcbs.ToString().Substring(5);
            borderstyle = borderstyle.Substring(0, 1).ToLower() + borderstyle.Substring(1);

            // The val attribute is used for the border style
            tcBorderType.SetAttributeValue(XName.Get("val", DocX.w.NamespaceName), borderstyle);

            int size;
            switch (border.Size)
            {
                case BorderSize.one: size = 2; break;
                case BorderSize.two: size = 4; break;
                case BorderSize.three: size = 6; break;
                case BorderSize.four: size = 8; break;
                case BorderSize.five: size = 12; break;
                case BorderSize.six: size = 18; break;
                case BorderSize.seven: size = 24; break;
                case BorderSize.eight: size = 36; break;
                case BorderSize.nine: size = 48; break;
                default: size = 2; break;
            }

            // The sz attribute is used for the border size
            tcBorderType.SetAttributeValue(XName.Get("sz", DocX.w.NamespaceName), (size).ToString());

            // The space attribute is used for the cell spacing (probably '0')
            tcBorderType.SetAttributeValue(XName.Get("space", DocX.w.NamespaceName), (border.Space).ToString());

            // The color attribute is used for the border color
            tcBorderType.SetAttributeValue(XName.Get("color", DocX.w.NamespaceName), border.Color.ToHex());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Get a table cell border
        /// Added by lckuiper @ 20101117
        /// </summary>
        /// <param name="borderType">The table cell border to get</param>
        public Border GetBorder(TableCellBorderType borderType)
        {
            // instance with default border values
            var b = new Border();

            /*
             * Get the tcPr (table cell properties) element for this Cell,
             * null will be return if no such element exists.
             */
            XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
            if (tcPr == null)
            {
                // uses default border style
            }

            /*
             * Get the tcBorders (table cell borders) element for this Cell,
             * null will be return if no such element exists.
             */
            XElement tcBorders = tcPr.Element(XName.Get("tcBorders", DocX.w.NamespaceName));
            if (tcBorders == null)
            {
                // uses default border style
            }

            /*
             * Get the 'borderType' (cell border) element for this Cell,
             * null will be return if no such element exists.
             */
            var tcbordertype = borderType.ToString();

            switch (tcbordertype)
            {
                case "TopLeftToBottomRight":
                    tcbordertype = "tl2br";
                    break;
                case "TopRightToBottomLeft":
                    tcbordertype = "tr2bl";
                    break;
                default:
                    // only lower the first char of string (because of insideH and insideV)
                    tcbordertype = tcbordertype.Substring(0, 1).ToLower() + tcbordertype.Substring(1);
                    break;
            }

            XElement tcBorderType = tcBorders.Element(XName.Get(tcbordertype, DocX.w.NamespaceName));
            if (tcBorderType == null)
            {
                // uses default border style
            }

            // The val attribute is used for the border style
            XAttribute val = tcBorderType.Attribute(XName.Get("val", DocX.w.NamespaceName));
            // If val is null, this cell contains no border information.
            if (val == null)
            {
                // uses default border style
            }
            else
            {
                try
                {
                    string bordertype = "Tcbs_" + val.Value;
                    b.Tcbs = (BorderStyle)Enum.Parse(typeof(BorderStyle), bordertype);
                }

                catch
                {
                    val.Remove();
                    // uses default border style
                }
            }

            // The sz attribute is used for the border size
            XAttribute sz = tcBorderType.Attribute(XName.Get("sz", DocX.w.NamespaceName));
            // If sz is null, this border contains no size information.
            if (sz == null)
            {
                // uses default border style
            }
            else
            {
                // If sz is not an int, something is wrong with this attributes value, so remove it
                int numerical_size;
                if (!int.TryParse(sz.Value, out numerical_size))
                    sz.Remove();
                else
                {
                    switch (numerical_size)
                    {
                        case 2: b.Size = BorderSize.one; break;
                        case 4: b.Size = BorderSize.two; break;
                        case 6: b.Size = BorderSize.three; break;
                        case 8: b.Size = BorderSize.four; break;
                        case 12: b.Size = BorderSize.five; break;
                        case 18: b.Size = BorderSize.six; break;
                        case 24: b.Size = BorderSize.seven; break;
                        case 36: b.Size = BorderSize.eight; break;
                        case 48: b.Size = BorderSize.nine; break;
                        default: b.Size = BorderSize.one; break;
                    }
                }
            }

            // The space attribute is used for the border spacing (probably '0')
            XAttribute space = tcBorderType.Attribute(XName.Get("space", DocX.w.NamespaceName));
            // If space is null, this border contains no space information.
            if (space == null)
            {
                // uses default border style
            }
            else
            {
                // If space is not an int, something is wrong with this attributes value, so remove it
                int borderspace;
                if (!int.TryParse(space.Value, out borderspace))
                {
                    space.Remove();
                    // uses default border style
                }
                else
                {
                    b.Space = borderspace;
                }
            }

            // The color attribute is used for the border color
            XAttribute color = tcBorderType.Attribute(XName.Get("color", DocX.w.NamespaceName));
            if (color == null)
            {
                // uses default border style
            }
            else
            {
                // If color is not a Color, something is wrong with this attributes value, so remove it
                try
                {
                    b.Color = ColorTranslator.FromHtml(string.Format("#{0}", color.Value));
                }
                catch
                {
                    color.Remove();
                    // uses default border style
                }
            }
            return b;
        }
Ejemplo n.º 5
0
        public Table SetTableCellStyle(Table tbl, int x, int y, string sCellStyleAll)
        {
            if (x >= tbl.Rows.Count || x < 0)
            {
                return(tbl);
            }
            else
            {
                if (y >= tbl.Rows[x].Cells.Count || (y != -1 && y < 0))
                {
                    return(tbl);
                }
            }
            string[] aCellStyle;//由属性串分割成的属性数组
            string   sStyleName  = "";
            string   sStyleValue = "";

            aCellStyle = sCellStyleAll.Split(';');
            for (int m = 0; m < aCellStyle.Length; m++)
            {
                if (aCellStyle[m].IndexOf(':') < 0)
                {
                    continue;
                }

                //sStyleName = aCellStyle[m].Substring(0, aCellStyle[m].IndexOf(':'));
                //sStyleValue = aCellStyle[m].Substring(aCellStyle[m].IndexOf(':') + 1);
                string[] aCellStyleOne = aCellStyle[m].Split(':');
                sStyleName  = aCellStyleOne[0];
                sStyleValue = aCellStyleOne[1];

                BorderStyle BorderStyle_Tcbs = BorderStyle.Tcbs_single;
                //BorderStyle BorderStyle_Tcbs = BorderStyle.Tcbs_dotted;
                BorderSize           BorderStyle_Size  = BorderSize.one;
                int                  BorderStyle_Space = 0;
                System.Drawing.Color BorderStyle_Color = System.Drawing.Color.Black;
                TableCellBorderType  tblCellBorderType = TableCellBorderType.Left;

                if (aCellStyleOne.Length > 2)
                {
                    for (int p = 2; p < aCellStyleOne.Length; p++)
                    {
                        string sBorderProperty = aCellStyleOne[p].Substring(0, aCellStyleOne[p].IndexOf('_'));
                        string sBorderValue    = aCellStyleOne[p].Substring(aCellStyleOne[p].IndexOf('_') + 1);
                        if (sBorderProperty.ToUpper() == "BORDERSTYLE")//调边线样式
                        {
                            if (sBorderValue.ToUpper() == "DOTTED")
                            {
                                BorderStyle_Tcbs = BorderStyle.Tcbs_dotted;
                            }
                            else if (sBorderValue.ToUpper() == "NONE")
                            {
                                BorderStyle_Tcbs = BorderStyle.Tcbs_none;
                            }
                            else if (sBorderValue.ToUpper() == "SINGLE")
                            {
                                BorderStyle_Tcbs = BorderStyle.Tcbs_single;
                            }
                        }
                        else if (sBorderProperty.ToUpper() == "BORDERSIZE")//调边线粗细
                        {
                        }
                        else if (sBorderProperty.ToUpper() == "BORDERCOLOR")//调边线颜色
                        {
                            if (sBorderValue.ToUpper() == "RED")
                            {
                                BorderStyle_Color = System.Drawing.Color.Red;
                            }
                            else if (sBorderValue.ToUpper() == "WHITE")
                            {
                                BorderStyle_Color = System.Drawing.Color.White;
                            }
                        }
                    }
                }

                if (sStyleName.ToUpper() == "BORDER")
                {
                    if (sStyleValue.ToUpper() == "LEFT")
                    {
                        tblCellBorderType = TableCellBorderType.Left;
                    }
                    else if (sStyleValue.ToUpper() == "TOP")
                    {
                        tblCellBorderType = TableCellBorderType.Top;
                    }
                    else if (sStyleValue.ToUpper() == "RIGHT")
                    {
                        tblCellBorderType = TableCellBorderType.Right;
                    }
                    else if (sStyleValue.ToUpper() == "BOTTOM")
                    {
                        tblCellBorderType = TableCellBorderType.Bottom;
                    }

                    if (y == -1)
                    {
                        for (int z = 0; z < tbl.Rows[x].Cells.Count; z++)
                        {
                            tbl.Rows[x].Cells[z].SetBorder(tblCellBorderType, new Border(BorderStyle_Tcbs, BorderStyle_Size, BorderStyle_Space, BorderStyle_Color));
                        }
                    }
                    else
                    {
                        tbl.Rows[x].Cells[y].SetBorder(tblCellBorderType, new Border(BorderStyle_Tcbs, BorderStyle_Size, BorderStyle_Space, BorderStyle_Color));
                    }
                }

                if (sStyleName.ToUpper() == "PARAGRAPHALIGN")
                {
                    if (sStyleValue.ToUpper() == "LEFT")
                    {
                        if (y == -1)
                        {
                            for (int z = 0; z < tbl.Rows[x].Cells.Count; z++)
                            {
                                tbl.Rows[x].Cells[z].Paragraphs[0].Alignment = Alignment.left;
                            }
                        }
                        else
                        {
                            tbl.Rows[x].Cells[y].Paragraphs[0].Alignment = Alignment.left;
                        }
                    }
                    else if (sStyleValue.ToUpper() == "RIGHT")
                    {
                        if (y == -1)
                        {
                            for (int z = 0; z < tbl.Rows[x].Cells.Count; z++)
                            {
                                tbl.Rows[x].Cells[z].Paragraphs[0].Alignment = Alignment.right;
                            }
                        }
                        else
                        {
                            tbl.Rows[x].Cells[y].Paragraphs[0].Alignment = Alignment.right;
                        }
                    }
                    else if (sStyleValue.ToUpper() == "CENTER")
                    {
                        if (y == -1)
                        {
                            for (int z = 0; z < tbl.Rows[x].Cells.Count; z++)
                            {
                                tbl.Rows[x].Cells[z].Paragraphs[0].Alignment = Alignment.center;
                            }
                        }
                        else
                        {
                            tbl.Rows[x].Cells[y].Paragraphs[0].Alignment = Alignment.center;
                        }
                    }
                    else if (sStyleValue.ToUpper() == "VTOP")
                    {
                        if (y == -1)
                        {
                            for (int z = 0; z < tbl.Rows[x].Cells.Count; z++)
                            {
                                tbl.Rows[x].Cells[z].VerticalAlignment = VerticalAlignment.Top;
                            }
                        }
                        else
                        {
                            tbl.Rows[x].Cells[y].VerticalAlignment = VerticalAlignment.Top;
                        }
                    }
                    else if (sStyleValue.ToUpper() == "VCENTER")
                    {
                        if (y == -1)
                        {
                            for (int z = 0; z < tbl.Rows[x].Cells.Count; z++)
                            {
                                tbl.Rows[x].Cells[z].VerticalAlignment = VerticalAlignment.Center;
                            }
                        }
                        else
                        {
                            tbl.Rows[x].Cells[y].VerticalAlignment = VerticalAlignment.Center;
                        }
                    }
                    else if (sStyleValue.ToUpper() == "VBOTTOM")
                    {
                        if (y == -1)
                        {
                            for (int z = 0; z < tbl.Rows[x].Cells.Count; z++)
                            {
                                tbl.Rows[x].Cells[z].VerticalAlignment = VerticalAlignment.Bottom;
                            }
                        }
                        else
                        {
                            tbl.Rows[x].Cells[y].VerticalAlignment = VerticalAlignment.Bottom;
                        }
                    }
                    else if (sStyleValue.ToUpper() == "BOTH")
                    {
                        if (y == -1)
                        {
                            for (int z = 0; z < tbl.Rows[x].Cells.Count; z++)
                            {
                                tbl.Rows[x].Cells[z].Paragraphs[0].Alignment = Alignment.both;
                            }
                        }
                        else
                        {
                            tbl.Rows[x].Cells[y].Paragraphs[0].Alignment = Alignment.both;
                        }
                    }
                }
            }

            return(tbl);
        }