Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / productivity / Office

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

4.50/5 (4 votes)
12 Jan 2012CPOL3 min read 48.3K  
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.

License

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