Click here to Skip to main content
Click here to Skip to main content
Go to top

Utility to generate Word documents from templates using Visual Studio 2010 and Open XML 2.0 SDK

, 12 Jan 2012
Rate this:
Please Sign up or sign in to vote.
This utility generates Word documents from templates using Content controls.

Download Source Code

This utility generates Word documents from templates using Content controls. The utility will be enhanced later as per feedback. It has been created in Visual Studio 2010 and uses Open XML 2.0 SDK which can be downloaded from here.

The next parts in this series are

In Part 2 I have discussed about

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

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“.

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.

A few samples for generating Word 2010 documents have been provided. The screenshots below display the sample template and the document generated out of this template using this utility.

Word 2010 Template -> Generated Document1.png

Word 2010 Template -> Generated Document -> Refreshed Document:2.png

Document refresh from within Word -> Refreshed Document:3.png

Template Design

The sample templates are inside “WordDocumentGenerator.Client\Sample Templates” folder. A content control as displayed below can have Title and Tag properties.

clip_image003

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.

As per the image above, 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”.

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 this link.

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. New samples will be added as per feedback.

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

 
QuestionDocati PinmemberRobert te Kaat12-Jun-14 4:01 
GeneralAnother approach to mail-merge Pinmemberj.kozelj23-Mar-14 7:59 
QuestionJava client cannot use this utility as it's not exposed as a service Pinmemberabhiseck5-Jan-12 10:26 
AnswerRe: Java client cannot use this utility as it's not exposed as a service Pinmemberatverma11-Jan-12 7:45 
GeneralMy vote of 4 Pinmemberabhiseck5-Jan-12 10:25 
GeneralRe: My vote of 4 Pinmemberatverma11-Jan-12 7:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 12 Jan 2012
Article Copyright 2012 by atverma
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid