This utility generates Word documents from templates using Content controls. It has been created in Visual Studio 2010 and uses Open Xml 2.0 SDK which can be downloaded from http://www.microsoft.com/download/en/details.aspx?id=5124. As Open Xml 2.0 SDK is used hence there is no dependency that Word should be installed.

The purpose of creating this utility was to use the Open Xml 2.0 SDK to generate Word documents based on predefined templates using minimum code changes. These documents can either be refreshable or non- refreshable. I’ll explain this difference later. Also there is no dependency that Word should be installed. More samples can be added later as per feedback.

Questions about this project - FAQ

Template Design:

The sample templates are inside “WordDocumentGenerator.Client\Sample Templates” folder. Content control has Title and Tag properties.

The logic is to have a tag property of a content control and then populate data using that placeholder. This means every content control inside a Word template will have a different Tag.

Lets assume that the tag of the content control is “PlaceholderNonRecursiveB”. During document generation we can assign (not mandatory) a unique Id e.g. Guid of a record to make the tag unique e.g. “PlaceholderNonRecursiveB:Guid”. Let’s say that we have an Id and Name field. Thus the Name will be the content of the control and tag will be “PlaceholderNonRecursiveB:Id”. As per Word 2010 the Tag maximum length is 64.

In code we map the tag to the PlaceHolderType enum.
    public enum PlaceHolderType    {
        None = 0,
        Recursive = 1,
        NonRecursive = 2,
        Ignore = 3,
        Container = 4
    }
There can be multiple types of PlaceHolders
  • Recursive: This type corresponds to controls where there is 1:N relation between template and data i.e. one example will be repeating a list of Items.
  • Non-Recursive: This type corresponds to controls where there is 1:1 relation between template and data i.e. one example will be showing a User name.
  • Ignore: No action is required for these controls.
  • Container: This type is required only for refreshable documents. We save the container region in CustomXmlPart the first time document is generated from template. Next time onwards we retrieve the container region that was saved and refresh the document. This makes the document self-refreshable.

I’ve named the tags in “Test_Template – 1.docx” as per their PlaceHolder type to make it more clear.

Implementation:

As explained above the Tag property will used to bind data to this content control. The project “WordDocumentGenerator.Library” is the utility library. The project “WordDocumentGenerator.Client” shows how this library can be used to generate documents.

“DocumentGenerator” is the base class that needs to be inherited by the document generators. The sample generators location is “WordDocumentGenerator.Client\Sample Document Generators”.

Samples that generate documents using data bound content controls have been added to the utility.

In order to protect the document I’ve used already created salt and hash. For an implementation where one needs to have document protection enabled for custom passwords one can view http://blogs.msdn.com/b/vsod/archive/2010/04/05/how-to-set-the-editing-restrictions-in-word-using-open-xml-sdk-2-0.aspx

Sample Generators

  1. SampleDocumentGenerator: This sample shows how to generate a non-refreshable document from a template. The content controls are populated using C# code i.e. not using data bound content controls. It covers both direct assignment as well as recursive controls. The screenshot is 1.png
  2. SampleRefreshableDocumentGenerator: This sample shows how to generate a refreshable document from a template. The content controls are populated using C# code i.e. not using data bound content controls. This is similar to SampleDocumentGenerator in implementation. Only difference is the generated document can be refreshed in this case. The screenshot is 2.png
  3. SampleDocumentWithTableGenerator: This sample shows how to generate a refreshable document from a template having Table. The content controls are populated using C# code i.e. not using data bound controls. This is similar to to SampleRefreshableDocumentGenerator in implementation. Only difference is the template has an additional table. The screenshot is 3.png
  4. SampleDocumentGeneratorUsingDatabinding: This sample shows how to generate a refreshable document from a template using data bound content controls. This requires that each of the placeholder(template’s content control) has a predefined XPath. The generated document is similar to SampleRefreshableDocumentGenerator.
  5. SampleDocumentWithTableGeneratorUsingDatabinding: This sample shows how to generate a refreshable document from a template having table using data bound content controls. This requires that each of the placeholder(template’s content control) has a predefined XPath. The generated document is similar to SampleDocumentWithTableGenerator.
  6. SampleDocumentGeneratorUsingXml: This sample shows how to generate a document from a template using XmlNode as data. This approach shows that a generic generator can be created which requires Xml as data. This requires that XPath for Tag as well as content needs to be provided. The content controls are populated using C# code i.e. not using data bound controls. It covers both direct assignment as well as recursive controls. The generated document is similar to SampleDocumentGenerator.
  7. SampleDocumentGeneratorUsingXmlAndDatabinding: This sample shows how to generate a document from a template using XmlNode as data and data bound content controls. This approach shows that a generic generator can be created which requires Xml as data. This requires that XPath for Tag as well as content needs to be provided. It covers both direct assignment as well as recursive controls. The generated document is similar to SampleDocumentGenerator.
  8. SampleDocumentWithTableAndChartsGenerator(New): This sample shows how to generate a document from a template having Charts. Implementation for Scatter and Line charts is provided. The screenshot is 4.png

Summary

The purpose of creating this utility was to use the Open Xml 2.0 SDK to generate Word documents based on predefined templates using minimum code changes. These documents can either be refreshable or non- refreshable. You can read more about it at
http://blogs.msdn.com/b/atverma/archive/2011/12/31/utility-to-generate-word-documents-from-templates-using-visual-studio-2010-and-open-xml-2-0-sdk.aspx
http://blogs.msdn.com/b/atverma/archive/2012/01/08/utility-to-generate-word-documents-from-templates-using-visual-studio-2010-and-open-xml-2-0-sdk-part-2-samples-updated.aspx
http://blogs.msdn.com/b/atverma/archive/2012/01/11/utility-to-generate-word-documents-from-templates-using-visual-studio-2010-and-open-xml-2-0-sdk-part-3.aspx
http://blogs.msdn.com/b/atverma/archive/2012/01/11/utility-to-generate-word-documents-from-templates-using-visual-studio-2010-and-open-xml-2-0-sdk-part-4.aspx

Last edited Jan 24, 2012 at 7:30 AM by atul_nith, version 15

Comments

No comments yet.