ソースを参照

Refactor the Table class: RowCount, ColumnCount, Rows properties and Insert/Remove methods. Also refactor CreateTable method - extract the same method CreateTableCell. Add powerfull unit test for this.

master
DragonFire_cp 14年前
コミット
a51588f35e
5個のファイルの変更228行の追加185行の削除
  1. 18
    9
      DocX/HelperFunctions.cs
  2. 136
    129
      DocX/Table.cs
  3. 0
    1
      Examples/Examples.csproj
  4. バイナリ
      Examples/bin/Debug/docs/Input.docx
  5. 74
    46
      UnitTests/UnitTest1.cs

+ 18
- 9
DocX/HelperFunctions.cs ファイルの表示

@@ -323,15 +323,7 @@ namespace Novacode
for (int j = 0; j < coloumnCount; j++)
{
XElement cell =
new XElement
(
XName.Get("tc", DocX.w.NamespaceName),
new XElement(XName.Get("tcPr", DocX.w.NamespaceName),
new XElement(XName.Get("tcW", DocX.w.NamespaceName), new XAttribute(XName.Get("w", DocX.w.NamespaceName), "2310"), new XAttribute(XName.Get("type", DocX.w.NamespaceName), "dxa"))),
new XElement(XName.Get("p", DocX.w.NamespaceName), new XElement(XName.Get("pPr", DocX.w.NamespaceName)))
);
XElement cell = CreateTableCell();
row.Add(cell);
}
@@ -340,6 +332,23 @@ namespace Novacode
return newTable;
}
/// <summary>
/// Create and return a cell of a table
/// </summary>
internal static XElement CreateTableCell()
{
return new XElement
(
XName.Get("tc", DocX.w.NamespaceName),
new XElement(XName.Get("tcPr", DocX.w.NamespaceName),
new XElement(XName.Get("tcW", DocX.w.NamespaceName),
new XAttribute(XName.Get("w", DocX.w.NamespaceName), "2310"),
new XAttribute(XName.Get("type", DocX.w.NamespaceName), "dxa"))),
new XElement(XName.Get("p", DocX.w.NamespaceName),
new XElement(XName.Get("pPr", DocX.w.NamespaceName)))
);
}
internal static void RenumberIDs(DocX document)
{
IEnumerable<XAttribute> trackerIDs =

+ 136
- 129
DocX/Table.cs ファイルの表示

@@ -11,7 +11,7 @@ using System.Drawing;
using System.Globalization;
namespace Novacode
{
{
/// <summary>
/// Represents a Table in a document.
/// </summary>
@@ -19,8 +19,6 @@ namespace Novacode
{
private Alignment alignment;
private AutoFit autofit;
private List<Row> rows;
private int rowCount, columnCount;
/// <summary>
/// Returns a list of all Paragraphs inside this container.
@@ -97,7 +95,7 @@ namespace Novacode
{
XElement tblPr = GetOrCreate_tblPr();
tblPr.Add(new XElement(DocX.w + "bidiVisual"));
foreach (Row r in Rows)
r.SetDirection(direction);
}
@@ -158,41 +156,52 @@ namespace Novacode
/// <summary>
/// Returns the number of rows in this table.
/// </summary>
public int RowCount { get { return rowCount; } }
public Int32 RowCount
{
get
{
return Xml.Elements(XName.Get("tr", DocX.w.NamespaceName)).Count();
}
}
/// <summary>
/// Returns the number of coloumns in this table.
/// </summary>
public int ColumnCount { get { return columnCount; } }
public Int32 ColumnCount
{
get
{
if (RowCount == 0)
return 0;
return Xml.Elements(XName.Get("tr", DocX.w.NamespaceName)) // select rows
.First() // get first row
.Elements(XName.Get("tc", DocX.w.NamespaceName)).Count(); // return column count from first row
}
}
/// <summary>
/// Returns a list of rows in this table.
/// </summary>
public List<Row> Rows
{
get
public List<Row> Rows
{
get
{
List<Row> rows =
List<Row> rows =
(
from r in Xml.Elements(XName.Get("tr", DocX.w.NamespaceName))
select new Row(this, Document, r)
).ToList();
rowCount = rows.Count;
if (rows.Count > 0)
if (rows[0].Cells.Count > 0)
columnCount = rows[0].Cells.Count;
return rows;
}
}
}
private TableDesign design;
internal PackagePart mainPart;
internal Table(DocX document, XElement xml):base(document, xml)
internal Table(DocX document, XElement xml)
: base(document, xml)
{
autofit = AutoFit.ColoumnWidth;
this.Xml = xml;
@@ -233,35 +242,35 @@ namespace Novacode
switch (value)
{
case Alignment.left:
{
alignmentString = "left";
break;
}
{
alignmentString = "left";
break;
}
case Alignment.both:
{
alignmentString = "both";
break;
}
{
alignmentString = "both";
break;
}
case Alignment.right:
{
alignmentString = "right";
break;
}
{
alignmentString = "right";
break;
}
case Alignment.center:
{
alignmentString = "center";
break;
}
{
alignmentString = "center";
break;
}
}
XElement tblPr = Xml.Descendants(XName.Get("tblPr", DocX.w.NamespaceName)).First();
XElement jc = tblPr.Descendants(XName.Get("jc", DocX.w.NamespaceName)).FirstOrDefault();
if(jc != null)
if (jc != null)
jc.Remove();
jc = new XElement(XName.Get("jc", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), alignmentString));
@@ -275,30 +284,30 @@ namespace Novacode
/// </summary>
public AutoFit AutoFit
{
get{return autofit;}
get { return autofit; }
set
{
string attributeValue = string.Empty;
switch(value)
switch (value)
{
case AutoFit.ColoumnWidth:
{
attributeValue = "dxa";
break;
}
{
attributeValue = "dxa";
break;
}
case AutoFit.Contents:
{
attributeValue = "auto";
break;
}
{
attributeValue = "auto";
break;
}
case AutoFit.Window:
{
attributeValue = "pct";
break;
}
{
attributeValue = "pct";
break;
}
}
var query = from d in Xml.Descendants()
@@ -315,7 +324,7 @@ namespace Novacode
/// <summary>
/// The design\style to apply to this table.
/// </summary>
public TableDesign Design
public TableDesign Design
{
get { return design; }
set
@@ -329,7 +338,7 @@ namespace Novacode
}
XAttribute val = style.Attribute(XName.Get("val", DocX.w.NamespaceName));
if(val == null)
if (val == null)
{
style.Add(new XAttribute(XName.Get("val", DocX.w.NamespaceName), ""));
val = style.Attribute(XName.Get("val", DocX.w.NamespaceName));
@@ -486,38 +495,6 @@ namespace Novacode
}
}
/// <summary>
/// Insert a row at the end of this table.
/// </summary>
/// <example>
/// <code>
/// // Load a document.
/// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
/// {
/// // Get the first table in this document.
/// Table table = document.Tables[0];
///
/// // Insert a new row at the end of this table.
/// Row row = table.InsertRow();
///
/// // Loop through each cell in this new row.
/// foreach (Cell c in row.Cells)
/// {
/// // Set the text of each new cell to "Hello".
/// c.Paragraphs[0].InsertText("Hello", false);
/// }
///
/// // Save the document to a new file.
/// document.SaveAs(@"C:\Example\Test2.docx");
/// }// Release this document from memory.
/// </code>
/// </example>
/// <returns>A new row.</returns>
public Row InsertRow()
{
return InsertRow(Rows.Count);
}
/// <summary>
/// Returns the index of this Table.
/// </summary>
@@ -586,6 +563,38 @@ namespace Novacode
Xml.Remove();
}
/// <summary>
/// Insert a row at the end of this table.
/// </summary>
/// <example>
/// <code>
/// // Load a document.
/// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
/// {
/// // Get the first table in this document.
/// Table table = document.Tables[0];
///
/// // Insert a new row at the end of this table.
/// Row row = table.InsertRow();
///
/// // Loop through each cell in this new row.
/// foreach (Cell c in row.Cells)
/// {
/// // Set the text of each new cell to "Hello".
/// c.Paragraphs[0].InsertText("Hello", false);
/// }
///
/// // Save the document to a new file.
/// document.SaveAs(@"C:\Example\Test2.docx");
/// }// Release this document from memory.
/// </code>
/// </example>
/// <returns>A new row.</returns>
public Row InsertRow()
{
return InsertRow(RowCount);
}
/// <summary>
/// Insert a column to the right of a Table.
/// </summary>
@@ -625,7 +634,7 @@ namespace Novacode
/// </example>
public void InsertColumn()
{
InsertColumn(columnCount);
InsertColumn(ColumnCount);
}
/// <summary>
@@ -650,7 +659,7 @@ namespace Novacode
/// </example>
public void RemoveRow()
{
RemoveRow(rowCount - 1);
RemoveRow(RowCount - 1);
}
/// <summary>
@@ -676,11 +685,10 @@ namespace Novacode
/// </example>
public void RemoveRow(int index)
{
if (index < 0 || index > Rows.Count)
if (index < 0 || index > RowCount - 1)
throw new IndexOutOfRangeException();
Rows[index].Xml.Remove();
if (Rows.Count == 0)
Remove();
}
@@ -707,7 +715,7 @@ namespace Novacode
/// </example>
public void RemoveColumn()
{
RemoveColumn(columnCount - 1);
RemoveColumn(ColumnCount - 1);
}
/// <summary>
@@ -733,7 +741,7 @@ namespace Novacode
/// </example>
public void RemoveColumn(int index)
{
if (index < 0 || index > columnCount - 1)
if (index < 0 || index > ColumnCount - 1)
throw new IndexOutOfRangeException();
foreach (Row r in Rows)
@@ -770,12 +778,15 @@ namespace Novacode
/// <returns>A new Row</returns>
public Row InsertRow(int index)
{
if (index < 0 || index > Rows.Count)
if (index < 0 || index > RowCount)
throw new IndexOutOfRangeException();
List<XElement> content = new List<XElement>();
for (int i = 0; i < columnCount; i++ )
content.Add(new XElement(XName.Get("tc", DocX.w.NamespaceName), new XElement(XName.Get("p", DocX.w.NamespaceName))));
for (int i = 0; i < ColumnCount; i++)
{
XElement cell = HelperFunctions.CreateTableCell();
content.Add(cell);
}
XElement e = new XElement(XName.Get("tr", DocX.w.NamespaceName), content);
Row newRow = new Row(this, Document, e);
@@ -786,14 +797,13 @@ namespace Novacode
rowXml = Rows.Last().Xml;
rowXml.AddAfterSelf(newRow.Xml);
}
else
{
rowXml = Rows[index].Xml;
rowXml.AddBeforeSelf(newRow.Xml);
}
rowCount = Rows.Count;
return newRow;
}
@@ -838,24 +848,19 @@ namespace Novacode
/// </example>
public void InsertColumn(int index)
{
if (Rows.Count > 0)
if (RowCount > 0)
{
foreach (Row r in Rows)
{
if(columnCount == index)
r.Cells[index - 1].Xml.AddAfterSelf(new XElement(XName.Get("tc", DocX.w.NamespaceName), new XElement(XName.Get("p", DocX.w.NamespaceName))));
// create cell
XElement cell = HelperFunctions.CreateTableCell();
// insert cell
if (r.Cells.Count == index)
r.Cells[index - 1].Xml.AddAfterSelf(cell);
else
r.Cells[index].Xml.AddBeforeSelf(new XElement(XName.Get("tc", DocX.w.NamespaceName), new XElement(XName.Get("p", DocX.w.NamespaceName))));
r.Cells[index].Xml.AddBeforeSelf(cell);
}
rows = (from r in Xml.Elements(XName.Get("tr", DocX.w.NamespaceName))
select new Row(this, Document, r)).ToList();
rowCount = Rows.Count;
if (rows.Count > 0)
if (rows[0].Cells.Count > 0)
columnCount = rows[0].Cells.Count;
}
}
@@ -1302,7 +1307,7 @@ namespace Novacode
/// </summary>
/// <example>
/// <code>
///// Create a new document.
/// // Create a new document.
///using (DocX document = DocX.Create("Test.docx"))
///{
/// // Insert a table into this document.
@@ -1546,24 +1551,24 @@ namespace Novacode
/// <summary>
/// A list of Cells in this Row.
/// </summary>
public List<Cell> Cells
{
get
public List<Cell> Cells
{
get
{
List<Cell> cells =
List<Cell> cells =
(
from c in Xml.Elements(XName.Get("tc", DocX.w.NamespaceName))
select new Cell(this, Document, c)
).ToList();
return cells;
}
}
}
public void Remove()
{
XElement table = Xml.Parent;
Xml.Remove();
if (table.Elements(XName.Get("tr", DocX.w.NamespaceName)).Count() == 0)
table.Remove();
@@ -1588,7 +1593,8 @@ namespace Novacode
internal Table table;
internal PackagePart mainPart;
internal Row(Table table, DocX document, XElement xml):base(document, xml)
internal Row(Table table, DocX document, XElement xml)
: base(document, xml)
{
this.table = table;
this.mainPart = table.mainPart;
@@ -1608,7 +1614,7 @@ namespace Novacode
XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
// If trPr is null, this row contains no height information.
if(trPr == null)
if (trPr == null)
return double.NaN;
/*
@@ -1616,7 +1622,7 @@ namespace Novacode
* null will be return if no such element exists.
*/
XElement trHeight = trPr.Element(XName.Get("trHeight", DocX.w.NamespaceName));
// If trHeight is null, this row contains no height information.
if (trHeight == null)
return double.NaN;
@@ -1683,7 +1689,7 @@ namespace Novacode
// The sum of all merged gridSpans.
int gridSpanSum = 0;
// Foreach each Cell between startIndex and endIndex inclusive.
foreach (Cell c in Cells.Where((z, i) => i > startIndex && i <= endIndex))
{
@@ -1704,7 +1710,7 @@ namespace Novacode
// Add this cells Pragraph to the merge start Cell.
Cells[startIndex].Xml.Add(c.Xml.Elements(XName.Get("p", DocX.w.NamespaceName)));
// Remove this Cell.
c.Xml.Remove();
}
@@ -1747,11 +1753,12 @@ namespace Novacode
}
}
public class Cell:Container
public class Cell : Container
{
internal Row row;
internal PackagePart mainPart;
internal Cell(Row row, DocX document, XElement xml):base(document, xml)
internal Cell(Row row, DocX document, XElement xml)
: base(document, xml)
{
this.row = row;
this.mainPart = row.mainPart;
@@ -1906,7 +1913,7 @@ namespace Novacode
// If shd is null, this cell contains no Color information.
if (shd == null)
return Color.White;
// Get the w attribute of the tcW element.
XAttribute fill = shd.Attribute(XName.Get("fill", DocX.w.NamespaceName));
@@ -1914,7 +1921,7 @@ namespace Novacode
if (fill == null)
return Color.White;
return ColorTranslator.FromHtml(string.Format("#{0}", fill.Value));
return ColorTranslator.FromHtml(string.Format("#{0}", fill.Value));
}
set
@@ -1978,7 +1985,7 @@ namespace Novacode
// If tcW is null, this cell contains no width information.
if (tcW == null)
return double.NaN;
// Get the w attribute of the tcW element.
XAttribute w = tcW.Attribute(XName.Get("w", DocX.w.NamespaceName));
@@ -2140,7 +2147,7 @@ namespace Novacode
tcMar.SetElementValue(XName.Get("left", DocX.w.NamespaceName), string.Empty);
tcMarLeft = tcMar.Element(XName.Get("left", DocX.w.NamespaceName));
}
// The type attribute needs to be set to dxa which represents "twips" or twentieths of a point. In other words, 1/1440th of an inch.
tcMarLeft.SetAttributeValue(XName.Get("type", DocX.w.NamespaceName), "dxa");
@@ -2600,7 +2607,7 @@ namespace Novacode
switch (border.Size)
{
case BorderSize.one: size = 2; break;
case BorderSize.two: size = 4; 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;
@@ -2799,8 +2806,8 @@ namespace Novacode
/// }
/// </code>
/// </example>
public Color FillColor
{
public Color FillColor
{
get
{
/*

+ 0
- 1
Examples/Examples.csproj ファイルの表示

@@ -62,7 +62,6 @@
<Content Include="bin\Debug\images\logo_the_happy_builder.png" />
</ItemGroup>
<ItemGroup>
<None Include="bin\Debug\docs\Input.docx" />
<None Include="Input.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>

バイナリ
Examples/bin/Debug/docs/Input.docx ファイルの表示


+ 74
- 46
UnitTests/UnitTest1.cs ファイルの表示

@@ -45,7 +45,7 @@ namespace UnitTests
{"COURT NAME","Fred Frump"},
{"Case Number","cr-md-2011-1234567"}
};
using (DocX replaceDoc = DocX.Load(directory_documents + "ReplaceTests.docx"))
{
foreach (var t in replaceDoc.Tables)
@@ -55,14 +55,14 @@ namespace UnitTests
Assert.IsTrue(t.Rows.Count == 1);
Assert.IsTrue(t.RowCount == 1);
}
// Make sure the origional strings are in the document.
Assert.IsTrue(replaceDoc.FindAll("<COURT NAME>").Count == 2);
Assert.IsTrue(replaceDoc.FindAll("<Case Number>").Count == 2);
// There are only two patterns, even though each pattern is used more than once
Assert.IsTrue(replaceDoc.FindUniqueByPattern(@"<[\w \=]{4,}>", RegexOptions.IgnoreCase).Count == 2);
// Make sure the new strings are not in the document.
Assert.IsTrue(replaceDoc.FindAll("Fred Frump").Count == 0);
Assert.IsTrue(replaceDoc.FindAll("cr-md-2011-1234567").Count == 0);
@@ -82,7 +82,7 @@ namespace UnitTests
// Make sure the replacement worked.
Assert.IsTrue(replaceDoc.Text == "\t\t\t\t\t\t\t\t\t\t\t\t\t\tThese two tables should look identical:\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tSTATE OF IOWA,\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tPlaintiff,\t\t\t\t\t\t\t\t\t\t\t\t\t\tvs.\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tFRED FRUMP,\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tDefendant.\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tCase No.: cr-md-2011-1234567\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tORDER SETTING ASIDE DEFAULT JUDGMENT\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tSTATE OF IOWA,\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tPlaintiff,\t\t\t\t\t\t\t\t\t\t\t\t\t\tvs.\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tFRED FRUMP,\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tDefendant.\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tCase No.: cr-md-2011-1234567\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tORDER SETTING ASIDE DEFAULT JUDGMENT\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t");
}
}
[TestMethod]
@@ -413,7 +413,7 @@ namespace UnitTests
{
// Extract Images from Document.
List<Novacode.Image> document_images = document.Images;
// Make sure there are 3 Images in this document.
Assert.IsTrue(document_images.Count() == 3);
@@ -703,21 +703,21 @@ namespace UnitTests
// Simple
Paragraph p1 = document.InsertParagraph("AC");
p1.InsertHyperlink(h); Assert.IsTrue(p1.Text == "linkAC");
p1.InsertHyperlink(h, p1.Text.Length); Assert.IsTrue(p1.Text == "linkAClink");
p1.InsertHyperlink(h); Assert.IsTrue(p1.Text == "linkAC");
p1.InsertHyperlink(h, p1.Text.Length); Assert.IsTrue(p1.Text == "linkAClink");
p1.InsertHyperlink(h, p1.Text.IndexOf("C")); Assert.IsTrue(p1.Text == "linkAlinkClink");
// Difficult
Paragraph p2 = document.InsertParagraph("\tA\tC\t");
p2.InsertHyperlink(h); Assert.IsTrue(p2.Text == "link\tA\tC\t");
p2.InsertHyperlink(h, p2.Text.Length); Assert.IsTrue(p2.Text == "link\tA\tC\tlink");
p2.InsertHyperlink(h); Assert.IsTrue(p2.Text == "link\tA\tC\t");
p2.InsertHyperlink(h, p2.Text.Length); Assert.IsTrue(p2.Text == "link\tA\tC\tlink");
p2.InsertHyperlink(h, p2.Text.IndexOf("C")); Assert.IsTrue(p2.Text == "link\tA\tlinkC\tlink");
// Contrived
// Add a contrived Hyperlink to this document.
Hyperlink h2 = document.AddHyperlink("\tlink\t", new Uri("http://www.google.com"));
Paragraph p3 = document.InsertParagraph("\tA\tC\t");
p3.InsertHyperlink(h2); Assert.IsTrue(p3.Text == "\tlink\t\tA\tC\t");
p3.InsertHyperlink(h2); Assert.IsTrue(p3.Text == "\tlink\t\tA\tC\t");
p3.InsertHyperlink(h2, p3.Text.Length); Assert.IsTrue(p3.Text == "\tlink\t\tA\tC\t\tlink\t");
p3.InsertHyperlink(h2, p3.Text.IndexOf("C")); Assert.IsTrue(p3.Text == "\tlink\t\tA\t\tlink\tC\t\tlink\t");
}
@@ -725,7 +725,7 @@ namespace UnitTests
[TestMethod]
public void Test_Paragraph_RemoveHyperlink()
{
{
// Create a new document
using (DocX document = DocX.Create("Test.docx"))
{
@@ -750,12 +750,12 @@ namespace UnitTests
// Try and remove a Hyperlink at an index greater than the last.
// This should throw an exception.
try
try
{
p1.RemoveHyperlink(3);
Assert.Fail();
}
catch (ArgumentException) {}
catch (ArgumentException) { }
catch (Exception) { Assert.Fail(); }
p1.RemoveHyperlink(0); Assert.IsTrue(p1.Text == "AlinkClink");
@@ -1056,63 +1056,91 @@ namespace UnitTests
}
[TestMethod]
public void Test_Table_InsertRow()
public void Test_Table_InsertRowAndColumn()
{
// Create a table
using (DocX document = DocX.Create(directory_documents + "Tables2.docx"))
{
// Add a Table to a document.
Table t = document.AddTable(2, 2);
t.Design = TableDesign.TableGrid;
t.Rows[0].Cells[0].Paragraphs[0].InsertText("X");
t.Rows[0].Cells[1].Paragraphs[0].InsertText("X");
t.Rows[1].Cells[0].Paragraphs[0].InsertText("X");
t.Rows[1].Cells[1].Paragraphs[0].InsertText("X");
// Insert the Table into the main section of the document.
Table t1 = document.InsertTable(t);
t1.InsertRow();
t1.InsertColumn();
// ... and add a column and a row
t1.InsertRow(1);
t1.InsertColumn(1);
// Save the document.
document.Save();
}
// Check table
using (DocX document = DocX.Load(directory_documents + "Tables2.docx"))
{
// Get a table from a document
Table t = document.Tables[0];
// Check that the table is equal this:
// X - X
// - - -
// X - X
Assert.AreEqual("X", t.Rows[0].Cells[0].Paragraphs[0].Text);
Assert.AreEqual("X", t.Rows[2].Cells[0].Paragraphs[0].Text);
Assert.AreEqual("X", t.Rows[0].Cells[2].Paragraphs[0].Text);
Assert.AreEqual("X", t.Rows[2].Cells[2].Paragraphs[0].Text);
Assert.IsTrue(String.IsNullOrEmpty(t.Rows[1].Cells[0].Paragraphs[0].Text));
Assert.IsTrue(String.IsNullOrEmpty(t.Rows[1].Cells[1].Paragraphs[0].Text));
Assert.IsTrue(String.IsNullOrEmpty(t.Rows[1].Cells[2].Paragraphs[0].Text));
Assert.IsTrue(String.IsNullOrEmpty(t.Rows[0].Cells[1].Paragraphs[0].Text));
Assert.IsTrue(String.IsNullOrEmpty(t.Rows[2].Cells[1].Paragraphs[0].Text));
}
}
[TestMethod]
public void Test_Document_ApplyTemplate()
{
using (MemoryStream documentStream = new MemoryStream())
[TestMethod]
public void Test_Document_ApplyTemplate()
{
using (DocX document = DocX.Create(documentStream))
{
document.ApplyTemplate(directory_documents + "Template.dotx");
document.Save();
Header firstHeader = document.Headers.first;
Header oddHeader = document.Headers.odd;
Header evenHeader = document.Headers.even;
using (MemoryStream documentStream = new MemoryStream())
{
using (DocX document = DocX.Create(documentStream))
{
document.ApplyTemplate(directory_documents + "Template.dotx");
document.Save();
Header firstHeader = document.Headers.first;
Header oddHeader = document.Headers.odd;
Header evenHeader = document.Headers.even;
Footer firstFooter = document.Footers.first;
Footer oddFooter = document.Footers.odd;
Footer evenFooter = document.Footers.even;
Footer firstFooter = document.Footers.first;
Footer oddFooter = document.Footers.odd;
Footer evenFooter = document.Footers.even;
Assert.IsTrue(firstHeader.Paragraphs.Count==1, "More than one paragraph in header.");
Assert.IsTrue(firstHeader.Paragraphs[0].Text.Equals("First page header"), "Header isn't retrieved from template.");
Assert.IsTrue(firstHeader.Paragraphs.Count == 1, "More than one paragraph in header.");
Assert.IsTrue(firstHeader.Paragraphs[0].Text.Equals("First page header"), "Header isn't retrieved from template.");
Assert.IsTrue(oddHeader.Paragraphs.Count == 1, "More than one paragraph in header.");
Assert.IsTrue(oddHeader.Paragraphs[0].Text.Equals("Odd page header"), "Header isn't retrieved from template.");
Assert.IsTrue(oddHeader.Paragraphs.Count == 1, "More than one paragraph in header.");
Assert.IsTrue(oddHeader.Paragraphs[0].Text.Equals("Odd page header"), "Header isn't retrieved from template.");
Assert.IsTrue(evenHeader.Paragraphs.Count == 1, "More than one paragraph in header.");
Assert.IsTrue(evenHeader.Paragraphs[0].Text.Equals("Even page header"), "Header isn't retrieved from template.");
Assert.IsTrue(evenHeader.Paragraphs.Count == 1, "More than one paragraph in header.");
Assert.IsTrue(evenHeader.Paragraphs[0].Text.Equals("Even page header"), "Header isn't retrieved from template.");
Assert.IsTrue(firstFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
Assert.IsTrue(firstFooter.Paragraphs[0].Text.Equals("First page footer"), "Footer isn't retrieved from template.");
Assert.IsTrue(firstFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
Assert.IsTrue(firstFooter.Paragraphs[0].Text.Equals("First page footer"), "Footer isn't retrieved from template.");
Assert.IsTrue(oddFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
Assert.IsTrue(oddFooter.Paragraphs[0].Text.Equals("Odd page footer"), "Footer isn't retrieved from template.");
Assert.IsTrue(oddFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
Assert.IsTrue(oddFooter.Paragraphs[0].Text.Equals("Odd page footer"), "Footer isn't retrieved from template.");
Assert.IsTrue(evenFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
Assert.IsTrue(evenFooter.Paragraphs[0].Text.Equals("Even page footer"), "Footer isn't retrieved from template.");
Assert.IsTrue(evenFooter.Paragraphs.Count == 1, "More than one paragraph in footer.");
Assert.IsTrue(evenFooter.Paragraphs[0].Text.Equals("Even page footer"), "Footer isn't retrieved from template.");
Paragraph firstParagraph = document.Paragraphs[0];
Assert.IsTrue(firstParagraph.StyleName.Equals("DocXSample"), "First paragraph isn't of style from template.");
}
Paragraph firstParagraph = document.Paragraphs[0];
Assert.IsTrue(firstParagraph.StyleName.Equals("DocXSample"), "First paragraph isn't of style from template.");
}
}
}
}
}
}

読み込み中…
キャンセル
保存