Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordML templating with XML Schema and OpenXML SDK

I'm trying to use a docx read in via the OpenXML SDK as template in a .Net web app for form letters (essentially). I have a docx with the formatting I want, marked up with XML Schema tags defined from a xsd (whatever they're called, the help is exceedingly useless on this).

The only thing similar I found on Google was what seemed to be replacing the CustomXmlBlock elements with text elements with the desired data. This seems... messy and fragile.

I also have the added complication that some of the data is hierarchical, with elements for sections, and then items in that section. This would seem to completely break the above method.

So, can someone point me in the right direction for doing this, or suggest a better way to handle templating form letters?

like image 506
Matt Sieker Avatar asked Sep 21 '10 21:09

Matt Sieker


1 Answers

There are a number of choices:

  1. What you're doing, XSD Schema mapping. This was introduced in Word 2003. It was really only truly active in that version of Word as when Word 2007 came out, the preferred new method of forms became #2.
  2. Content Controls. More on this below.
  3. Tried and true are Bookmarks. If you're interested, there is an article here.

For Content Controls 1, the concept is simple: wrap your content controls in the hierarchy needed. That is to say, for example, have a Date Picker Content Control and a Rich Text Content Control and then group them. Then inside the Rich Text Content Control, place a Combo Box Content Control. Eric White has written a great starter article on this at retrieving content from inside of Content Controls at Processing all Content Parts in an Open XML WordprocessingML Document. This article is a much more comprehensive introduction to Content Controls in it's links Using Open XML WordprocessingML Documents as Data Sources.

More links on Content Controls:

  1. Brian Jones' site has a lot of great articles. I believe these were his first: The Easy Way to Assemble Multiple Word Documents and Create a rich Word document based on your own custom XML (without the need for XSLT).
  2. The Word Content Control Toolkit. Probably the best tool around for working with Content Controls.
  3. Visual How To Content. Many articles here deal with Content Controls.

1To work with Content Controls manually in the Word client, enable the "Developer" Ribbon from Options and then insert Content Controls and set their properties from the left-hand section of the Controls group in the Developer Ribbon.

like image 150
Todd Main Avatar answered Sep 27 '22 16:09

Todd Main