Procházet zdrojové kódy

This patch is to add "nil" to table borders, this is different from "none" - see enumerations.

This patch also adds the ability to set a row to be a table header and have that header repeated on every page.

Patch provided by bbc_ben
master
MadBoy_cp před 12 roky
rodič
revize
49e1db1d22
3 změnil soubory, kde provedl 130 přidání a 77 odebrání
  1. 15
    4
      DocX/HelperFunctions.cs
  2. 113
    72
      DocX/Table.cs
  3. 2
    1
      DocX/_Enumerations.cs

+ 15
- 4
DocX/HelperFunctions.cs Zobrazit soubor

@@ -9,6 +9,7 @@ using System.Reflection;
using System.Security.Principal;
using System.Text;
using System.Xml.Linq;
using System.Xml;
namespace Novacode
{
@@ -395,7 +396,17 @@ namespace Novacode
);
}
internal static XElement CreateTable(int rowCount, int columnCount)
internal static XElement CreateTable(int rowCount, int columnCount)
{
int[] columnWidths = new int[columnCount];
for (int i = 0; i < columnCount; i++)
{
columnWidths[i] = 2310;
}
return CreateTable(rowCount, columnWidths);
}

internal static XElement CreateTable(int rowCount, int[] columnWidths)
{
XElement newTable =
new XElement
@@ -411,8 +422,8 @@ namespace Novacode
);
XElement tableGrid = new XElement(XName.Get("tblGrid", DocX.w.NamespaceName));
for (int i = 0; i < columnCount; i++)
tableGrid.Add(new XElement(XName.Get("gridCol", DocX.w.NamespaceName), new XAttribute(XName.Get("w", DocX.w.NamespaceName), "2310")));
for (int i = 0; i < columnWidths.Length; i++)
tableGrid.Add(new XElement(XName.Get("gridCol", DocX.w.NamespaceName), new XAttribute(XName.Get("w", DocX.w.NamespaceName), XmlConvert.ToString(columnWidths[i]))));
newTable.Add(tableGrid);
@@ -420,7 +431,7 @@ namespace Novacode
{
XElement row = new XElement(XName.Get("tr", DocX.w.NamespaceName));
for (int j = 0; j < columnCount; j++)
for (int j = 0; j < columnWidths.Length; j++)
{
XElement cell = CreateTableCell();
row.Add(cell);

+ 113
- 72
DocX/Table.cs Zobrazit soubor

@@ -1797,77 +1797,80 @@ namespace Novacode
/// </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.
*/
string tbordertype;
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);
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), ColorTranslator.ToHtml(border.Color));
}
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.
*/
string tbordertype;
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), ColorTranslator.ToHtml(border.Color));
}
}
/// <summary>
/// Get a table border
@@ -2179,7 +2182,45 @@ namespace Novacode
}
}
/// <summary>
/// Set to true to make this row the table header row that will be repeated on each page
/// </summary>
public bool TableHeader
{
get
{
XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
XElement tblHeader = trPr.Element(XName.Get("tblHeader", DocX.w.NamespaceName));
if (tblHeader == null)
{
return false;
}
else
{
return true;
}
}
set
{
XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
if (trPr == null)
{
Xml.SetElementValue(XName.Get("trPr", DocX.w.NamespaceName), string.Empty);
trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
}
XElement tblHeader = trPr.Element(XName.Get("tblHeader", DocX.w.NamespaceName));
if (tblHeader == null && value)
{
trPr.SetElementValue(XName.Get("tblHeader", DocX.w.NamespaceName), string.Empty);
}
if (tblHeader != null && !value)
{
tblHeader.Remove();
}
}
}
/// <summary>
/// Allow row to break across pages.
/// The default value is true: Word will break the contents of the row across pages.

+ 2
- 1
DocX/_Enumerations.cs Zobrazit soubor

@@ -89,7 +89,8 @@
Tcbs_threeDEmboss,
Tcbs_threeDEngrave,
Tcbs_outset,
Tcbs_inset
Tcbs_inset,
Tcbs_nil
}
/// <summary>

Načítá se…
Zrušit
Uložit