예제 #1
0
파일: Table.cs 프로젝트: WordDocX/DocX
        /// <summary>
        /// Set a table 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);
        ///
        ///    // Create a large blue border.
        ///    Border b = new Border(BorderStyle.Tcbs_single, BorderSize.seven, 0, Color.Blue);
        ///
        ///    // Set the tables Top, Bottom, Left and Right Borders to b.
        ///    t.SetBorder(TableBorderType.Top, b);
        ///    t.SetBorder(TableBorderType.Bottom, b);
        ///    t.SetBorder(TableBorderType.Left, b);
        ///    t.SetBorder(TableBorderType.Right, b);
        ///
        ///    // Save the document.
        ///    document.Save();
        ///}
        /// </code>
        /// </example>
        /// <param name="borderType">The table border to set</param>
        /// <param name="border">Border object to set the table border</param>
        public void SetBorder(TableBorderType borderType, Border border)
        {
            /*
             * Get the tblPr (table properties) element for this Table,
             * null will be return if no such element exists.
             */
            XElement tblPr = Xml.Element(XName.Get("tblPr", DocX.w.NamespaceName));
            if (tblPr == null)
            {
                Xml.SetElementValue(XName.Get("tblPr", DocX.w.NamespaceName), string.Empty);
                tblPr = Xml.Element(XName.Get("tblPr", DocX.w.NamespaceName));
            }

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

            /*
             * Get the 'borderType' (table border) element for this Table,
             * null will be return if no such element exists.
             */
            var tbordertype = borderType.ToString();
            // only lower the first char of string (because of insideH and insideV)
            tbordertype = tbordertype.Substring(0, 1).ToLower() + tbordertype.Substring(1);

            XElement tblBorderType = tblBorders.Element(XName.Get(borderType.ToString(), DocX.w.NamespaceName));
            if (tblBorderType == null)
            {
                tblBorders.SetElementValue(XName.Get(tbordertype, DocX.w.NamespaceName), string.Empty);
                tblBorderType = tblBorders.Element(XName.Get(tbordertype, 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
            tblBorderType.SetAttributeValue(XName.Get("val", DocX.w.NamespaceName), borderstyle);

            if (border.Tcbs != BorderStyle.Tcbs_nil)
            {
                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
                tblBorderType.SetAttributeValue(XName.Get("sz", DocX.w.NamespaceName), (size).ToString());

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

                // The color attribute is used for the border color
                tblBorderType.SetAttributeValue(XName.Get("color", DocX.w.NamespaceName), border.Color.ToHex());
            }
        }
예제 #2
0
파일: Table.cs 프로젝트: WordDocX/DocX
        /// <summary>
        /// Get a table border
        /// Added by lckuiper @ 20101117
        /// </summary>
        /// <param name="borderType">The table border to get</param>
        public Border GetBorder(TableBorderType borderType)
        {
            // instance with default border values
            Border b = new Border();

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

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

            /*
             * Get the 'borderType' (table border) element for this Table,
             * null will be return if no such element exists.
             */
            var tbordertype = borderType.ToString();
            // only lower the first char of string (because of insideH and insideV)
            tbordertype = tbordertype.Substring(0, 1).ToLower() + tbordertype.Substring(1);

            XElement tblBorderType = tblBorders.Element(XName.Get(tbordertype, DocX.w.NamespaceName));
            if (tblBorderType == null)
            {
                // uses default border style
            }

            // The val attribute is used for the border style
            XAttribute val = tblBorderType.Attribute(XName.Get("val", DocX.w.NamespaceName));
            // If val is null, this table 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 = tblBorderType.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 = tblBorderType.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 = tblBorderType.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;
        }