Crionics LOGO

TUTORIALS

 

 Creating and changing
RichText form fields

As easy as 1, 2, 3...

 

Introduction

Beginning with PDF 1.5, the text contents of variable text form fields markup annotations can include formatting (style) information. These rich text strings are fully-formed XHTML 1.0 documents, augmented
with a restricted set of CSS2 style attributes.

We will explain in this section the grammar to compose a valid rich text field value.
First, let's see what a rich text value looks like:

<?xml version="1.0"?>
<body xmlns="http://www.w3.org/1999/xtml" xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/"
xfa:contentType="text/html" xfa:APIVersion="Acrobat:6.0.0" xfa:spec="2.0.2"
>
<span style="text-align:left">
<b><i>
Here is some bold italic text
</i></b>
</span>

<p style= "font-size:16pt;font-style:italic">
This text uses default text state parameters but changes the font size to 16 italic.
</p>
</body>

The <body> element is the root element; it requires certain attributes to be defined in order to give the parser information about the data it holds.
Other elements (<p> and <span>) contain enclosed text that may take style attributes - which are listed in one of the tables below - and are CSS inline style property declarations that take the form name:value ... with each declaration separated by a semicolon.

Element Description
<body>
The element at the root of the XML document. We will see in section 3 that it requires attributes.
<p>
Encloses text that is interpreted as a paragraph. It may take style attributes - see below.
<i>
Encloses text that is displayed in an italic font.
<b>
Encloses text that is displayed in a bold font.
<span>
Groups text solely for the purpose of applying styles - see below.
<br>
Starts a new line. Applies to multi-line fields.

 

CSS2 style attributes used in rich text strings
Attribute
Value
Description
text-align
keyword
Horizontal alignment. Possible values: left, right, center.
vertical-align
decimal
An amount by which to adjust the baseline of the enclosed text. A positive value indicates a superscript; a negative value indicates a subscript. The value is of the form <decimal number>pt, optionally preceded by a sign, and followed by “pt”. Examples: -3pt, 4pt.

font-size
decimal
The font size of the enclosed text. The value is of the form:
<decimal number>pt.
font-style
keyword
Specifies whether the enclosed text should be displayed using a normal or italic (oblique) font. Possible values: normal, italic.
font-weight
keyword
The weight of the font for the enclosed text. Possible values: normal, bold, 100, 200, 300, 400, 500, 600, 700, 800, 900.
Note: normal is equivalent to 400, and bold is equivalent to 700.
font
list
A shorthand CSS font property of the form
font:<font-style> <font-weight> <font-size> <font-family>.
font-family
list
A font name or list of font names to be used to display the enclosed text. (If a list is provided, the first one containing glyphs for the specified text is used.)
color
RGB value
The color of the enclosed text. The value is an RGB value specified in the sRGB color space: <http://www.srgb.com>). It can be in one of two forms:
#rrggbb with a 2-digit hexadecimal value for each component.
rgb(rrr,ggg,bbb) with a decimal value for each component.
text-decoration
keyword
One of the following keywords:
underline: The enclosed text should be underlined.
line-through: The enclosed text should have a line drawn through it.
font-stretch
keyword
Specifies a normal, condensed or extended face from a font family. Supported values from narrowest to widest are: ultra-condensed, extra-condensed, condensed, semi-condensed, normal, semi-expanded, expanded, extra-expanded, and ultra-expanded.

Creating a RichText field

In the following example, we will create a document with its RichText form field. For more details about form field creation please refer to this tutorial.

// Create a document in memory and its first page:
PdfDocument doc = new PdfDocument();
Page.createInstance(doc);

// Create a multi-line, RichText form field:
Rectangle rect = new Rectangle(100, 100, 100, 100);
TextField field = TextField.createInstance(doc, "RichTextField", 1, rect);
field.setMultiline(true);
field.setRichText(true);

Once a form field has its RichText flag activated, it can only accept values which comply with the XML description given in section #1.

Similarly, the getValue() method will return the associated XML description (if any).

 

3Changing RichText field values

 

Here come the easy part - setting the value. The most difficult part is to compose the document itself. Please refer to section #1 for a detailed decription of the XML grammar. One important thing to mention is the attributes on the body tag: they are mandatory. Failure to use these attributes will cause the field to be interpreted as a standard text field.

// Now set its value using a RichText XML:
field.setValue("<?xml version=\"1.0\"?><body xfa:APIVersion=\"Acroform:2.4.5292.0\" xfa:spec=\"2.1\" xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\"><p><b>RichText Content</b></p><p style=\"color:#009900\">Style 1</p><p><b><i>Style 2</i></b></p><p style=\"text-align:right;text-decoration:underline;color:#FF0000\">Style 3</p></body>");

// Save and close the modified document:
doc.saveAs("MyDocument.pdf");
doc.close();

The generated file should look like this screenshot:

result

 


© 2000-2007 Crionics Inc. Report issues with this page