Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a section optional when mapped to optional data in a Word OpenXml Part?

I'm using OpenXml SDK to generate word 2013 files. I'm running on a server (part of a server solution), so automation is not an option.

Basically I have an xml file that is output from a backend system. Here's a very simplified example:

<my:Data 
    xmlns:my="https://schemas.mycorp.com">
    <my:Customer>
        <my:Details>
            <my:Name>Customer Template</my:Name>
        </my:Details>
        <my:Orders>
            <my:Count>2</my:Count>
            <my:OrderList>
                <my:Order>
                    <my:Id>1</my:Id>
                    <my:Date>19/04/2017 10:16:04</my:Date>
                </my:Order>
                <my:Order>
                    <my:Id>2</my:Id>
                    <my:Date>20/04/2017 10:16:04</my:Date>
                </my:Order>
            </my:OrderList>
        </my:Orders>
    </my:Customer>
</my:Data>

Then I use Word's Xml Mapping pane to map this data to content control:

enter image description here

I simply duplicate the word file, and write new Xml data when generating new files.

This is working as expected. When I update the xml part, it reflects the data from my backend.

Thought, there's a case that does not works. If a customer has no order, the template content is kept in the document. The xml data is :

<my:Data 
    xmlns:my="https://schemas.mycorp.com">
    <my:Customer>
        <my:Details>
            <my:Name>Some customer</my:Name>
        </my:Details>
        <my:Orders>
            <my:Count>0</my:Count>
            <my:OrderList>
            </my:OrderList>
        </my:Orders>
    </my:Customer>
</my:Data>

(see the empty order list).

In Word, the xml pane reflects the correct data (meaning no Order node):

mapping with empty data

But as you can see, the template content is still here.

Basically, I'd like to hide the order list when there's no order (or at least an empty table).

How can I do that?

PS: If it can help, I uploaded the word and xml files, and a small PowerShell script that injects the data : repro.zip

like image 504
Steve B Avatar asked Apr 19 '17 14:04

Steve B


1 Answers

Thanks for sharing your files so we can better help you.

I had a difficult time trying to solve your problem with your existing Word Content Controls, XML files and the PowerShell script that added the XML to the Word document. I found what seemed to be Microsoft's VSTO example solution to your problem, but I couldn't get this to work cleanly.

I was however able to write a simple C# console application that generates a Word file based on your XML data. The OpenXML code to generate the Word file was generated code from the Open XML Productivity Tool. I then added some logic to read your XML file and generate the second table rows dynamically depending on how many orders there are in the data. I have uploaded the code for you to use if you are interested in this solution. Note: The xml data file should be in c:\temp and the generated word files will be in c:\temp also.

Another added bonus to this solution is if you were to add all of the customer data into one XML file, the application will create separate word files in your temp directory like so:

customer_<name1>.docx

customer_<name2>.docx

customer_<name3>.docx

etc.

Here is the document generated from the first xml file enter image description here

Here is the document generated from the second xml file with the empty row enter image description here

Hope this helps.

like image 119
Taterhead Avatar answered Nov 15 '22 11:11

Taterhead