I want to change the styling of each content control in the word document. I was able to set the run within the sdt, but none of the content in the controls were italicized.
SPFieldCollection fldAPItemCol = lstAPItem.Fields;
// string
strColValue = "";
var fldTextCol = lstAPItem.Fields.Cast<SPField>()
// .Where(f =>
f.TypeAsString.EndsWith("Text"))
.Select(f =>
{
return new
{
APItemKey = f.InternalName,
APItemValue = f.GetFieldValueAsText(lstAPItem[f.InternalName])
};
})
.ToDictionary(a => a.APItemKey, a => a.APItemValue);
System.Data.DataTable dtDefaultVal = ConvertListItemsToDataTable("AP_Default_Value");
MemoryStream APDocMem = new MemoryStream();
try
{
#region Openfile to apply styling
SetAsposeWordLicense();
Aspose.Words.Document mainAP = new Aspose.Words.Document(APFile.OpenBinaryStream());
DocumentBuilder builder = new DocumentBuilder(mainAP);
string debugMsg = string.Empty;
foreach (Section sect in mainAP.Sections)
{
foreach (StructuredDocumentTag sdt in sect.Body.GetChildNodes(NodeType.StructuredDocumentTag, true))
{
if (!string.IsNullOrEmpty(sdt.Tag))
{
string APItemValue = fldTextCol[sdt.Tag].ToString().TrimEnd(Æ\rÆ, æ\nÆ).Trim();
DataView dvDefaultValue = dtDefaultVal.DefaultView;
dvDefaultValue.RowFilter = "Title=Æ" + sdt.Tag + "Æ";
string sDefaultValue = string.Empty;
foreach (DataRowView drv in dvDefaultValue)
{
sDefaultValue = drv["DefaultText"].ToString().Replace(" ", "").Replace(" ", "");
sDefaultValue = HttpUtility.HtmlDecode(sDefaultValue);
if
(sDefaultValue.Contains(APItemValue) && APItemValue.Length > 0)
{
debugMsg
+= sdt.Tag + " | ";
foreach (Aspose.Words.Run run in sdt.GetChildNodes(NodeType.Run, true))
{
builder.MoveTo(run);
builder.Font.Italic
= true;
run.Font.Italic = true;
}
}
}
}
}
}
mainAP.Save(APDocMem,
Aspose.Words.SaveFormat.Docx);
#endregion
}
catch (Exception ex)
{
WriteToULSLog(ex.StackTrace, "ApplyDocumentStyle", TraceSeverity.High, EventSeverity.ErrorCritical);
}
AddFile(APFile.ServerRelativeUrl, APDocMem, "Apply styling to default text");
APDocMem.Close();