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

Free Support Forum - aspose.com

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);
        Run r = new Run(doc);
        r.Font.Bold = true;
        r.Text = "Bold Text";

        Paragraph p = new Paragraph(doc);

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


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:id w:val="2054577459"/>
      <w:docPart w:val="DefaultPlaceholder_22675703"/>
    <w:dataBinding w:xpath="/root[1]/text[1]" w:storeItemID="{1F96A4DD-2ADF-4737-B240-C14F04F89FCA}"/>
    <w:p w14:paraId="625F6D8F" w14:textId="77777777" w:rsidR="002A00DC" w:rsidRPr="00375004" w:rsidRDefault="00375004">
      <w:r w:rsidRPr="00375004">
        <w:t>Hello, World!</w:t>

I believe I need to programmatically set the run properties (<w:rPr>) under sdt properties (<w:sdtPr>) to get the desired behaviour
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:


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.