Rich Text Format (RTF) and Watermarks

 

Seldom is the question asked, "Is there an RTF directive that can be used to add watermarks in RTF documents? "

One day recently this question found me, and after delving into the world of the Rich Text Format (RTF) specification you may in turn be interested in what I found, which includes a new appreciation for RTF. At risk of repeating what is stated in many blogs in many similar ways, the RTF format affords fluidity of fidelity over time. What this essentially means is that RTF format provides a significant hedge against ever changing specifications. Thus, RTF is kind of a time capsule document format that can be opened in the future, with the same fidelity potential encoded at the time it was created.

Of course, RTF implementations do differ from application to application. However, since applications SHOULD ignore control words they do not implement they will only lack the ability to render the RTF at the same fidelity encoded in the original document. And, since RTF is a Rich "Text" Format, the encoding is in plain text and not encoded in a more abstruse Binary File Format. So reading the format and implementing the format is far more accessible by comparison. 

To delve the RTF specification, refer to this blog, which references the Word 2007: Rich Text Format (RTF) Specification, version 1.9.1 (the latest version at the time of this writing). This is the essential reference for implementing the RTF specification. 

Now that I’ve provided a little background and the necessary references, I’ll restate the purpose of this blog, which is to answer the question, "Is there an RTF directive that can be used to add watermarks in RTF documents?

The answer is derived from the RTF specification. 

First, nowhere in the RFT specification will you find the word "Watermark" or an explanation for it. That’s because there is no Watermark control code or object in the RTF specification per-se. Watermarking is implemented in RFT by various control codes that compose a Watermark, and in this case also includes a special Word Watermark object that of course is not part of the RTF specification (see below). 

An easy way to discover the control codes for implementing a Watermark in RTF (among other things) is to create a Watermark in Word and save the file as an RTF document. A Watermark in Word is a Shape (i.e. a Drawing Object). In RTF the shape that composes the Watermark begins with the control word "\shp". 

In the RTF specification reference, refer to the section titled, "Word 97 Through Word 2007 RTF for Drawing Objects (Shapes)", which details the meaning of the highlighted keywords below as they relate to "\shp". The first destination ( \shp) is always present. This control word groups everything related to a shape together. Following the destination change is basic information regarding the shape. The following keywords with values can appear in any order after the "{ \shp" control word.

The basic syntax for drawing objects in RTF is as follows:

<shape>

'{' \shp <shpinfo> <shpinst> <shprslt> '}'

<shpinfo>

 

 

\shpleftN? \shptopN? \shpbottomN? \shprightN? \shplidN? \shpzN? \shpfhdrN? \shpbxpage ? \shpbxmargin ? \shpbxcolumn? \shpbxignore? \shpbypage ? \shpbymargin ? \shpbypara? \shpbyignore? \shpwrN? \shpwrkN? \shpfblwtxtN? \shplockanchor? \shptxt?

<shpinst>

'{\*' \shpinst <sp>+ '}'

<sp>

'{' \sp <sn> <sv> <hsv>? '}'

<sn>

'{' \sn ... '}'

<sv>

'{' \sv ... '}'

<shprslt>

'{\*' \shprslt ... '}'

<hsv>

'{\*' \hsv <accent> & \ctintN & \cshadeN '}'

<accent>

\caccentone| \caccenttwo | \caccentthree | \caccentfour | \caccentfive | \caccentsix

 

I won’t go into every control word definition, you can surely follow every control word definition in the RTF specification, but I will point out the control words germane to answering the question at hand. The following is a snippet from a test RTF document I used by saving a Microsoft Word document with a Watermark as RTF. 

{\shp{\*\shpinst\shpleft0\shptop0\shpright10557\shpbottom2639\shpfhdr0\shpbxmargin\shpbxignore\shpbymargin\shpbyignore\shpwr3\shpwrk0\shpfblwtxt1\shpz0\shplid2049{\sp{\snshapeType}{\sv 136}}{\sp{\sn fFlipH}{\sv 0}}{\sp{\sn fFlipV}{\sv 0}} 

{\sp{\sn gtextUNICODE}{\sv CONFIDENTIAL}}{\sp{\sn gtextSize}{\sv 65536}}{\sp{\sn gtextFont}{\sv Calibri}}{\sp{\sn fGtext}{\sv 1}}{\sp{\sn gtextFStretch}{\sv 1}}{\sp{\snfillColor}{\sv 12632256}} 

{\sp{\snfillOpacity}{\sv 32768}}{\sp{\sn fLine}{\sv 0}}{\sp{\sn wzName}{\svPowerPlusWaterMarkObject357476642}}{\sp{\sn posh}{\sv 2}}{\sp{\snposrelh}{\sv 0}}{\sp{\sn posv}{\sv 2}}{\sp{\sn posrelv}{\sv 0}}{\sp{\sn dhgt}{\sv 251659264}}… 

The first thing to point out is the control symbol "\*". I’ve alluded to this before, and per the RTF specification, "Destinations added after the 1987 RTF Specification may be preceded by the control symbol \* (backslash asterisk).This control symbol identifies destinations whose related text should be ignored if the RTF reader does not recognize the destination control word. " This means the shape will be ignored by an RTF reader that does not implement the control word. You can test out a quick example of this by creating an RTF document in Word that contains a Watermark then open the document in WordPad and you’ll notice the Watermark will not be rendered since WordPad does not support Watermarks. Open the same RTF file in Word and of course you see the Watermark. 

In my example file above, I used a "CONFIDENTIAL" Watermark which refers to the Shape (control word "\shp") and the Drawing Object "PowerPlusWaterMarkObject"). The Drawing Object is a special object identifier specifically for Word. You’ll find references to this object in API calls, macros, etc. (i.e. wherever you are dealing with a Word Watermark). 

You can refer to the RTF specification and note the definitions for the shape (position, text font/size, left, top, right, bottom, etc). Just follow the nested open/close braces "{ }" (the Group symbol), which indicates the beginning and end of each Group (and Groups can and will be nested). Essentially, the entire RTF file is in a Group. 

An RTF file has the following syntax:

<File>

'{' <header> <document> '}'

Note, the important takeaway point in this example is that there is no "Watermark" per-se in RTF, but rather the "\shp" control words compose the Watermark feature for the RTF reader. 

If there is another feature you’d like to examine to see how it is composed/represented in RTF, just open Word and save your example as RTF and open in your editor of choice. It’s an easy way to understand RTF and how various features are implemented in the RTF format.

 

NOTE: Questions and comments are welcome. However, please DO NOT post a comment using the comment tool at the end of this post. Instead, post a new thread in the Open Specifications Forum at https://social.msdn.microsoft.com/Forums/en-US/