by Paul Evad, <pevad@kudosnet.com>

Unlocking the Secrets of RTF
Making FileMaker speak RTF for output
BONUS: RTF_FILE.FP3
PLATFORM: Mac/Windows

The Challenge

The task at hand was to create a publishing database solution to help a local publisher automate its classified style advertisements. Needless to say, organizing advertisements that are sorted by various categories within PageMaker would have been time consuming and costly. Yet a database doesn't have the layout power behind it to do the job alone either. So a merger of FileMaker Pro and PageMaker as a package solution was required.

Knowing that FileMaker could output pretty much whatever I want via a tab separated export, and using a similar technique for generating static html documents (see issue 15 and 17), I figured that I could teach FileMaker to export an rtf formatted text file so that we can specify character level formatting as well as style sheet information prior to the document hitting PageMaker.

The goal was to get the system to a point where publishing the formatted data to PageMaker was just a mouse click away. Final tweaks to columns would of course have to be done in the PageMaker layout.

Going Backwards

Of course, whenever I'm dealing with any type of automated publishing task, I prefer to design the page in the ‘end product' and then figure out how to achieve that result using the tools at hand.

In PageMaker we created the columns and layout elements, then the various text styles (using style sheets) for paragraph blocks. We had the standard heading, subhead, body and rule styles.

Then I exported a sample file from PageMaker as an rtf document to use as a guide. I probably could have worked from that, but I prefer to have a reference to fall back on to understand what a lot of the cryptic rtf code was saying.    

A complete copy of the rtf specifications is available at: http://www.Microsoft.com/kb/articles/q86/9/99.htm
(thanks to John Paul Fullerton for digging up this link)

An Overview of RTF

An RTF file consists of unformatted text, control words, control symbols and groups. Control words begin with a backslash and are usually delimited with a space character following and groups are enclosed by {}. The entire RTF document is considered to be a group itself so the basic format of the file consists of:

{<header> <document>}

The header may contain groups which designate such formatting parameters such as fonts used in the document, stylesheets, page margins and other word processing commands. A very simple rtf document would look like:

{ tf1ansi{info{doccomm generated by FileMaker Pro}}
This is the body text, and this is  bold 0. parpard
This would be the second paragraph in the documentline
\line designates a soft return, otherwise known as a line break.
}


tf1
indicates we are using the rtf version 1 specs
ansi
indicates we are using the ansi character set for the document
info
designates the following group as document information
doccomm
comments displayed in Word's Edit Summary info dialog box.
(not necessary,but it would be nice to see the expression on someone's
face when they read that :-)
 and 0
    designate bold on and bold off. plain is also used quite a bit
throughout an rtf document.
par
designates a paragraph break
pard
resets the paragraph to the default settings
\
is an escaped backslash

The FileMaker Field Structure
    I found it easiest to modify and experiment with the formatting by using some global fields to contain a bulk of the rtf coding. I created three global text fields, rtf_header rtf_body_format and rtf_footer.
The information in the rtf_header and rtf_footer fields would remain static in this example, but you could probably modify them depending on the type of record(s) you were exporting. You are only limited by your imagination here.
For this example we will use one user input field and call it simply body. To show some of the power behind this we will extract and format the first word of the first paragraph as bold, the second paragraph we will format as italic and centered.
To do this we create the following calculation fields:

body_escaped
Substitute(
Substitute(
Substitute(
Substitute(
Substitute(
        body,
"{", "{"),
"}", "}"),
"", "\"),
"¶¶", "parpard "),
"¶", "line")

firstword
LeftWords(body_escaped, 1)


firstpara

Left(
RightWords(body_escaped, WordCount(body_escaped)-1)
,
Position(
RightWords(body_escaped, WordCount(body_escaped)-1),
"parpard ", 0, 1
) +8
)

second para
Right(
body_escaped,
Length(body_escaped) - ( Position(body_escaped, "parpard", 0, 1) +9 )
)

Wrap it all together we use the rtf_body_format with some substitutions:

rtf_body
If(
Status(CurrentRecordNumber) = 1
,
rtf_header
,
""
)
&
Substitute(
Substitute(
    Substitute(
rtf_body_format,
    "_firstword_", firstword),
"_firstpara_", firstpara),
"_secondpara_", secondpara)
&
If(
Status(CurrentRecordNumber) = Status(CurrentFoundCount)
,
rtf_footer
,
""
)

Format Control
In your global rtf_body_format field enter the following:

 _firstword_ 0 _firstpara_ qc _secondpara_ parpard

Make sure you don't use any return characters in this area or you may wind up with some funny characters popping up in your end result. (qc indicates centered formatting).

Testing it out
Now you should be ready to test this out. Enter two paragraphs in your body field as a test. If you want, create multiple records and enter more than two paragraphs of text.
Export the records as a tab separated text file with a.rtf extension. The only field that you will be exporting will be rtf_body. Import the resulting file into whatever word processor or page layout program you have on hand (most will recognize the rtf codes) to check your results.

Conclusion
We just touched on a few basics with this example, but with a copy of the rtf specifications (included in the bonus files) and some imagination you can create some pretty powerful database publishing solutions. Of course, the techniques given in this article aren't the only way to do this. Experiment a little and you may just come up with a few surprises yourself :-).


## END ##

Paul Evád is the proprietor of a Kudosnet Communications Services, a member company of the Internet Marketing & Communications Group. Paul can be contacted at <pevad@kudosnet.com>