In this post, I examine the approaches for building a template document for the document generation process.
This post is the second in a series of blog posts. Here is the complete list: Generating Open XML WordprocessingML Documents Blog Post Series
In my approach to document generation, a template document is a DOCX document that contains content controls that will control the document generation process. The document template designer can format this document as desired, and the document generation process will generate documents that have the format of the template document.
When working with content controls, first of all, remember that you need to turn on the developer tab in the ribbon. Click File => Options => Customize Ribbon, and then turn on the developer tab:
Turning on the Developer Tab
Another point that will make it easier to work with content controls is to turn on design mode. If design mode is turned off (which is the default), content controls have a square boxed appearance with a tab at the top that contains the title of the content control:
Content Control - not in Design Mode
This is not a problem, except that if the focus is not in a content control, there is no visual indication that the content control is there. Instead, turn on design mode:
Turning on design mode
With design mode turned on, content controls have blue tags that indicate the beginning and end of the location of a content control. With design mode turned on, a template document will look something like the following:
Sample template document with content controls
In this document, plain text content controls contain a LINQ query that returns a single value. Formatting is easy – the value returned by the query takes on the formatting of the containing run or paragraph.
In this document, the rich text content control with Table as its title contains a LINQ query that returns a collection of anonymous types. The results of the query will be inserted into the document as a WordprocesssingML table. The inserted table will have the formatting of the empty table that is inserted into the rich text content control.
Other uses of the word ‘Template’ in Microsoft Office
One minor issue around the idea of creating a template WordprocessingML document is that the term ‘template’ is overloaded. Microsoft Word has the notion of ‘Document Templates’, which are saved with the dotx extension. These are WordprocessingML documents with one special characteristic – when the user opens one of these documents, the user cannot directly save back to the dotx file – the user must instead supply a new filename, and Word will append docx as the extension.
In addition, related to dotx document templates are ‘document template projects’ in Visual Studio 2010 (and 2008). These are template-based document-level projects (see Architecture of Document-Level Customizations) that consist of managed code that is attached to a document template instead of a document. The user opens the template, uses the managed customization to do whatever it does, and then saves as a docx document. The docx document can have a managed customization, or it can be stripped of the customization, leaving a plain old docx.
For this document generation project, we don’t need to use either of these facilities. Instead, the template document that the designer creates is, as far as Word is concerned, an ordinary word-processing document.