Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Utility to generate Word documents from templates using Visual Studio 2010 and Open Xml 2.0 SDK – Part 2 (Samples Updated)

, 12 Jan 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
This is the second post of this series.

Download Source Code

This is the second post of this series. The previous post can be read at Part 1. The next post can be read at Part 3.

In Part 1 I discussed about:

  • Document generation using Content Controls and Open XML 2.0 SDK
  • Creating Word templates
  • Implementation and Samples

In Part 3 I have explained one of the way to “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“. In this post I’ll discuss:

  • List of functionalities that can be achieved using the utility/source code
  • Description regarding Samples provided with utility
  • New samples added in this update

The sample document generators are discussed later. The functionalities that can be achieved using the utility/source code are:

Document Generation

  1. Generate document from a Word template using content controls as place holders and populate controls with data (Object) [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). [SampleDocumentGeneratorUsingDatabinding, SampleDocumentWithTableGeneratorUsingDatabinding, SampleDocumentGeneratorUsingXmlAndDatabinding]
  3. Generate document from a Word template using content controls as place holders and populate controls with data (XmlNode) [SampleDocumentGeneratorUsingXml]
  4. Generate document from a Word template using content controls as place holders(data bound content controls) and populate controls with data (XmlNode) [SampleDocumentGeneratorUsingXmlAndDatabinding]
  5. 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 [Explained in next post i.e. Part 3]
  6. 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.
  7. Append documents using AltChunk
  8. Protect Document
  9. UnProtect Document
  10. Removal of Content Controls from a document while keeping contents
  11. Removal of Foot notes
  12. Ensuring the each content control has unique Id's by fixing the duplicate Id's if any for a document
  13. 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

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. The screenshot is:
  2. image_thumb

  3. 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 shown here:
  4. image_thumb1

  5. 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 shown here:
  6. image_thumb2

  7. 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.
  8. 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.
  9. SampleDocumentGeneratorUsingXml (New): 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.
  10. SampleDocumentGeneratorUsingXmlAndDatabinding (New): 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

atverma
Software Developer (Senior)
India India
Atul works at Microsoft as a .NET consultant. As a consultant his job is to design, develop and deploy enterprise level secure and scalable solutions using Microsoft Technologies.
 
His technical expertise include .NET Framework(4.0, 3.5, 3.0), WPF, WCF, SharePoint 2010, ASP.net, AJAX, Web Services, Enterprise Applications, SQL Server 2008, Open Xml, MS Word Automation.
 
Follow him on twitter @verma_atul
 
He blogs at
http://www.atulverma.com
http://blogs.msdn.com/b/atverma
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.141029.1 | Last Updated 12 Jan 2012
Article Copyright 2012 by atverma
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid