RTF from ASP with Word
beginner level manual

Home PDF from ASP with LaTeX PDF Forms with Acrobat PDF from ASP with COM

To generate Rich Text Format files with ASP, it's not necessary to learn complete RTF specification. It is possible to create template in word processor and them transform it to ASP with minimal changes. This requires only basic knowledge of RTF commands, most of which can be easily guessed from their names.

Rich Text Format

Rich text format is similar to HTML: it is human readable,  with text and markup commands mixed. Here is a text of simple RTF file which says: Hello, World!

{\rtf1\ansi
{\fonttbl{\f0\froman Times New Roman;}}
\pard
\plain\f0
{\i Hello} ,
{\b World}!
\par }


Complete RTF specification can be found at Microsoft web site.

Create a template of document in word processor which is capable of dealing with RTF files (Wordpad, Word, etc.). Use easily searchable placeholders instead of data which will be added dynamically in ASP. Here is an example, it uses QQQ1, QQQ2, ... as placeholders. Save document in RTF format.

Now you have to decide what MIME type will be used by this document. The MIME type tells the browser how to handle received document. If you have Word installed, it is good idea to use "application/msword". In this case Word will be automagically invoked and embeded in browser window. If you use another application, you should check what MIME type is registered to RTF file: run menu command View=> Options=> File Types in My Computer window and find Rich Text Format in a list of registered file types.

Now open RTF template in text editor which work with plain text files only (like Notepad). Make sure it don't wrap long lines or make any other changes to original file. Insert the following in the beginning of file, right before the opening bracket of {\rtf1:
<%Response.ContentType = "application/msword" %>{\rtf1\ansi
It is very important that there are no spaces before and after <%...%>.

Search the text for placeholders and replace them with data you need to be presented in document. The easiest way is to use <%=....%> notation. Here is previous exapmle (with appropriate changes made) in source and working forms.

Save the file, change its extension to .asp and move to a web directory with script permissions. You're done.

word-ie.gif (8235 bytes)

Generating of tables is also simple if they contain constant number of columns (you are generating rows of the same shape). This is because each cell has its own description of dimensions and borders, so recalculating these numbers is complicated (but possible). First, make a template with headings and two or three rows. Fill these rows with placeholders. Then find were ehach row begins and ends, delete second and third row and pay attention to any additional commands and brackets which are at the end of last row. As you can see from the example RTF document (line breakes added for readability), the pattern has the form:

\trowd 
   some stuff...
{\trowd
   some stuff...
\row}
Delete all but one row, fill it with the data you need and put inside a loop leaving closing stuff outside. Here is an example created with Word 2000 (it may not work with previous versions or may not open in the browser. Depending on your configuretion, it may be necessary to save a file on disk, remane it to ".rtf" and then open in a separate Word window): template, source and result.

 

(c) 1999  Radamir