Thursday, April 8, 2010

Building Document Generation Systems from Templates with Word 2010 and Word 2007


Summary: Some business scenarios require generation of many documents by using a template document and a data source.

Building Document Generation Systems from Templates

Sometimes software systems must automatically generate documents to support various scenarios. The design of the system may include the ability for a subject matter expert to create a template document that drives the generation. The template document must determine specific areas of the document for replacement with automatically-generated content. For example, a placeholder in the document must be replaced with the salutation and name of the addressee of a letter. In addition, the template document may delineate specific sections of the template to remove from the generated document. For example, a template for a legal document may include all possible legal clauses, but based on the specific circumstances, certain legal clauses are omitted in the generated document. The numbering of headings and sections in the resulting document should be automatically adjusted as necessary.

Content controls are ideally suited to this scenario. You can make it easy to insert content controls by including macros to do the insertion. You can automatically set the title and tag of the content control in the macro. Or, you can develop a managed add-in that creates a custom Ribbon with buttons to insert various instances of content controls. You could have buttons that insert content controls with one of a specific set of tags such as Name, Address, Phone, and Legal_Clause.

Sometimes you want to maintain auxiliary information about each content control. You may want to allow the document designer to set a specific set of properties for each one. Content controls do not have a facility such as a special element or attribute in the markup for storing such information. In addition, the Microsoft Word 2010 user interface (UI) and Microsoft Office Word 2007 UI do not allow the document designer to maintain auxiliary information for a content control. However, there is an approach where you can easily allow the document designer to create and configure information associated with each content control. For more information about this process, see Associating Data with Content Controls. This blog entry shows a managed add-in that creates a custom task pane for maintaining this information. This auxiliary information is stored in a custom XML part. The information for each content control is associated by a unique ID that Word automatically generates for each content control. The following figure from this example shows a task pane that allows the content author to set auxiliary information about a content control.

Tip:

The blog entry, Associating Data with Content Controls, shows how to create an application-level task pane. Another approach is to create a task pane associated with each document. This may be more appropriate for some scenarios, such as if you have many document types, and you want to maintain different information for content controls in each document type.

When you create a custom task pane in an application that uses multiple windows to display documents and other items, you must take additional steps to ensure that the task pane is visible when the user expects it to be. The article, Managing Custom Task Panes in Multiple Application Windows, explains what you must know about how to make task panes work with multiple document interfaces (MDI). The article, Managing Task Panes in Multiple Word and InfoPath Documents provides more detail and sample code.

Replacing Content Controls with Content

There are two approaches that you can take when you use content controls to replace content. You can completely replace the content control with the content, or you can bind the content control to XML in a custom XML part. The following sections explore each approach in more detail.

Sometimes you want to completely remove the content controls from the generated document, and replace them with the specific text or content. The following resources provide guidance and examples about how to implement this scenario.

Binding Content Controls to XML in Custom XML Parts

Another approach for using content controls to generate documents is to bind the content controls to custom XML data. This approach has the advantage that when generating the custom document, you do not have to form WordprocessingML to replace the content controls. Instead, if you start with a document that already has content controls that are bound to custom XML data, you only have to replace the custom XML part with XML that contains the data for the new document. The data-bound content controls are automatically updated with the new data.

Handling Events when Manipulating Content Controls

In your solution, you may want to take specific action when a content control is inserted, deleted, or updated. For example, you can write an event handler that is called when the user moves out of a content control. You can then validate the data that is entered by the user.

Content control events are associated with the Document object.

Conclusion

Document generation systems are one of the most compelling uses of Open XML WordprocessingML. Content controls provide a great way to give structure to your content and to make it easier to build document generation systems.

No comments: