Project DescriptionWordDocumentGenerator 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 addedUpdate 2: Report generation samples using XmlNode data, XPath and data bound content controls have been addedUpdate 3: Sample that shows how to refresh a document from within Word has been addedUpdate 4: Sample that generates documents having Charts(Line Chart & Scatter Chart) has been addedUpdate 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)
Template -> Document generation
Template -> Document generation -> Document Refresh
Document Refresh from within Word
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
- Generate document from a Word template using content controls as place holders and populate controls with data(Object) e.g. [SampleDocumentGenerator, SampleRefreshableDocumentGenerator, SampleDocumentWithTableGenerator]
- 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]
- 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
- Generate document from a Word template using content controls as place holders and populate controls with data(XmlNode) e.g. [SampleDocumentGeneratorUsingXml]
- 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]
- Generate document from a Word template having charts and table e.g. [SampleDocumentWithTableAndChartsGenerator]
- Generate document that can be
- Standalone: Once generated document cannot be refreshed.
- 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.
- Append documents using AltChunk
- Charts(Scatter and Line chart as of now)
- Protect Document
- UnProtect Document
- Removal of Content Controls from a document while keeping contents
- Removal of Foot notes
- Ensuring the each content control has unique Id's by fixing the duplicate Id's if any for a document
- 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
- Set text of a content control(not applicable for data bound content controls)
- Get text from a content control(not applicable for data bound content controls)
- Set text of content control while keeping PermStart and PermEnd elements(not applicable for data bound content controls)
- Set Tag of a content control
- Get Tag of a content control
- Set data binding of a content control
- 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
- Adding a CustomXmlPart to a document
- Removing CustomXmlPart from a document
- Getting CustomXmlPart from a document
- Add/Update a Xml element node inside CustomXmlPart. This is required
- To keep Document related metadata e.g. Document type, version etc.
- 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
- 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.aspxhttp://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.aspxhttp://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.aspxhttp://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.