Kaynağa Gözat

Simplifying some expressions

master
Sergii Zhevzhyk 9 yıl önce
ebeveyn
işleme
fb6b2824fe
2 değiştirilmiş dosya ile 110 ekleme ve 263 silme
  1. 109
    257
      DocX/Table.cs
  2. 1
    6
      DocX/_BaseClasses.cs

+ 109
- 257
DocX/Table.cs Dosyayı Görüntüle

* Get the tcPr (table cell properties) element for the first cell in this merge, * Get the tcPr (table cell properties) element for the first cell in this merge,
* null will be returned if no such element exists. * null will be returned if no such element exists.
*/ */
XElement start_tcPr = null;
XElement start_tcPr;
if (columnIndex > Rows[startRow].Cells.Count) if (columnIndex > Rows[startRow].Cells.Count)
start_tcPr = Rows[startRow].Cells[Rows[startRow].Cells.Count - 1].Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); start_tcPr = Rows[startRow].Cells[Rows[startRow].Cells.Count - 1].Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
else else
{ {
get get
{ {
List<Paragraph> paragraphs = new List<Paragraph>();
var paragraphs = new List<Paragraph>();
foreach (Row r in Rows) foreach (Row r in Rows)
paragraphs.AddRange(r.Paragraphs); paragraphs.AddRange(r.Paragraphs);
{ {
get get
{ {
List<Picture> pictures = new List<Picture>();
var pictures = new List<Picture>();
foreach (Row r in Rows) foreach (Row r in Rows)
pictures.AddRange(r.Pictures); pictures.AddRange(r.Pictures);
{ {
get get
{ {
List<Hyperlink> hyperlinks = new List<Hyperlink>();
var hyperlinks = new List<Hyperlink>();
foreach (Row r in Rows) foreach (Row r in Rows)
hyperlinks.AddRange(r.Hyperlinks); hyperlinks.AddRange(r.Hyperlinks);
} }
// remove all existing values // remove all existing values
grid.RemoveAll();
grid?.RemoveAll();
// append new column widths // append new column widths
XElement gridCol = null;
Int32 i = 0; Int32 i = 0;
Double value = width;
Double total = 0;
foreach (var w in widths) foreach (var w in widths)
{ {
value = w;
double value = w;
if (i == index) value = width; if (i == index) value = width;
gridCol = new XElement(XName.Get("gridCol", DocX.w.NamespaceName),
new XAttribute(XName.Get("w", DocX.w.NamespaceName), value));
grid.Add(gridCol);
var gridCol = new XElement(XName.Get("gridCol", DocX.w.NamespaceName),
new XAttribute(XName.Get("w", DocX.w.NamespaceName), value));
grid?.Add(gridCol);
i += 1; i += 1;
total += value;
} }
// remove cell widths // remove cell widths
{ {
get get
{ {
List<Double> widths = new List<Double>();
var widths = new List<Double>();
// get the table grid props // get the table grid props
XElement grid = Xml.Element(XName.Get("tblGrid", DocX.w.NamespaceName)); XElement grid = Xml.Element(XName.Get("tblGrid", DocX.w.NamespaceName));
if (grid == null) return null;
// get col properties // get col properties
var cols = grid.Elements(XName.Get("gridCol", DocX.w.NamespaceName));
var cols = grid?.Elements(XName.Get("gridCol", DocX.w.NamespaceName));
if (cols == null) return null; if (cols == null) return null;
String value = String.Empty;
foreach (var col in cols) foreach (var col in cols)
{ {
value = col.GetAttribute(XName.Get("w", DocX.w.NamespaceName));
string value = col.GetAttribute(XName.Get("w", DocX.w.NamespaceName));
widths.Add(Convert.ToDouble(value)); widths.Add(Convert.ToDouble(value));
} }
return widths; return widths;
XElement properties = xml.Element(XName.Get("tblPr", DocX.w.NamespaceName)); XElement properties = xml.Element(XName.Get("tblPr", DocX.w.NamespaceName));
XElement style = properties.Element(XName.Get("tblStyle", DocX.w.NamespaceName));
XElement style = properties?.Element(XName.Get("tblStyle", DocX.w.NamespaceName));
if (style != null) if (style != null)
{ {
XAttribute val = style.Attribute(XName.Get("val", DocX.w.NamespaceName)); XAttribute val = style.Attribute(XName.Get("val", DocX.w.NamespaceName));
else else
design = TableDesign.None; design = TableDesign.None;
XElement tableLook = properties.Element(XName.Get("tblLook", DocX.w.NamespaceName));
XElement tableLook = properties?.Element(XName.Get("tblLook", DocX.w.NamespaceName));
if (tableLook != null) if (tableLook != null)
{ {
TableLook = new TableLook();
TableLook.FirstRow = tableLook.GetAttribute(XName.Get("firstRow", DocX.w.NamespaceName)) == "1";
TableLook.LastRow = tableLook.GetAttribute(XName.Get("lastRow", DocX.w.NamespaceName)) == "1";
TableLook.FirstColumn = tableLook.GetAttribute(XName.Get("firstColumn", DocX.w.NamespaceName)) == "1";
TableLook.LastColumn = tableLook.GetAttribute(XName.Get("lastColumn", DocX.w.NamespaceName)) == "1";
TableLook.NoHorizontalBanding = tableLook.GetAttribute(XName.Get("noHBand", DocX.w.NamespaceName)) == "1";
TableLook.NoVerticalBanding = tableLook.GetAttribute(XName.Get("noVBand", DocX.w.NamespaceName)) == "1";
TableLook = new TableLook
{
FirstRow = tableLook.GetAttribute(XName.Get("firstRow", DocX.w.NamespaceName)) == "1",
LastRow = tableLook.GetAttribute(XName.Get("lastRow", DocX.w.NamespaceName)) == "1",
FirstColumn = tableLook.GetAttribute(XName.Get("firstColumn", DocX.w.NamespaceName)) == "1",
LastColumn = tableLook.GetAttribute(XName.Get("lastColumn", DocX.w.NamespaceName)) == "1",
NoHorizontalBanding = tableLook.GetAttribute(XName.Get("noHBand", DocX.w.NamespaceName)) == "1",
NoVerticalBanding = tableLook.GetAttribute(XName.Get("noVBand", DocX.w.NamespaceName)) == "1"
};
} }
} }
get get
{ {
XElement tblPr = Xml.Element(XName.Get("tblPr", DocX.w.NamespaceName)); XElement tblPr = Xml.Element(XName.Get("tblPr", DocX.w.NamespaceName));
if (tblPr != null)
XElement caption = tblPr?.Element(XName.Get("tblCaption", DocX.w.NamespaceName));
if (caption != null)
{ {
XElement caption = tblPr.Element(XName.Get("tblCaption", DocX.w.NamespaceName));
if (caption != null)
{
_tableCaption = caption.GetAttribute(XName.Get("val", DocX.w.NamespaceName));
}
_tableCaption = caption.GetAttribute(XName.Get("val", DocX.w.NamespaceName));
} }
return _tableCaption; return _tableCaption;
} }
XElement tblDescription = XElement tblDescription =
tblPr.Descendants(XName.Get("tblDescription", DocX.w.NamespaceName)).FirstOrDefault(); tblPr.Descendants(XName.Get("tblDescription", DocX.w.NamespaceName)).FirstOrDefault();
if (tblDescription != null)
tblDescription.Remove();
tblDescription?.Remove();
tblDescription = new XElement(XName.Get("tblDescription", DocX.w.NamespaceName), tblDescription = new XElement(XName.Get("tblDescription", DocX.w.NamespaceName),
new XAttribute(XName.Get("val", DocX.w.NamespaceName), value)); new XAttribute(XName.Get("val", DocX.w.NamespaceName), value));
get get
{ {
XElement tblPr = Xml.Element(XName.Get("tblPr", DocX.w.NamespaceName)); XElement tblPr = Xml.Element(XName.Get("tblPr", DocX.w.NamespaceName));
if (tblPr != null)
XElement caption = tblPr?.Element(XName.Get("tblDescription", DocX.w.NamespaceName));
if (caption != null)
{ {
XElement caption = tblPr.Element(XName.Get("tblDescription", DocX.w.NamespaceName));
if (caption != null)
{
_tableDescription = caption.GetAttribute(XName.Get("val", DocX.w.NamespaceName));
}
_tableDescription = caption.GetAttribute(XName.Get("val", DocX.w.NamespaceName));
} }
return _tableDescription; return _tableDescription;
} }
XElement tblPr = Xml.Descendants(XName.Get("tblPr", DocX.w.NamespaceName)).First(); XElement tblPr = Xml.Descendants(XName.Get("tblPr", DocX.w.NamespaceName)).First();
XElement jc = tblPr.Descendants(XName.Get("jc", DocX.w.NamespaceName)).FirstOrDefault(); XElement jc = tblPr.Descendants(XName.Get("jc", DocX.w.NamespaceName)).FirstOrDefault();
if (jc != null)
jc.Remove();
jc?.Remove();
jc = new XElement(XName.Get("jc", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), alignmentString)); jc = new XElement(XName.Get("jc", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), alignmentString));
tblPr.Add(jc); tblPr.Add(jc);
if (tblLayout == null) if (tblLayout == null)
{ {
XElement tmp = tblPr.Element(XName.Get("tblInd", DocX.w.NamespaceName));
if (tmp == null)
{
tmp = tblPr.Element(XName.Get("tblW", DocX.w.NamespaceName));
}
XElement tmp = tblPr.Element(XName.Get("tblInd", DocX.w.NamespaceName)) ??
tblPr.Element(XName.Get("tblW", DocX.w.NamespaceName));
tmp.AddAfterSelf(new XElement(XName.Get("tblLayout", DocX.w.NamespaceName))); tmp.AddAfterSelf(new XElement(XName.Get("tblLayout", DocX.w.NamespaceName)));
tmp = tblPr.Element(XName.Get("tblLayout", DocX.w.NamespaceName)); tmp = tblPr.Element(XName.Get("tblLayout", DocX.w.NamespaceName));
case TableDesign.ColorfulGridAccent6: case TableDesign.ColorfulGridAccent6:
val.Value = "ColorfulGrid-Accent6"; val.Value = "ColorfulGrid-Accent6";
break; break;
default:
break;
} }
} }
if (Document.styles == null) if (Document.styles == null)
} }
else else
{ {
bool directionTest = true;
var positionIndex = 1; var positionIndex = 1;
var actualPosition = 1; var actualPosition = 1;
var gridAfterVal = 0; var gridAfterVal = 0;
if ((index - gridAfterVal) >= actualPosition if ((index - gridAfterVal) >= actualPosition
&& (index - gridAfterVal) <= (actualPosition + gridSpanVal)) && (index - gridAfterVal) <= (actualPosition + gridSpanVal))
{ {
if (index == (actualPosition + gridSpanVal) && direction == true)
bool directionTest;
if (index == (actualPosition + gridSpanVal) && direction)
{ {
directionTest = true; directionTest = true;
} }
/// <param name="celIndex">index of the cell you want to remove</param> /// <param name="celIndex">index of the cell you want to remove</param>
public void DeleteAndShiftCellsLeft(int rowIndex, int celIndex) public void DeleteAndShiftCellsLeft(int rowIndex, int celIndex)
{ {
XAttribute gridAfterVal = new XAttribute(XName.Get("val", DocX.w.NamespaceName), 0);
var trPr = Rows[rowIndex].Xml.Element(XName.Get("trPr", DocX.w.NamespaceName)); var trPr = Rows[rowIndex].Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
if (trPr != null) if (trPr != null)
{ {
if (gridAfter != null) if (gridAfter != null)
{ {
var val = gridAfter.Attribute(XName.Get("val", DocX.w.NamespaceName)); var val = gridAfter.Attribute(XName.Get("val", DocX.w.NamespaceName));
if (val != null)
{
val.Value = (int.Parse(val.Value) + 1).ToString();
}
else
{
val.Value = "1";
}
val.Value = (int.Parse(val.Value) + 1).ToString();
} }
else else
{ {
var gridAfterElement = new XElement("gridAfter");
var gridAfterValAttribute = new XAttribute("val", 1);
gridAfter.SetAttributeValue("val", 1); gridAfter.SetAttributeValue("val", 1);
} }
} }
* Get the 'borderType' (table border) element for this Table, * Get the 'borderType' (table border) element for this Table,
* null will be return if no such element exists. * null will be return if no such element exists.
*/ */
string tbordertype;
tbordertype = borderType.ToString();
var tbordertype = borderType.ToString();
// only lower the first char of string (because of insideH and insideV) // only lower the first char of string (because of insideH and insideV)
tbordertype = tbordertype.Substring(0, 1).ToLower() + tbordertype.Substring(1); tbordertype = tbordertype.Substring(0, 1).ToLower() + tbordertype.Substring(1);
// instance with default border values // instance with default border values
Border b = new Border(); Border b = new Border();
/*
* Get the tblPr (table properties) element for this Table,
* null will be return if no such element exists.
*/
// 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)); XElement tblPr = Xml.Element(XName.Get("tblPr", DocX.w.NamespaceName));
if (tblPr == null) if (tblPr == null)
{ {
* Get the 'borderType' (table border) element for this Table, * Get the 'borderType' (table border) element for this Table,
* null will be return if no such element exists. * null will be return if no such element exists.
*/ */
string tbordertype;
tbordertype = borderType.ToString();
var tbordertype = borderType.ToString();
// only lower the first char of string (because of insideH and insideV) // only lower the first char of string (because of insideH and insideV)
tbordertype = tbordertype.Substring(0, 1).ToLower() + tbordertype.Substring(1); tbordertype = tbordertype.Substring(0, 1).ToLower() + tbordertype.Substring(1);
if (trPr != null) if (trPr != null)
{ {
var gridAfter = trPr.Element(XName.Get("gridAfter", DocX.w.NamespaceName)); var gridAfter = trPr.Element(XName.Get("gridAfter", DocX.w.NamespaceName));
if (gridAfter != null)
var val = gridAfter?.Attribute(XName.Get("val", DocX.w.NamespaceName));
if (val != null)
{ {
var val = gridAfter.Attribute(XName.Get("val", DocX.w.NamespaceName));
if (val != null)
{
gridAfterValue += int.Parse(val.Value);
}
gridAfterValue += int.Parse(val.Value);
} }
} }
return gridAfterValue; return gridAfterValue;
XElement table = Xml.Parent; XElement table = Xml.Parent;
Xml.Remove(); Xml.Remove();
if (table.Elements(XName.Get("tr", DocX.w.NamespaceName)).Count() == 0)
if (!table.Elements(XName.Get("tr", DocX.w.NamespaceName)).Any())
table.Remove(); table.Remove();
} }
{ {
get get
{ {
/*
* Get the trPr (table row properties) element for this Row,
* null will be return if no such element exists.
*/
// Get the trPr (table row properties) element for this Row,
// null will be return if no such element exists.
XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName)); XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
// If trPr is null, this row contains no height information. // If trPr is null, this row contains no height information.
if (trPr == null)
return double.NaN;
/*
* Get the trHeight element for this Row,
* null will be return if no such element exists.
*/
XElement trHeight = trPr.Element(XName.Get("trHeight", DocX.w.NamespaceName));
// Get the trHeight element for this Row,
// 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 is null, this row contains no height information.
if (trHeight == null)
return double.NaN;
// Get the val attribute for this trHeight element. // Get the val attribute for this trHeight element.
XAttribute val = trHeight.Attribute(XName.Get("val", DocX.w.NamespaceName));
XAttribute val = trHeight?.Attribute(XName.Get("val", DocX.w.NamespaceName));
// If w is null, this cell contains no width information. // If w is null, this cell contains no width information.
if (val == null) if (val == null)
{ {
XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName)); XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
XElement tblHeader = trPr.Element(XName.Get("tblHeader", DocX.w.NamespaceName)); XElement tblHeader = trPr.Element(XName.Get("tblHeader", DocX.w.NamespaceName));
if (tblHeader == null)
{
return false;
}
else
{
return true;
}
return tblHeader != null;
} }
set set
{ {
{ {
XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName)); XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
if (trPr == null)
return true;
XElement trCantSplit = trPr.Element(XName.Get("cantSplit", DocX.w.NamespaceName));
XElement trCantSplit = trPr?.Element(XName.Get("cantSplit", DocX.w.NamespaceName));
if (trCantSplit == null)
return true;
return false;
return trCantSplit == null;
} }
set set
if (trCantSplit == null) if (trCantSplit == null)
trPr.SetElementValue(XName.Get("cantSplit", DocX.w.NamespaceName), string.Empty); trPr.SetElementValue(XName.Get("cantSplit", DocX.w.NamespaceName), string.Empty);
} }
if (value == true)
else
{ {
XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName)); XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName));
if (trPr != null)
{
XElement trCantSplit = trPr.Element(XName.Get("cantSplit", DocX.w.NamespaceName));
if (trCantSplit != null)
trCantSplit.Remove();
}
XElement trCantSplit = trPr?.Element(XName.Get("cantSplit", DocX.w.NamespaceName));
trCantSplit?.Remove();
} }
} }
} }
foreach (Cell c in Cells.Where((z, i) => i > startIndex && i <= endIndex)) foreach (Cell c in Cells.Where((z, i) => i > startIndex && i <= endIndex))
{ {
XElement tcPr = c.Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); XElement tcPr = c.Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
if (tcPr != null)
XElement gridSpan = tcPr?.Element(XName.Get("gridSpan", DocX.w.NamespaceName));
if (gridSpan != null)
{ {
XElement gridSpan = tcPr.Element(XName.Get("gridSpan", DocX.w.NamespaceName));
if (gridSpan != null)
{
XAttribute val = gridSpan.Attribute(XName.Get("val", DocX.w.NamespaceName));
XAttribute val = gridSpan.Attribute(XName.Get("val", DocX.w.NamespaceName));
int value = 0;
if (val != null)
if (int.TryParse(val.Value, out value))
gridSpanSum += value - 1;
}
int value;
if (val != null && int.TryParse(val.Value, out value))
gridSpanSum += value - 1;
} }
// Add this cells Pragraph to the merge start Cell. // Add this cells Pragraph to the merge start Cell.
{ {
var gridSpanVal = 0; var gridSpanVal = 0;
XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
if (tcPr != null)
XElement gridSpan = tcPr?.Element(XName.Get("gridSpan", DocX.w.NamespaceName));
if (gridSpan != null)
{ {
XElement gridSpan = tcPr.Element(XName.Get("gridSpan", DocX.w.NamespaceName));
if (gridSpan != null)
{
XAttribute val = gridSpan.Attribute(XName.Get("val", DocX.w.NamespaceName));
XAttribute val = gridSpan.Attribute(XName.Get("val", DocX.w.NamespaceName));
int value = 0;
if (val != null)
if (int.TryParse(val.Value, out value))
gridSpanVal = value;
}
int value;
if (val != null && int.TryParse(val.Value, out value))
gridSpanVal = value;
} }
return gridSpanVal; return gridSpanVal;
} }
XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
// If tcPr is null, this cell contains no width information. // If tcPr is null, this cell contains no width information.
if (tcPr == null)
return VerticalAlignment.Center;
/*
* Get the vAlign (table cell vertical alignment) element for this Cell,
* null will be return if no such element exists.
*/
XElement vAlign = tcPr.Element(XName.Get("vAlign", DocX.w.NamespaceName));
// Get the vAlign (table cell vertical alignment) element for this Cell,
// null will be return if no such element exists.
XElement vAlign = tcPr?.Element(XName.Get("vAlign", DocX.w.NamespaceName));
// If vAlign is null, this cell contains no vertical alignment information. // If vAlign is null, this cell contains no vertical alignment information.
if (vAlign == null)
return VerticalAlignment.Center;
// Get the val attribute of the vAlign element. // Get the val attribute of the vAlign element.
XAttribute val = vAlign.Attribute(XName.Get("val", DocX.w.NamespaceName));
XAttribute val = vAlign?.Attribute(XName.Get("val", DocX.w.NamespaceName));
// If val is null, this cell contains no vAlign information. // If val is null, this cell contains no vAlign information.
if (val == null) if (val == null)
set set
{ {
/*
* Get the tcPr (table cell properties) element for this Cell,
* null will be return if no such element exists.
*/
// Get the tcPr (table cell properties) element for this Cell,
// null will be return if no such element exists.
XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
if (tcPr == null) if (tcPr == null)
{ {
tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
} }
/*
* Get the vAlign (table cell vertical alignment) element for this Cell,
* null will be return if no such element exists.
*/
// Get the vAlign (table cell vertical alignment) element for this Cell,
// null will be return if no such element exists.
XElement vAlign = tcPr.Element(XName.Get("vAlign", DocX.w.NamespaceName)); XElement vAlign = tcPr.Element(XName.Get("vAlign", DocX.w.NamespaceName));
if (vAlign == null) if (vAlign == null)
{ {
{ {
get get
{ {
/*
* Get the tcPr (table cell properties) element for this Cell,
* null will be return if no such element exists.
*/
// Get the tcPr (table cell properties) element for this Cell,
// null will be return if no such element exists.
XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
// If tcPr is null, this cell contains no Color information. // If tcPr is null, this cell contains no Color information.
if (tcPr == null)
return Color.White;
/*
* Get the shd (table shade) element for this Cell,
* null will be return if no such element exists.
*/
XElement shd = tcPr.Element(XName.Get("shd", DocX.w.NamespaceName));
// Get the shd (table shade) element for this Cell,
// null will be return if no such element exists.
XElement shd = tcPr?.Element(XName.Get("shd", DocX.w.NamespaceName));
// If shd is null, this cell contains no Color information. // If shd is null, this cell contains no Color information.
if (shd == null)
return Color.White;
// Get the w attribute of the tcW element. // Get the w attribute of the tcW element.
XAttribute fill = shd.Attribute(XName.Get("fill", DocX.w.NamespaceName));
XAttribute fill = shd?.Attribute(XName.Get("fill", DocX.w.NamespaceName));
// If fill is null, this cell contains no Color information. // If fill is null, this cell contains no Color information.
if (fill == null) if (fill == null)
set set
{ {
/*
* Get the tcPr (table cell properties) element for this Cell,
* null will be return if no such element exists.
*/
// Get the tcPr (table cell properties) element for this Cell,
// null will be return if no such element exists.
XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
if (tcPr == null) if (tcPr == null)
{ {
{ {
get get
{ {
/*
* Get the tcPr (table cell properties) element for this Cell,
* null will be return if no such element exists.
*/
// Get the tcPr (table cell properties) element for this Cell,
// null will be return if no such element exists.
XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
// If tcPr is null, this cell contains no width information. // If tcPr is null, this cell contains no width information.
if (tcPr == null)
return double.NaN;
/*
* Get the tcW (table cell width) element for this Cell,
* null will be return if no such element exists.
*/
XElement tcW = tcPr.Element(XName.Get("tcW", DocX.w.NamespaceName));
// Get the tcW (table cell width) element for this Cell,
// null will be return if no such element exists.
XElement tcW = tcPr?.Element(XName.Get("tcW", DocX.w.NamespaceName));
// If tcW is null, this cell contains no width information. // If tcW is null, this cell contains no width information.
if (tcW == null)
return double.NaN;
// Get the w attribute of the tcW element. // Get the w attribute of the tcW element.
XAttribute w = tcW.Attribute(XName.Get("w", DocX.w.NamespaceName));
XAttribute w = tcW?.Attribute(XName.Get("w", DocX.w.NamespaceName));
// If w is null, this cell contains no width information. // If w is null, this cell contains no width information.
if (w == null) if (w == null)
// remove cell width; due to set on table prop. // remove cell width; due to set on table prop.
tcW.Remove(); tcW.Remove();
return; return;
//tcW.SetAttributeValue(XName.Get("type", DocX.w.NamespaceName), "auto");
//return;
} }
// 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. // 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.
XElement tcMar = tcPr.Element(XName.Get("tcMar", DocX.w.NamespaceName)); XElement tcMar = tcPr.Element(XName.Get("tcMar", DocX.w.NamespaceName));
// If tcMar is null, this cell contains no margin information. // If tcMar is null, this cell contains no margin information.
if (tcMar == null)
return double.NaN;
// Get the left (LeftMargin) element // Get the left (LeftMargin) element
XElement tcMarLeft = tcMar.Element(XName.Get("left", DocX.w.NamespaceName));
XElement tcMarLeft = tcMar?.Element(XName.Get("left", DocX.w.NamespaceName));
// If tcMarLeft is null, this cell contains no left margin information. // If tcMarLeft is null, this cell contains no left margin information.
if (tcMarLeft == null)
return double.NaN;
// Get the w attribute of the tcMarLeft element. // Get the w attribute of the tcMarLeft element.
XAttribute w = tcMarLeft.Attribute(XName.Get("w", DocX.w.NamespaceName));
XAttribute w = tcMarLeft?.Attribute(XName.Get("w", DocX.w.NamespaceName));
// If w is null, this cell contains no width information. // If w is null, this cell contains no width information.
if (w == null) if (w == null)
XElement tcMar = tcPr.Element(XName.Get("tcMar", DocX.w.NamespaceName)); XElement tcMar = tcPr.Element(XName.Get("tcMar", DocX.w.NamespaceName));
// If tcMar is null, this cell contains no margin information. // If tcMar is null, this cell contains no margin information.
if (tcMar == null)
return double.NaN;
// Get the right (RightMargin) element // Get the right (RightMargin) element
XElement tcMarRight = tcMar.Element(XName.Get("right", DocX.w.NamespaceName));
XElement tcMarRight = tcMar?.Element(XName.Get("right", DocX.w.NamespaceName));
// If tcMarRight is null, this cell contains no right margin information. // If tcMarRight is null, this cell contains no right margin information.
if (tcMarRight == null)
return double.NaN;
// Get the w attribute of the tcMarRight element. // Get the w attribute of the tcMarRight element.
XAttribute w = tcMarRight.Attribute(XName.Get("w", DocX.w.NamespaceName));
XAttribute w = tcMarRight?.Attribute(XName.Get("w", DocX.w.NamespaceName));
// If w is null, this cell contains no width information. // If w is null, this cell contains no width information.
if (w == null) if (w == null)
XElement tcMar = tcPr.Element(XName.Get("tcMar", DocX.w.NamespaceName)); XElement tcMar = tcPr.Element(XName.Get("tcMar", DocX.w.NamespaceName));
// If tcMar is null, this cell contains no margin information. // If tcMar is null, this cell contains no margin information.
if (tcMar == null)
return double.NaN;
// Get the top (TopMargin) element // Get the top (TopMargin) element
XElement tcMarTop = tcMar.Element(XName.Get("top", DocX.w.NamespaceName));
XElement tcMarTop = tcMar?.Element(XName.Get("top", DocX.w.NamespaceName));
// If tcMarTop is null, this cell contains no top margin information. // If tcMarTop is null, this cell contains no top margin information.
if (tcMarTop == null)
return double.NaN;
// Get the w attribute of the tcMarTop element. // Get the w attribute of the tcMarTop element.
XAttribute w = tcMarTop.Attribute(XName.Get("w", DocX.w.NamespaceName));
XAttribute w = tcMarTop?.Attribute(XName.Get("w", DocX.w.NamespaceName));
// If w is null, this cell contains no width information. // If w is null, this cell contains no width information.
if (w == null) if (w == null)
XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
// If tcPr is null, this cell contains no width information. // If tcPr is null, this cell contains no width information.
if (tcPr == null)
return double.NaN;
/* /*
* Get the tcMar * Get the tcMar
* *
*/ */
XElement tcMar = tcPr.Element(XName.Get("tcMar", DocX.w.NamespaceName));
XElement tcMar = tcPr?.Element(XName.Get("tcMar", DocX.w.NamespaceName));
// If tcMar is null, this cell contains no margin information. // If tcMar is null, this cell contains no margin information.
if (tcMar == null) if (tcMar == null)
XElement tcMarBottom = tcMar.Element(XName.Get("bottom", DocX.w.NamespaceName)); XElement tcMarBottom = tcMar.Element(XName.Get("bottom", DocX.w.NamespaceName));
// If tcMarBottom is null, this cell contains no bottom margin information. // If tcMarBottom is null, this cell contains no bottom margin information.
if (tcMarBottom == null)
return double.NaN;
// Get the w attribute of the tcMarBottom element. // Get the w attribute of the tcMarBottom element.
XAttribute w = tcMarBottom.Attribute(XName.Get("w", DocX.w.NamespaceName));
XAttribute w = tcMarBottom?.Attribute(XName.Get("w", DocX.w.NamespaceName));
// If w is null, this cell contains no width information. // If w is null, this cell contains no width information.
if (w == null) if (w == null)
public Border GetBorder(TableCellBorderType borderType) public Border GetBorder(TableCellBorderType borderType)
{ {
// instance with default border values // instance with default border values
Border b = new Border();
var b = new Border();
/* /*
* Get the tcPr (table cell properties) element for this Cell, * Get the tcPr (table cell properties) element for this Cell,
* Get the 'borderType' (cell border) element for this Cell, * Get the 'borderType' (cell border) element for this Cell,
* null will be return if no such element exists. * null will be return if no such element exists.
*/ */
string tcbordertype;
tcbordertype = borderType.ToString();
var tcbordertype = borderType.ToString();
switch (tcbordertype) switch (tcbordertype)
{ {
* null will be return if no such element exists. * null will be return if no such element exists.
*/ */
XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
if (tcPr == null)
XElement shd = tcPr?.Element(XName.Get("shd", DocX.w.NamespaceName));
XAttribute fill = shd?.Attribute(XName.Get("fill", DocX.w.NamespaceName));
if (fill == null)
return Color.Empty; return Color.Empty;
else
{
XElement shd = tcPr.Element(XName.Get("shd", DocX.w.NamespaceName));
if (shd == null)
return Color.Empty;
else
{
XAttribute fill = shd.Attribute(XName.Get("fill", DocX.w.NamespaceName));
if (fill == null)
return Color.Empty;
else
{
int argb = Int32.Parse(fill.Value.Replace("#", ""), NumberStyles.HexNumber);
return Color.FromArgb(argb);
}
}
}
int argb = Int32.Parse(fill.Value.Replace("#", ""), NumberStyles.HexNumber);
return Color.FromArgb(argb);
} }
set set
XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
// If tcPr is null, this cell contains no width information. // If tcPr is null, this cell contains no width information.
if (tcPr == null)
return TextDirection.right;
XElement textDirection = tcPr.Element(XName.Get("textDirection", DocX.w.NamespaceName));
if (textDirection == null)
return TextDirection.right;
XAttribute val = textDirection.Attribute(XName.Get("val", DocX.w.NamespaceName));
XElement textDirection = tcPr?.Element(XName.Get("textDirection", DocX.w.NamespaceName));
XAttribute val = textDirection?.Attribute(XName.Get("val", DocX.w.NamespaceName));
if (val == null) if (val == null)
return TextDirection.right; return TextDirection.right;
{ {
return (TextDirection)Enum.Parse(typeof(TextDirection), val.Value, true); return (TextDirection)Enum.Parse(typeof(TextDirection), val.Value, true);
} }
catch catch
{ {
val.Remove(); val.Remove();

+ 1
- 6
DocX/_BaseClasses.cs Dosyayı Görüntüle

Xml.AddBeforeSelf(newParagraph); Xml.AddBeforeSelf(newParagraph);
XElement newlyInserted = Xml.ElementsBeforeSelf().Last(); XElement newlyInserted = Xml.ElementsBeforeSelf().Last();
Paragraph p = new Paragraph(Document, newlyInserted, -1);
return p;
return new Paragraph(Document, newlyInserted, -1);
} }
public virtual Paragraph InsertParagraphAfterSelf(string text, bool trackChanges, Formatting formatting) public virtual Paragraph InsertParagraphAfterSelf(string text, bool trackChanges, Formatting formatting)
//Dmitchern //Dmitchern
return new Table(Document, newlyInserted) { mainPart=mainPart}; //return new table, dont affect parameter table return new Table(Document, newlyInserted) { mainPart=mainPart}; //return new table, dont affect parameter table
//t.Xml = newlyInserted;
//return t;
} }
} }

Loading…
İptal
Kaydet