internal void ApplyTextFormattingProperty(XName textFormatPropName, string value, object content)
{
XElement rPr = null;
if (runs.Count == 0)
{
XElement pPr = Xml.Element(XName.Get("pPr", DocX.w.NamespaceName));
if (pPr == null)
{
Xml.AddFirst(new XElement(XName.Get("pPr", DocX.w.NamespaceName)));
pPr = Xml.Element(XName.Get("pPr", DocX.w.NamespaceName));
}
rPr = pPr.Element(XName.Get("rPr", DocX.w.NamespaceName));
if (rPr == null)
{
pPr.AddFirst(new XElement(XName.Get("rPr", DocX.w.NamespaceName)));
rPr = pPr.Element(XName.Get("rPr", DocX.w.NamespaceName));
}
rPr.SetElementValue(textFormatPropName, value);
var last = rPr.Elements(textFormatPropName).Last();
if (content as XAttribute != null)//If content is an attribute
{
if (last.Attribute(((XAttribute)(content)).Name) == null)
{
last.Add(content); //Add this attribute if element doesn't have it
}
else
{
last.Attribute(((XAttribute)(content)).Name).Value = ((XAttribute)(content)).Value; //Apply value only if element already has it
}
}
return;
}
var contentIsListOfFontProperties = false;
var fontProps = content as IEnumerable;
if (fontProps != null)
{
foreach (object property in fontProps)
{
contentIsListOfFontProperties = (property as XAttribute != null);
}
}
foreach (XElement run in runs)
{
rPr = run.Element(XName.Get("rPr", DocX.w.NamespaceName));
if (rPr == null)
{
run.AddFirst(new XElement(XName.Get("rPr", DocX.w.NamespaceName)));
rPr = run.Element(XName.Get("rPr", DocX.w.NamespaceName));
}
rPr.SetElementValue(textFormatPropName, value);
XElement last = rPr.Elements(textFormatPropName).Last();
if (contentIsListOfFontProperties) //if content is a list of attributes, as in the case when specifying a font family
{
foreach (object property in fontProps)
{
if (last.Attribute(((XAttribute)(property)).Name) == null)
{
last.Add(property); //Add this attribute if element doesn't have it
}
else
{
last.Attribute(((XAttribute)(property)).Name).Value =
((XAttribute)(property)).Value; //Apply value only if element already has it
}
}
}
if (content as XAttribute != null)//If content is an attribute
{
if (last.Attribute(((XAttribute)(content)).Name) == null)
{
last.Add(content); //Add this attribute if element doesn't have it
}
else
{
last.Attribute(((XAttribute)(content)).Name).Value = ((XAttribute)(content)).Value; //Apply value only if element already has it
}
}
else
{
//IMPORTANT
//But what to do if it is not?
}
}
}