Setting StructuredDocumentTag Font To Bold When Bound To CustomXml

In our project, we are replacing mergefields with content controls. We are binding the content controls to CustomXml to set the values.

We need to preserve the formatting like paragraph styles and font styles (bold, italic, underline).

In the code below, I am creating a new content control, I want the content to be in Bold Font.

      Document doc = new Document();
        CustomXmlPart xmlPart = doc.CustomXmlParts.Add(Guid.NewGuid().ToString("B"), "<root><text>Hello, World!</text></root>");

        StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Block);
        sdt.RemoveAllChildren(); 
        
        Run r = new Run(doc);
        r.Font.Bold = true;
        r.Text = "Bold Text";

        Paragraph p = new Paragraph(doc);
        p.AppendChild(r);
        sdt.AppendChild(p);
        doc.FirstSection.Body.AppendChild(sdt);

        //uncomment to bind to custom xml 
        //sdt.XmlMapping.SetMapping(xmlPart, "/root[1]/text[1]", "");

        doc.Save(@"C:\TestDocs\output.docx");

When the content control is not bound it will show “Bold Text” in bold font, but if I uncomment the line

sdt.XmlMapping.SetMapping(xmlPart, “/root[1]/text[1]”, “”);

the content control is bound to customxml, it shows the text “Hello World!” not in bold.

How can we set the format of bound content control?

If I manualy set ( using Microsoft Word) the content control to bold font , the openxml will be like this

<w:sdt>
  <w:sdtPr>
    <w:rPr>
      <w:b/>
      <w:bCs/>
    </w:rPr>
    <w:id w:val="2054577459"/>
    <w:placeholder>
      <w:docPart w:val="DefaultPlaceholder_22675703"/>
    </w:placeholder>
    <w:dataBinding w:xpath="/root[1]/text[1]" w:storeItemID="{1F96A4DD-2ADF-4737-B240-C14F04F89FCA}"/>
    <w:text/>
  </w:sdtPr>
  <w:sdtEndPr/>
  <w:sdtContent>
    <w:p w14:paraId="625F6D8F" w14:textId="77777777" w:rsidR="002A00DC" w:rsidRPr="00375004" w:rsidRDefault="00375004">
      <w:pPr>
        <w:rPr>
          <w:b/>
          <w:bCs/>
        </w:rPr>
      </w:pPr>
      <w:r w:rsidRPr="00375004">
        <w:rPr>
          <w:b/>
          <w:bCs/>
        </w:rPr>
        <w:t>Hello, World!</w:t>
      </w:r>
    </w:p>
  </w:sdtContent>
</w:sdt>

I believe I need to programmatically set the run properties (<w:rPr>) under sdt properties (<w:sdtPr>) to get the desired behaviour
<w:sdtPr>
<w:rPr>
<w:b/>
<w:bCs/>
</w:rPr>
Can we set run properties (<w:rPr>) under sdt properties (<w:sdtPr>) programmatically using Aspose Words?

I found out the solution to my problem:

I just need to set sdt.ContentsFont :grinning:

@rainierlugue

It is nice to hear from you that you have found the solution of your query. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.