Apply Styles to Content Control

Hi,
I want to modify style of the content of the content control such as changing font color, background color etc.

@mhtsharma9,

Thanks for your inquiry. To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your simplified input Word document
  • Aspose.Words 18.8 generated output DOCX file showing the undesired behavior (if any)
  • Your expected DOCX Word document showing the correct output. You can create expected document by using Microsoft Word.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you code to achieve the same by using Aspose.Words. Thanks for your cooperation.

Not unexpexted behavior.
We are not finding that how we can apply styles to already existing content in content control.

@mhtsharma9,

Please ZIP and attach the resources that I mentioned in my previous post for further proceedings.

Temp.zip (58.8 KB)
Please find attached zip containing input and output expected.

@mhtsharma9,

Please try using the following code:

Document doc = new Document("D:\\temp\\input.docx");

foreach(StructuredDocumentTag sdt in doc.GetChildNodes(NodeType.StructuredDocumentTag, true))
{
    foreach(Run run in sdt.GetChildNodes(NodeType.Run, true))
    {   
        run.Font.Color = Color.Red;
        run.Font.HighlightColor = Color.LightGreen;

        run.Font.Italic = true;
        run.Font.Underline = Underline.Single;
        run.Font.UnderlineColor = Color.Red;
                    
    }
}

doc.Save("D:\\temp\\18.8.docx");

TestDoc.zip (18.4 KB)
Please find the attached document on which the above mentioned code does not show any effect.

@mhtsharma9,

These Content Controls are mapped to custom XML parts; and their formatting are specified inside their custom XML part. To workaround this problem, you can simply remove custom XML Mapping:

Document doc = new Document("D:\\TestDoc\\TestDoc.docx");

foreach (StructuredDocumentTag sdt in doc.GetChildNodes(NodeType.StructuredDocumentTag, true))
{
    //CustomXmlPart customXmlPart = doc.CustomXmlParts.GetById(sdt.XmlMapping.StoreItemId);
    sdt.XmlMapping.Delete();

    foreach (Run run in sdt.GetChildNodes(NodeType.Run, true))
    {
        run.Font.Color = Color.Red;
        run.Font.HighlightColor = Color.LightGreen;

        run.Font.Italic = true;
        run.Font.Underline = Underline.Single;
        run.Font.UnderlineColor = Color.Red;
    }
}

doc.Save("D:\\TestDoc\\18.8.docx");

We can not remove mapping. Please suggest some other alternative.

@mhtsharma9,

We have logged your requirement in our issue tracking system. The ID of this issue is WORDSNET-17376. We will further look into the details of this problem and will keep you updated on the status of this issue. We apologize for any inconvenience.

@mhtsharma9,

These Content Controls are mapped to custom XML parts; and their formatting are specified inside their custom XML part. So, to change their formatting you have to get CustomXmlPart related to the Content Controls and then read their custom XML.

CustomXmlPart customXmlPart = doc.CustomXmlParts.GetById(sdt.XmlMapping.StoreItemId);
AnyXmlReader xmlReader = new AnyXmlReader(new MemoryStream(customXmlPart.Data));

To find out which property you have to change, you can change Content Control’s color in MS Word -> Save the document and then compare \customXml\item1.xml with current item1.xml. Looks like the needed properties for shared document are here
ClinicalSetNew-> Protocol-> TitlePage-> ShortProtocolTitle-> Component->Text

I am afraid, Custom XML will always be different and there is no specification for it. So, there is no way to provide this functionality in Aspose.Words’ API to work with these properties directly.

So regarding WORDSNET-17376, we would not be able to implement the fix of this issue in Aspose.Words API.