0

I am getting some data and from it I am generating a custom XML file. I have a Microsoft Office Word document with Rich Text Content Controls in it. I bind the data to the Content Controls with this method:

private void BindControl(WordprocessingDocument doc, string controlTitle, DocumentFormat.OpenXml.StringValue xPathValue)
{
    DataBinding dataBinding = new DataBinding()
    {
        XPath = xPathValue,
        StoreItemId = "{SomeID}"
    };
    SdtElement sdtElement = doc.MainDocumentPart.Document.Descendants<SdtElement>()
        .Where(
            element =>
            element.SdtProperties.GetFirstChild<SdtAlias>() != null &&
            element.SdtProperties.GetFirstChild<SdtAlias>().Val == controlTitle).FirstOrDefault();
    if (sdtElement != null)
    {
        sdtElement.SdtProperties.Append(dataBinding);
    }
}

However this method only inserts the text with no formating.

I want to make some of the thext in the output document Bold, other with different color and then I must make hyperlinks.

I tried to insert the html formating in the XML file but as result it is not rendered, just displayed.

Gesh
  • 323
  • 3
  • 18

1 Answers1

0

You can't bind a rich text content control.

[MS-DOCX] fixes that, at 2.5.1.6 dataBinding:

If the parent structured document tag is not of type rich text (as specified in [ISO/IEC29500-1:2011] section 17.5.2.26) then this behaves like a dataBinding (as specified in [ISO/IEC29500-1:2011] section 17.5.2.6) element, otherwise the data stored in the XML element will be an escaped string comprised of a flattened WordprocessingML document representing the formatted data in the structured document tag range.

You can however include run properties (w:rPr) in the content control properties of a plain text control.

JasonPlutext
  • 15,352
  • 4
  • 44
  • 84