Search code examples
c#openxmlword-2013

Word Content Controls: how to bind Pictures and tables?


I can bind text to content controls in word2013 using. Works like a charm. I have a CustomXML part in the docx Word file with all the data that is to be bound.

But how do I do this with tables I generate in c# and want to display in Word? And what about pictures? Can I use the same mechanisms? if so: how does that work, as the size of tables f.i. is not previously known...


Solution

  • It can be done with tables and pictures. The approach is basically the same, but different because you use different kinds of Content Controls. For a table (or any formatted text) you need a RichText type of content control and the data you bind must be valid WordOpenXML. For pictures you need an Image content control and the information must be base64 encoded.

    The easiest way for you to see how it works is to create a simple, sample document in the Word UI, save it, close it. The open the document in the Open XML SDK Productivity Tool where you can see the individual XML files, the XML they contain AND the Open XML SDK code required to generate everything.