We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Change the style of content control

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;<?xml:namespace prefix = “o” ns = “urn:schemas-microsoft-com:office:office” /><o:p></o:p>

//string
strColValue = “”;
<o:p></o:p>

var fldTextCol = lstAPItem.Fields.Cast<SPField>()<o:p></o:p>

//.Where(f =>
f.TypeAsString.EndsWith(“Text”))
<o:p></o:p>

.Select(f =><o:p></o:p>

{<o:p></o:p>

return new { APItemKey =
f.InternalName, APItemValue = f.GetFieldValueAsText(lstAPItem[f.InternalName])
};<o:p></o:p>

})<o:p></o:p>

.ToDictionary(a =>
a.APItemKey, a => a.APItemValue);<o:p></o:p>

System.Data.DataTable dtDefaultVal =
ConvertListItemsToDataTable(
“AP_Default_Value”);<o:p></o:p>

MemoryStream APDocMem = new MemoryStream();<o:p></o:p>

try<o:p></o:p>

{<o:p></o:p>

#region Openfile to apply styling<o:p></o:p>

SetAsposeWordLicense();<o:p></o:p>

Aspose.Words.Document mainAP = new Aspose.Words.Document(APFile.OpenBinaryStream());<o:p></o:p>

<o:p> </o:p>

DocumentBuilder builder
=
new DocumentBuilder(mainAP);<o:p></o:p>

string debugMsg = string.Empty;<o:p></o:p>

foreach (Section sect in mainAP.Sections)<o:p></o:p>

{<o:p></o:p>

foreach (StructuredDocumentTag sdt in sect.Body.GetChildNodes(NodeType.StructuredDocumentTag,
true))<o:p></o:p>

{<o:p></o:p>

if (!string.IsNullOrEmpty(sdt.Tag))<o:p></o:p>

{<o:p></o:p>

string APItemValue =
fldTextCol[sdt.Tag].ToString().TrimEnd(
’\r’, ‘\n’).Trim();<o:p></o:p>

DataView dvDefaultValue =
dtDefaultVal.DefaultView;<o:p></o:p>


dvDefaultValue.RowFilter =
“Title=’” + sdt.Tag + “’”;<o:p></o:p>

string sDefaultValue = string.Empty;<o:p></o:p>

foreach (DataRowView drv in dvDefaultValue)<o:p></o:p>

{<o:p></o:p>


sDefaultValue = drv[
“DefaultText”].ToString().Replace("

", “”).Replace("
", “”);<o:p></o:p>

sDefaultValue = HttpUtility.HtmlDecode(sDefaultValue);<o:p></o:p>

<o:p> </o:p>

if
(sDefaultValue.Contains(APItemValue) && APItemValue.Length > 0)<o:p></o:p>

{<o:p></o:p>

debugMsg
+= sdt.Tag +
" | ";<o:p></o:p>

foreach (Aspose.Words.Run run in sdt.GetChildNodes(NodeType.Run, true))<o:p></o:p>

{<o:p></o:p>


builder.MoveTo(run);<o:p></o:p>

builder.Font.Italic
=
true;<o:p></o:p>


run.Font.Italic =
true;<o:p></o:p>

}<o:p></o:p>

}<o:p></o:p>

}<o:p></o:p>

}<o:p></o:p>

}<o:p></o:p>

}<o:p></o:p>

<o:p> </o:p>

mainAP.Save(APDocMem,
Aspose.Words.
SaveFormat.Docx);<o:p></o:p>

#endregion<o:p></o:p>

}<o:p></o:p>

catch (Exception ex)<o:p></o:p>

{<o:p></o:p>

WriteToULSLog(ex.StackTrace, “ApplyDocumentStyle”, TraceSeverity.High, EventSeverity.ErrorCritical);<o:p></o:p>

}<o:p></o:p>

<o:p> </o:p>


AddFile(APFile.ServerRelativeUrl, APDocMem,
“Apply styling to default text”);<o:p></o:p>


APDocMem.Close();

I've modified the codes a bit, but still can't get it to italicize the text within the content control. Here's what was modified.

if (sDefaultValue.Contains(APItemValue) && APItemValue.Length > 0)<?xml:namespace prefix = "o" ns = "urn:schemas-microsoft-com:office:office" />

{

debugMsg += "-x";

builder.MoveTo(sdt.FirstChild);

builder.Italic = true;

Node sdtChildNode = sdt.FirstChild;

foreach (Aspose.Words.Run run in sdt.GetChildNodes(NodeType.Run, true))

{

run.Font.Italic = true;

}

Found the solution. Instead of setting the font properties of the Run within the content control, set the font on the control. Using sdt.ContentFont.italic = true works.

Hi Tan,


Thanks for your inquiry. It is great you were able to find what you were looking for. Please let us know any time you have any further queries.

Best regards,