Setting Rich Text Format With JavaScript
Each block of text in which the formatting is different requires a separate object inside a richValue array.
In my last post, Multiple Formats In Fields With Rich Text Formatting, I explained how to set different formats of text in the same field with Rich Text Formatting (RTF) and the RTF toolbar. Setting up RTF with JavaScript is a lot different than setting field properties without RTF.
The value and the formatting is all contained in one property, richValue. The richValue property is an array of Span objects. From the Acrobat JavaScript reference guide, the Span object is:
A generic object that represents a length of text and its associated properties in a rich text form field or annotation. A rich text value consists of an array of span objects representing the text and formatting.
There is a different Span object for each block of text when the format changes. For example, if the text of the field contains only one format, the richValue array would contain only one Span object. If the block of text changes formats seven times, the richValue array would contain eight Span objects. Even if the formats are repeated, there needs to be a separate Span object because part of that object is the richValue text element, which is the value of that block of text.
Rich Text Span Properties
Here’s a list of the most common span properties and their functions.
alignment has the same value options as form field alignment (left, center, right).
fontFamily is the font name. Bold and italic (oblique) fonts are not used because they are set by different properties: fontWeight and fontStyle respectively. Note that fontFamily is an array with 2 entries: The first entry is the font name of the font to use. The second entry is a generic family name if a match to the first entry can’t be found.
fontStyle is either normal or italic. normal is the default.
fontWeight value options are 100, 200, 300,400, 500, 600, 700, 800, or 900. Bold is 700 or higher.
strikethrough value is true or false. The default is false. If true a line is drawn through the
text.subscript value is true or false. The default is false.
superscript value is true or false. The default is false.
text is the text within the span object. Include spaces and line breaks (for multi-line fields), otherwise you will need separate spans for these.
textColor is a color array representing the RGB color. The default is black.
textSize is the point size of the text. The default is 12.
underline value is true or false. If true, the text is underlined. The default is false.
A Working Example
In the example above, all text is Helvetica font except the word quick, which is Times. quick is also the only text that is bold. The word jumped has a text size of 8, while the rest of the sentence is 12. The text color of the word fox is red and the lazy is blue. Lazy is underlined. The first word is followed by a 1 in superscript and the word dogs is followed by a 2 in subscript. The word brown is italic. The multiline field is large enough to fit the entire contents on one line, but there’s a line break after fox and another after lazy.
Building the richValue with JavaScript
Assuming the field is called Text1, and is a multiline field set for Rich Text Formatting, running the following script in the console should produce the result displayed in the image above:
var spans=new Array();
spans[0]=new Object();
spans[0].text="The";
spans[1]=new Object();
spans[1].text="1";
spans[1].superscript=true;
spans[2]=new Object();
spans[2].text=" quick";
spans[2].fontWeight=700;
spans[2].fontFamily=["Times"];
spans[3]=new Object();
spans[3].text=" brown";
spans[3].fontStyle="italic";
spans[4]=new Object();
spans[4].text=" fox \r";
spans[4].textColor=color.red;
spans[5]=new Object();
spans[5].text="jumped ";
spans[5].textSize=8;
spans[6]=new Object();
spans[6].text="over ";
spans[6].strikethrough=true;
spans[7]=new Object();
spans[7].text="the ";
spans[7].textColor=color.blue;
spans[8]=new Object();
spans[8].text="lazy\r";
spans[8].textColor=color.blue;
spans[8].underline=true;
spans[9]=new Object();
spans[9].text="dogs.";
spans[10]=new Object();
spans[10].text="2";
spans[10].subscript=true;
this.getField("Text1").richValue=spans;
It could also be entered as a custom calculation script in the field with the following change in the last line:
event.target.richValue=spans;
If any text values are derived from other field values the text string would be replaced by this.getField("theField").value. Any spaces should be added to the value. For example, suppose you wanted to replace brown with the value of another field named Color:
//this line of code
spans[3].text=" brown";
//would be replace with the following line of code
spans[3].text=" " + this.getField("Color").value;