ASP.NET generating Dynamic word Documents

Introduction

This is my first article to the Tech World.

This document will help programmers to dynamically create documents using word template without generating any word object on the server.

Background

In some web applications it is required to create a document generated dynamically, like an Offer letter given to the candidate, some other official letters used during the interviewing purpose, or mark sheet templates etc.

In such type of documents most of the things are similar except for few changes.

Let’s take some examples.

1.Mark Sheet: Name, Marks Scored, Seat Number etc will change from student to student but other details like the Exam name, stream , university name etc. will remain same.

2. Offer letter: The name, address, post date of offer and date of joining, designation and pay structure etc. will change from candidate to candidate. But other details will remain same.

Solution

To generate O/P for such type of requirement follow a step wise procedure given below.

Step1: Create A Word Template

Why Word template?

I.It is very easy to generate formatted O/P in word.

II.Sub scripts and super scripts e.g. ™ can be easily written.

III.Maintenance of such documents can be easily done, if changes arise.

How to Create Template?

e.g.

1. Offer Letter:

##Address##, ##DOF##

Dear ##Name##,

We are glad to appoint you for the designation ## desig ##.

Mark sheet:

Name: ##NAME##

Seat Number: ##Number##

Subject1 ##SUB1##

Subject2 ##SUB2##

Subject3 ##SUB3##

Subject4 ##SUB4##

Subject5 ##SUB5##

Total ##Total##

Percentage: ##PER##

In the e.g.1 we see ##Address##, ##DOF##, ##NAME## and ##desig##. All these will be replaced by dynamic data selected by the user. Whereas the static data will remain the same.

Similarly in example 2. for Mark sheet

##SUB1## , ##SUB2## , ##SUB3## etc. will be dynamic.

This dynamic data can be made available from the database.

Once you have formatted your template file in word as per the guidelines outlined above save the file. Provide appropriate name to the file.

Note: The saving format should be Rich Text Format(RTF). Click on File – select save as and then select the path and save the file as .rtf extension file.

Step2: Proceed with the code & instructions given below

Make use of file scripting object to copy the template file into some temporary file.

Note: The path of the temporary file should be fixed and it should also have a .rtf extension as the template file.

Easy maintainability – Changes in the template can be made easily and updated.

Just Few Lines of code.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

Hi,
if my word template has a table and table has a row with 4 cell, and if i wanted to add the rows in table dynamically then
how can do this?
And
How do we write the content into the cell of inserted rows?

In my output file I have to use "C39HrP24DhTt" ( a barcode font) for ##ref##. When I change the font in the input file the "string.replace" doesn't recongnize this font. That's why I'm thinking to replace the data first then on the way to write to the output file changing the font.

If you are using a RTF "rich text file" then check the desired font is supported by RTF file format. Installing it explicitly too may help. If you check the final replacement string prior writing (in some intermediate window), then you can be a more clearer picture like the how a bold text , different font text will appear.

i have a problem with the code. i got it all working, connecting to the database and reading / writing to the file.

My problem is when i try to replace more than one field, it only replace the last. i would think that the problem is in my while loop, but i cant get it to work. This is my loop, can someone help me get it right?

I am testing your method and have a few of issues:
1) When saving the .doc as .rtf the file size jumped from 194kb to 2188kb. This is a lot of data to be moving around on the web especially as there may be anything thing between 10 to 30 actions occurring at the same time.
2)The text that was entered in the database was cut and pasted from the .rtf. When it was written onto the template with the streamwriter all the formatting was lost. Is there a way to enter the data and ensure formatting is stored in the database and written by the writer?
3) I aslo seem to be experiencing an issue when two or more people attempt to access the same file. The first browser goes blank. Not sure if this is only because of my test environment.

Hi,
Please check the description point wise.
1. It should not increase the size.In fact the idea behind .rtf file is to reduce the size.
Open the file and in save as select .rtf file type document. It shpuld reduce the size.
2. If you want to save the formating in database then you can write the whole .rtf file. I havent tried but it should work. You can have a debug before the file writing code and see how it gets your fonts . it will have a long string say like verdana\10pt and something like that.
3. If you are opening more than one instance then it may happen. In your task manager check for word file instances.This may occour because of the same

!th great article yust what i needed. But i needed it it C#/.NET 2.0 so i did a dirty rewrite that replaces a word String "##Gebruiker##" with "blaat". Have fun with it.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text.RegularExpressions;

I've create a word template .rtf with mergefield <<refno>> and <<Title>>, I can successful generate the .doc with correct format by using the method in this article. but the statement
str = replace(str, "##refno##", "your_real_string") never recongnized and found "##refno##", or "##Title##". I've also tried
str = replace(str, "MERGEFIELD refno \* MERGEFORMAT", "your_real_string") without luck
"MERGEFIELD refno \* MERGEFORMAT" is the string displayed when you click "toggle field codes" on the specific <<refno>> merge field.

Hi,
I had a quick look at your article and the content was ok, but unfortunately your formatting of the article is not so good. It kind of makes reading your article a bit of hard work.
Try using the <pre> tags to put your code in.
The guys on here are kind of really picky on the formatting of the tutorials which may be why the voting of your article is not so good.

thats just my 2c

Keep it up,

"a fool will not learn from a wise man, but a wise man will learn from a fool"
"It is hard to fail, but it is worse never to have tried to succeed." - Theodore Roosevelt
"Success is going from failure to failure without losing your enthusiasm." - Winston ChurchillMy Website || My Blog