Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
We need to automate the generation of documents from pre-designed word templates in ASP.NET application. The solution should enable us to populate (Word) template documents with the database data. The documents should be in the form of Word documents so the end users will be able to add/modify them after they have been generated.

Is there an existing .NET library that performs population of word documents/templates (mail merge) with application data? We are inclined to use a free library but are also open to hear about the commercial ones.


UPDATE:
In case anyone else having the same problem in the future, we ended up using Docentric Toolkit.
Posted
Updated 23-Mar-14 7:26am
v3
Comments
Hatepeora 16-Feb-16 8:16am    
I must say this word processing library for .net has a much wider range of features for various mail merge tasks done in C#. You should check it out.

It depends on the version of Word(r) you're wanting to target. For the old .DOC files, Microsoft has a robust interop assembly, but it needs to reside on the computer/server running the tasks. This may present security concerns for public-facing machines.

For the new .DOCX files, I was able to accomplish the task by creating a template with text=placeholders for the data to be inserted. I renamed the template to be a .ZIP file. (The new .docx format is simply a zip file with specific document elements (folder structure, xml files, etc).

In the zip folders there is a file ~\word\document.xml that contains the text of the document.

For each new document, I unzip the .zip file to an empty directory, replace the placeholders in the xml file with the proper data, and then re-zip the directory and rename the resulting zip file to end in .docx. The last step is to delete the temporary files and then provide a link for downloading the new .docx file.

I use the free Ionic zipping library for ASP http://dotnetzip.codeplex.com/[^] to unzip

I've also had success with swapping out images (in the ~/word/media folder), document properties, and so forth.

This method works very well for simple documents with only a few images and a few pages of text. For very large documents with many chapters, headings, many large images, complex formatting, table of contents, it can strain the server during the un-zip, re-zip phases.
 
Share this answer
 
Comments
Maciej Los 11-Mar-14 16:36pm    
Very interesting explanation ;)
+5!
filip_prasinovic 12-Mar-14 13:46pm    
Thanks! I have been studying Open XML for the last couple of days. It looks like you can do anything by this library but I can see that this will be a very demanding task to implement all these required features (placeholders, lists and images). I found Aspose.Word library that can do mail merge (using Merge Fields), but is a bit pricey. We can pay for such a library but I would like to know if there are any other similar .NET libraries around?
Bob@work 12-Mar-14 15:06pm    
There are other libraries out there, and other workarounds for creating DOCX files. But the libraries were too expensive to justify the cost for creating small, simple documents, and the workarounds resulted in Microsoft-specific file formats that Linux and Apple users couldn't use.

The template method I use is really pretty easy to set up - again for small simple documents.

Good luck with your search!
In general, .NET developers have several options to choose from when implementing document generation automation or mail merge (in your case):

  • Office Word Automation (Office COM): Although this approach can still be used in .NET, it is not recommended any more, especially on server side (it relies on MS Word, which needs to be installed on the production machine).
  • Open XML SDK: This Microsoft's library[^] can be used to create your own document parsing functionality to process word documents (to find and replace placeholders...). This is the preferred way to process documents over the Office COM because it does not need Word installed on the server.
  • 3rd party libraries: As far as I know there are several .NET libraries that enable you to process documents without relying on MS Word. In my company we are using Docentric Toolkit which has really good mail merge capabilities.
 
Share this answer
 
v2
Comments
filip_prasinovic 23-Mar-14 13:20pm    
This 3rd party .NET library is exactly what I've been looking for. Thanks!

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900