Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


How To Output RTF-formatted Documents with ASP : Page 2

HTML isn't your only choice for displaying content from ASP. Learn how to build a simple class to generate RTF documents and tables on the fly with basic text and paragraph formatting.

Create the Basic RTF Document
RTF markup must contain the opening tag {\rtf1 and a closing bracket }. You can test your minimum configuration with the following code:

   MyFile.WriteLine ("Hello world!")
   MyFile.WriteLine ("}")
MyFile.WriteLine does for RTF output what Response.Write does for screen output. Depending on your system settings, when you run this code in ASP you will either see the resultant RTF in Word within the browser window (meaning Word has been invoked by the browser to read the page) or you will be prompted to download the new RTF file for saving and viewing. You should also note that the actual text stream "Hello world!" is not formatted in any way.

Creating an RTF Document with Markup
RTF markup can be very complex. You can download Microsoft's RTF 1.7 markup specifications to get the full list of supported markup.

The RTF specifications cover a wide variety of elements in a document besides basic text formatting. You can specify a font family, a color table, document properties (such as Title, Author, Version, and other fields found in Word under File | Properties). If you create an RTF file in Word and open it in Notepad you can see the RTF code directly. Listing 1 shows a commented and formatted example of Word RTF markup for a 2 x 3 table formatted with a single line grid.

None of the section specifications shown in Listing 1 is required; the simple Hello World code example shown earlier is a perfectly valid RTF document. However, you'll probably want to do some intermediate amount of text and paragraph formatting. That's not difficult. For example, here's the RTF markup to create bold text:

      MyFile.WriteLine ("\b This is bold \b0")
Notice the opening \b and closing \b0 tags, which function much like <b> and </b> tags in HTML. You have to supply opening and closing tags for text formatting styles such as italics, underline, etc. Paragraph formatting such as right justify, center, etc. are 'overwritten' when another paragraph style is specified.

You can, of course, manually apply RTF markup to your ASP text output, but that can get tedious and you won't be able to reuse the code. It is much easier, in the long run, to create RTF markup functionality in code, and place that in an include file so you can use it in more than one project. Depending on your needs, you can create a simple function library, or a comprehensive class that functions as a custom RTF 'black box' generator for your ASP applications.

RTF Markup in a Class Module
VBScript classes let you to declare a class object and work with the objects and methods wrapped by the class. A well-formed class structure also simplifies transitioning your code to .NET or other OO frameworks more easily. In the following discussion, I'll show you a couple of RTF markup functions and how to call them from a class in a separate page.

To begin, create a new ASP page called RTF_Classes.asp file that you'll use as an INCLUDE file in your main OutputToRTF.asp pages. The sample code that accompanies this article includes a class called RTFTextMarkup that exposes several text and paragraph formatting methods used in the OutputToRTF.asp page. To begin, write the following code in the RTF_Classes.asp file:

Class RTFTextMarkup
End Class
Inside this class, create the function RTF_b, which applies bold formatting to text you send it:

Public Sub RTF_b(Text)
   MyFile.WriteLine "\b"
   MyFile.WriteLine Text
   MyFile.WriteLine "\b0"
End Sub
You can create a more sophisticated version that overloads simple formatting functions, such as RTF_f() to create any mixture of text markup with a generic closing RTF_unf() that will remove all the formatting called in the opening function. I will leave this implementation as an exercise to the reader. For the purposes of this article, the class methods operate on blocks of text in the RTF output page.

Sample RTF formatting functions:

'declare a line break   
Public sub RTF_br
   MyFile.WriteLine "\par"
End Sub

'italic block
Public sub RTF_i(Text)
   MyFile.WriteLine "\i"
   MyFile.WriteLine Text
   MyFile.WriteLine "\i0"
End Sub
'center a paragraph
Public sub RTF_Center(Text)
   MyFile.WriteLine "\qc"
   MyFile.WriteLine Text
End Sub

'justify a paragraph
Public sub RTF_Justify(Text)
   MyFile.WriteLine "\ql"
   MyFile.WriteLine Text
End Sub
Wrapping these functions (and any others needed for the RTF markup library) in a class, makes them easy to implement them in any RTF output page. Include the RTF_Classes.asp page in a new OutputToRTF.asp RTF output page and instantiate the class with the line:

   Set p = New RTFTextMarkup
You can then invoke the functions as follows:

p.RTF_b ("This should be in bold")
p.RTF_i ("This should be in italics")
p.RTF_b ("Another bold font line!")
p.RTF_Center ("this is a paragraph block")
Remember to end the entire RTF output stream in a closing tag with MyFile.WriteLine ("}").

As you can see, the OutputToRTF.asp page now applies the RTF markup methods to given blocks of text—and the naming style intuitively mimics HTML markup. You no longer need to write code such as MyFile.WriteLine "\b" and MyFile.WriteLine "\b0" because the class library emits the markup automatically. Again, you can build more flexible and sophisticated functions that apply the various combinations of text markup that you need. A class such as this transfers easily to VB or VB.NET, so you can compile the class and make it available for VB and .NET desktop or ASP.NET applications as well. You can roll your own RTF output black box!

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date