Project Description
WordDocumentGenerator is an utility to generate Word documents from templates using Visual Studio 2010 and Open XML 2.0 SDK. WordDocumentGenerator helps generate Word documents both non-refresh-able as well as refresh-able based on predefined templates using minimum code changes. Content controls are used as placeholders for document generation. It supports Word 2007 and Word 2010.

Request: Please share your comments so that it can be improved. Please share/tweet about this utility if you like it.

Update 1: Report generation samples using data bound content controls have been added
Update 2: Report generation samples using XmlNode data, XPath and data bound content controls have been added
Update 3: Sample that shows how to refresh a document from within Word has been added
Update 4: Sample that generates documents having Charts(Line Chart & Scatter Chart) has been added
Update 5: Sample that generates documents using generic Xml has been added. I will update documentation in a week.

Document generation is quite easy as code changes required are very less. A few images of document generation/refresh is displayed below

Document generation(Tables & Charts)
4.png

Template -> Document generation

3.png


Template -> Document generation -> Document Refresh

1.png


Document Refresh from within Word

2.png

Mostly one just needs to override these five methods while coding a new generator class. Sample generators have been provided in utility's source code.

        /// <summary>
        /// Gets the place holder tag to type collection.
        /// </summary>
        /// <returns></returns>
        protected abstract Dictionary<string, PlaceHolderType> GetPlaceHolderTagToTypeCollection();

        /// <summary>
        /// Ignore placeholder found.
        /// </summary>
        /// <param name="placeholderTag">The placeholder tag.</param>
        /// <param name="openXmlElementDataContext">The open XML element data context.</param>
        protected abstract void IgnorePlaceholderFound(string placeholderTag, OpenXmlElementDataContext openXmlElementDataContext);

        /// <summary>
        /// Non recursive placeholder found.
        /// </summary>
        /// <param name="placeholderTag">The placeholder tag.</param>
        /// <param name="openXmlElementDataContext">The open XML element data context.</param>
        protected abstract void NonRecursivePlaceholderFound(string placeholderTag, OpenXmlElementDataContext openXmlElementDataContext);

        /// <summary>
        /// Recursive placeholder found.
        /// </summary>
        /// <param name="placeholderTag">The placeholder tag.</param>
        /// <param name="openXmlElementDataContext">The open XML element data context.</param>
        protected abstract void RecursivePlaceholderFound(string placeholderTag, OpenXmlElementDataContext openXmlElementDataContext);

        /// <summary>
        /// Container placeholder found.
        /// </summary>
        /// <param name="placeholderTag">The placeholder tag.</param>
        /// <param name="openXmlElementDataContext">The open XML element data context.</param>
        protected abstract void ContainerPlaceholderFound(string placeholderTag, OpenXmlElementDataContext openXmlElementDataContext);

Development Status

The functionalities that can be achieved are:

Document Generation
  1. Generate document from a Word template using content controls as place holders and populate controls with data(Object) e.g. [SampleDocumentGenerator, SampleRefreshableDocumentGenerator, SampleDocumentWithTableGenerator]
  2. Generate document from a Word template using content controls as place holders(data bound content controls) and populate controls with data(Object is serialized to Xml) e.g. [SampleDocumentGeneratorUsingDatabinding, SampleDocumentWithTableGeneratorUsingDatabinding, SampleDocumentGeneratorUsingXmlAndDatabinding]
  3. Refresh the document from within the document(e.g. right click on document and click Refresh) using document-level projects for Word 2007 and Word 2010
  4. Generate document from a Word template using content controls as place holders and populate controls with data(XmlNode) e.g. [SampleDocumentGeneratorUsingXml]
  5. Generate document from a Word template using content controls as place holders(data bound content controls) and populate controls with data(XmlNode) e.g. [SampleDocumentGeneratorUsingXmlAndDatabinding]
  6. Generate document from a Word template having charts and table e.g. [SampleDocumentWithTableAndChartsGenerator]
  7. Generate document that can be
    1. Standalone: Once generated document cannot be refreshed.
    2. Refreshable: Once generated document can be refreshed. Content controls will be added/updated/deleted and content control's content will be refreshed as per data.
  8. Append documents using AltChunk
  9. Charts(Scatter and Line chart as of now)
  10. Protect Document
  11. UnProtect Document
  12. Removal of Content Controls from a document while keeping contents
  13. Removal of Foot notes
  14. Ensuring the each content control has unique Id's by fixing the duplicate Id's if any for a document
  15. Serializing an Object to Xml using XmlSerializer(Used for document generation using data bound content controls as serialized object is written to CustomXmlPart)

Content Controls
  1. Set text of a content control(not applicable for data bound content controls)
  2. Get text from a content control(not applicable for data bound content controls)
  3. Set text of content control while keeping PermStart and PermEnd elements(not applicable for data bound content controls)
  4. Set Tag of a content control
  5. Get Tag of a content control
  6. Set data binding of a content control
  7. Set text of a data bound content control from CustomXmlPart manually. This is helpful in cases when CustomXmlPart needs to be removed and this copies the text from the CustomXmlPart node using XPath.

CustomXmlPart
  1. Adding a CustomXmlPart to a document
  2. Removing CustomXmlPart from a document
  3. Getting CustomXmlPart from a document
  4. Add/Update a Xml element node inside CustomXmlPart. This is required
    1. To keep Document related metadata e.g. Document type, version etc.
    2. To make the Document self-refreshable. In this case the container content control is persisted inside a Placeholder node, the first time document is generated from template. Onwards when refreshing document we fetch the container content control from CustomXmlPart
    3. Saving the Xml e.g. serialized object which will be the data store for data bound content controls


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
---------------------------------------------------------------------------------------------------------------------
DISCLAIMER: The sample code or utility described herein is provided on an "as is" basis, without warranty of any kind.


Last edited Jan 31, 2012 at 7:02 PM by atul_nith, version 20