Sfoglia il codice sorgente

Implemented the cell margins of Table

master
Sergii Zhevzhyk 10 anni fa
parent
commit
03d8bd6c99
3 ha cambiato i file con 115 aggiunte e 0 eliminazioni
  1. 32
    0
      DocX/Table.cs
  2. 24
    0
      DocX/_Enumerations.cs
  3. 59
    0
      UnitTests/DocXUnitTests.cs

+ 32
- 0
DocX/Table.cs Vedi File

@@ -232,6 +232,38 @@ namespace Novacode
return tblPr;
}
/// <summary>
/// Set the specified cell margin for the table-level.
/// </summary>
/// <param name="type">The side of the cell margin.</param>
/// <param name="margin">The value for the specified cell margin.</param>
/// <remarks>More information can be found <see cref="http://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.tablecellmargindefault.aspx">here</see></remarks>
public void SetTableCellMargin(TableCellMarginType type, double margin)
{
XElement tblPr = GetOrCreate_tblPr();
// find (or create) the element with the cell margins
XElement tblCellMar = tblPr.Element(XName.Get("tblCellMar", DocX.w.NamespaceName));
if (tblCellMar == null)
{
tblPr.AddFirst(new XElement(XName.Get("tblCellMar", DocX.w.NamespaceName)));
tblCellMar = tblPr.Element(XName.Get("tblCellMar", DocX.w.NamespaceName));
}
// find (or create) the element with cell margin for the specified side
XElement tblMargin = tblCellMar.Element(XName.Get(type.ToString(), DocX.w.NamespaceName));
if (tblMargin == null)
{
tblCellMar.AddFirst(new XElement(XName.Get(type.ToString(), DocX.w.NamespaceName)));
tblMargin = tblCellMar.Element(XName.Get(type.ToString(), DocX.w.NamespaceName));
}
tblMargin.RemoveAttributes();
// set the value for the cell margin
tblMargin.Add(new XAttribute(XName.Get("w", DocX.w.NamespaceName), margin));
// set the side of cell margin
tblMargin.Add(new XAttribute(XName.Get("type", DocX.w.NamespaceName), "dxa"));
}
/// <summary>
/// Gets the column width for a given column index.

+ 24
- 0
DocX/_Enumerations.cs Vedi File

@@ -663,6 +663,30 @@ namespace Novacode
Auto,
None
}
/// <summary>
/// Cell margin for all sides of the table cell.
/// </summary>
public enum TableCellMarginType
{
/// <summary>
/// The left cell margin.
/// </summary>
left,
/// <summary>
/// The right cell margin.
/// </summary>
right,
/// <summary>
/// The bottom cell margin.
/// </summary>
bottom,
/// <summary>
/// The top cell margin.
/// </summary>
top
}
public enum HeadingType
{
[Description("Heading1")]

+ 59
- 0
UnitTests/DocXUnitTests.cs Vedi File

@@ -2768,6 +2768,65 @@ namespace UnitTests
Assert.AreEqual("22", szCsValue);
}
}
[TestMethod]
public void SetTableCellMargin_WhenSetTwoCellMargins_ContainsTwoCellMargins()
{
using (var document = DocX.Create("Set table cell margins.docx"))
{
Table table = document.InsertTable(1,1);
table.SetTableCellMargin(TableCellMarginType.left, 20);
table.SetTableCellMargin(TableCellMarginType.right, 40);
var elements = table.Xml.Descendants(XName.Get("tblCellMar", DocX.w.NamespaceName)).ToList();
// should contain only one element named tblCellMar
Assert.AreEqual(1, elements.Count);
var element = elements.First();
// should contain two elements defining the margins
Assert.AreEqual(2, element.Elements().Count());
var left = element.Element(XName.Get("left", DocX.w.NamespaceName));
var right = element.Element(XName.Get("right", DocX.w.NamespaceName));
Assert.IsNotNull(left);
Assert.IsNotNull(right);
Assert.AreEqual("20", left.Attribute(XName.Get("w", DocX.w.NamespaceName)).Value);
Assert.AreEqual("dxa", left.Attribute(XName.Get("type", DocX.w.NamespaceName)).Value);
Assert.AreEqual("40", right.Attribute(XName.Get("w", DocX.w.NamespaceName)).Value);
Assert.AreEqual("dxa", right.Attribute(XName.Get("type", DocX.w.NamespaceName)).Value);
}
}
[TestMethod]
public void SetTableCellMargin_WhenReSetCellMargin_ContainsOneCellMargin()
{
using (var document = DocX.Create("Set table cell margin.docx"))
{
Table table = document.InsertTable(1, 1);
table.SetTableCellMargin(TableCellMarginType.left, 20);
// change the table cell margin
table.SetTableCellMargin(TableCellMarginType.left, 40);
var elements = table.Xml.Descendants(XName.Get("tblCellMar", DocX.w.NamespaceName)).ToList();
// should contain only one element named tblCellMar
Assert.AreEqual(1, elements.Count);
var element = elements.First();
// should contain two elements defining the margins
Assert.AreEqual(1, element.Elements().Count());
var left = element.Element(XName.Get("left", DocX.w.NamespaceName));
Assert.IsNotNull(left);
// check that the last value is taken
Assert.AreEqual("40", left.Attribute(XName.Get("w", DocX.w.NamespaceName)).Value);
Assert.AreEqual("dxa", left.Attribute(XName.Get("type", DocX.w.NamespaceName)).Value);
}
}
}
}

Loading…
Annulla
Salva