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;
// 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();

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)
{
    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,