This is a step by step instruction on how to dynamically generate MS Word 2007 documents.
Background on Word 2007 Document Internals
Word 2007 document is based on the Office Open XML file formats. As you will see, the Office Open XML file formats are based on standard ZIP file technology. Each top-level file is saved as a ZIP archive, which means you will be able to open a Word document just as you would any other ZIP file and look inside.
Note that the 2007 Microsoft Office system applications such as Word and Excel have introduced file extensions for documents that use the new format. For example, the .docx extension is used for Word documents stored in the Office Open XML file formats while the more familiar .doc extension continues to be used for Word docs stored in the binary format.
Let’s create a new document in Word 2007 and add "Hello Word" as the text. Save the document using the default format to a new file named Hello.docx and close Word. Next, locate Hello.docx using Windows Explorer and rename it Hello.zip. Open Hello.zip and see the structure of folders and files that Word has created inside (see Figure).

Figure 1 DOCX File is a ZIP Archive
The top-level file (Hello.docx) is known as a package. Since a package is implemented as a standard ZIP archive, it automatically provides compression and it makes its contents instantly accessible to many existing utilities and APIs on both Windows platforms and other platforms alike.
Inside a package there are two kinds of internal components: parts and items. In general, parts contain content and items contain metadata describing the parts.
A basic package contains an XML file called [Content_Types].xml at the root, along with three directories: _rels, docProps, and a directory specific for the document type (for example, in a .docx word processing package, there would be a word directory). The word directory contains the document.xml file which is the core content of the document.
A part is named using a Uniform Resource Identifier (URI) that contains its relative path within the package file combined with the part file name. For example, the main part within the package for a Word document is /word/document.xml. Here are a few more typical part names you will find inside the package for a simple Word document:
- /[Content_Types].xml
This file describes the contents of the package. It also contains a mapping for file extensions and overrides for specific URIs.
- _rels
This directory contains relationships for the files within the package. To find the relationships for a specific file, look for the _rels directory that is a sibling of the file, and then for a file that has the original file name with a .rels appended to it. For example, if the content types file had any relationships, there would be a file called [Content_Types].xml.rels inside the _rels directory.
- /_rels/.rels
This file is where the package relationships are located. Applications look here first. Viewing in a text editor, one will see it outlines each relationship for that section. In a minimal document containing only the basic document.xml file, the relationships detailed are metadata and document.xml.
- /docProps/core.xml
This file contains the core properties for any Office Open XML document.
- /word/document.xml
This file is the main part for any Word document – which we will be manipulating in our code.
The Project
We will be using Visual Studio 2008, .NET Framework 3.5, the NorthWind database and a simple Word template in our application.
Step 1 – Create a Word 2007 template.
Create a simple Word 2007 file with placeholders enclosed between two #’s (like #ORDERID#) and save it as ‘MyTemplate.docx’
Below is the content I used:
From: My Company
#ADDRESS#
#CITY#, #REGION#, #POSTCODE#
#COUNTRY#
Dear #CUSTOMERID#
We're pleased to inform you that your order number #ORDERID# is going to be shipped on #SHIPPEDDATE#.
If you have any questions or comments, please feel free to give us a call at #HOMEPHONE#
Regards,
#FIRSRNAME# #LASTNAME#
#TITLE#
For More information
http://openxmldeveloper.org/
http://www.codeproject.com/KB/cs/GenerateWord2007.aspx
http://en.wikipedia.org/wiki/Office_Open_XML
http://msdn.microsoft.com/en-us/magazine/cc163526.aspx#S1
We will replace the place holders with values from the Orders table of the Northwind database.
Step 2 – Create an OrderInformation class
This class simply represents an Order object with properties for OrderID, CustomerID,….
Step 3 – Create a GenerateDocument class
A main method, public void GenerateWord( OrderInformation orderInfo), is implemented to generate the new document named after the value of OrderID of the current Order.
In this class, System.IO.Package is used to replicate the template, replacing the place holder variables in the /word/document.xml file with the actual values from the current Order.
Below is the code:
public void GenerateWord( OrderInformation orderInfo)
{
string templateDoc = string.Format("{0}{1}", ConfigurationManager.AppSettings["TemplateFolder"], ConfigurationManager.AppSettings["TemplateFile"]);
string filename = string.Format("{0}{1}.docx", ConfigurationManager.AppSettings["OutputFolder"], orderInfo.OrderID);
// Copy a new file name from template file
File.Copy(templateDoc, filename, true);
// Open the new Package
Package pkg = Package.Open(filename, FileMode.Open, FileAccess.ReadWrite);
// Specify the URI of the part to be read
Uri uri = new Uri("/word/document.xml", UriKind.Relative);
PackagePart part = pkg.GetPart(uri);
XmlDocument xmlMainXMLDoc = new XmlDocument();
xmlMainXMLDoc.Load(part.GetStream(FileMode.Open,FileAccess.Read));
xmlMainXMLDoc.InnerXml = ReplacePlaceHoldersInTemplate(orderInfo, xmlMainXMLDoc.InnerXml);
// Open the stream to write document
StreamWriter partWrt = new StreamWriter(part.GetStream(FileMode.Open, FileAccess.Write));
//doc.Save(partWrt);
xmlMainXMLDoc.Save(partWrt);
partWrt.Flush();
partWrt.Close();
pkg.Close();
}
private string ReplacePlaceHoldersInTemplate(OrderInformation orderInfo, string templateBody)
{
//#ADDRESS#
templateBody = templateBody.Replace("#ADDRESS#", orderInfo.FirstName);
//#CITY#
templateBody = templateBody.Replace("#CITY#", orderInfo.City);
…
}
Step 4 – Implement a web or WinForms application utilizing the above classes
Here is a sample button_Click handler:
protected void btnFrom_Click(object sender, EventArgs e)
{
string orderID = “1234”;// lstOrders.SelectedValue;
//generate documet
OrderInformation orderInfo;
GenerateDocument doc = new GenerateDocument();
orderInfo = doc.GetOrderInformation(orderID);
doc.GenerateWord(orderInfo);
}
As an example of how to create a provider for dashCommerce, I have implemented an Authorize.NET Payment Provider for dashCommerce based on Advanced Integration Method guidelines from Authorize.NET http://developer.authorize.net/guides/AIM/ and following the structure of the existing providers in dashCommerce.
Below is the general outline of the implementation:
1. Implement the Store.Services.PaymentService.IPaymentProvider interface.
- The provider must implement this interface, specifically the Authorize, Charge and Refund methods. You can see the code at Store.Services\PaymentService\AuthorizeNetPaymentProvider.cs
- The actual implementation of the above methods is in Store\Services\PaymentService\AuthorizeNET\AuthorizeNetService.cs and the provider has a reference to this class.
- The class instance has a constructor with parameters matching the configuration properties stored (for Authorize.NET, we need to store atleast API Version, API LoginID, TransactionKey and IsTestMode).
- The implementation for Authorize, Charge and Refund methods are almost the same except on some parameters to Authorize.NET services like transaction type "x_type" ("AUTH_ONLY" = Authorize, "AUTH_CAPTURE" = Charge, "CREDIT" = Refund). For a complete implementation, please see the code. Below is a rough implementation guideline:
i. use a NameValueCollection and build up the arguments like so NameValueCollection objInf = new NameValueCollection(30);
objInf.Add("x_version", account.AuthNetVersion);
objInf.Add("x_login", account.AuthNetLoginID);
objInf.Add("x_card_num", order.CreditCardNumber);
objInf.Add("x_amount", order.Total.ToString());
…
ii. Pass them to the UploadValues method of the WebClient class, which returns a byte[] response
WebClient objRequest = new WebClient();
objRetBytes = objRequest.UploadValues(objRequest.BaseAddress, "POST", objInf);
iii. Parse that using the Encoding.Ascii.GetString. Split the string by "," and eliminate the field delimiters "".
objRetVals = System.Text.Encoding.ASCII.GetString(objRetBytes).Split(",".ToCharArray());
iv. Process the return values and handle accordingly
// Process Return Values
response.ReturnCode = objRetVals[0].Trim(char.Parse(""));
if (objRetVals[0].Trim(char.Parse("")) == "1")
{
// Returned Authorisation Code
response.AuthorizationCode = objRetVals[4].Trim(char.Parse(""));
// Returned Transaction ID
response.TransactionID = objRetVals[6].Trim(char.Parse(""));
strMessage = "Transaction completed successfully.";
}
else
{//look into the code. There are hundreds of possible errors … can’t put them here …
2. Add a control that creates and saves the configuration settings for the provider in the same order they are used in the class instance above.
- You can see the code at Web\admin\controls\configuration\paymentproviders\authorizenetconfiguration.ascx.cs
- When we save/configure the providers, the properties need to be added to the AuthorizeNetPaymentProvider’s ProviderSettings in the order they are expected by the constructor of the payment service above.
3. Configuring the Authorize.NET Payment Provider in Your dashCommerce site:
After this just go ahead and configure the new payment provider as you would with any additional provider.
1. Log In to your dashCommerce web site using your administrator account. Proceed to Payment Configuration -> Configure Payment Providers. Select the AuthorizeNetPaymentProvider from the dropdown list in the PaymentProvider Configuration section and click “Configure Provider”. This will then display the AuthorizeNet Payment Provider Configuration panel, as seen below:

2. Put in the values from your Authorize.NET account in the appropriate boxes.
3. If you are ready to go live with your site, then ensure you check “Is Live”.
4. Press Save when done.