Creating and changing

RichText form fields

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:

>

<body xmlns="http:/www.w3.org/1999/xtml" xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/"

xfa:contentType="texthtml" xfa:APIVersion="Acrobat:6.0.0" xfa:spec="2.0.2" >

<b><i>

<i>

<span>

This text uses default text state parameters but changes the font size to 16 italic.

<p>

<body>

>

The

Other elements (<p>

>

Element Description

>

>

The element at the root of the XML document. We will see in section 3 that it requires attributes.

>

>

Encloses text that is interpreted as a paragraph. It may take style attributes - see below.

>

>

Encloses text that is displayed in an italic font.

>

>

Encloses text that is displayed in a bold font.

>

>

Groups text solely for the purpose of applying styles - see below.

>

>

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 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

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.

>

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.

![][2]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);

field.setMultiline(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).

>

>

![3][4]Changing 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/\">RichText Content

Style 1

Style 2

Style 3

");

>

// Save and close the modified document:

doc.saveAs("MyDocument.pdf");

>

The generated file should look like this screenshot:

>

!result

>

>