Click here to Skip to main content
15,885,278 members
Articles / Productivity Apps and Services / Microsoft Office

Utility to Generate Word Documents from Templates using Visual Studio 2010 and Open XML 2.0 SDK

Rate me:
Please Sign up or sign in to vote.
4.50/5 (4 votes)
12 Jan 2012CPOL3 min read 47.7K   34   4
This utility generates Word documents from templates using Content controls.

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 ways 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 Document

309307/1.png

Word 2010 Template -> Generated Document -> Refreshed Document

309307/2.png

Document Refresh from Within Word -> Refreshed Document

309307/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.

C#
    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 be 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.

This article was originally posted at http://www.atulverma.com/feeds/posts/default

License

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


Written By
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

Comments and Discussions

 
QuestionJava client cannot use this utility as it's not exposed as a service Pin
abhiseck5-Jan-12 10:26
abhiseck5-Jan-12 10:26 
AnswerRe: Java client cannot use this utility as it's not exposed as a service Pin
atverma11-Jan-12 7:45
atverma11-Jan-12 7:45 
GeneralMy vote of 4 Pin
abhiseck5-Jan-12 10:25
abhiseck5-Jan-12 10:25 
GeneralRe: My vote of 4 Pin
atverma11-Jan-12 7:46
atverma11-Jan-12 7:46 

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

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