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;
}