47
3.4 Interactive Elements 71
3.4.2 Formatting Options for Text Fields
In Acrobat it is possible to specify various options for formatting the contents of a text
field, such as monetary amounts, dates, or percentages. This is implemented via custom
JavaScript code used by Acrobat. PDFlib does not directly support these formatting fea-
tures since they are not specified in the PDF reference. However, for the benefit of
PDFlib users we present some information below which will allow you to realize format-
ting options for text fields by supplying simple JavaScript code fragements with the
action option of PDF_create_field( ).
In order to apply formatting to a text field JavaScript snippets are attached to a text
field as keystroke and format actions. The JavaScript code calls some internal Acrobat
function where the parameters control details of the formatting.
The following sample creates two keystroke and format actions, and attaches them to
a form field so that the field contents will be formatted with two decimal places and the
EUR currency identifier:
keystroke_action = p.create_action("JavaScript",
"script={AFNumber_Keystroke(2, 0, 3, 0, \"EUR \", true); }");
format_action = p.create_action("JavaScript",
"script={AFNumber_Format(2, 0, 0, 0, \"EUR \", true); }");
String optlist = "font=" + font + " action={keystroke " + keystroke_action +
" format=" + format_action + "}";
p.create_field(50, 500, 250, 600, "price", "textfield", optlist);
Cookbook A full code sample can be found in the Cookbook topic interactive/form_textfield_input_
format.
In order to specify the various formats which are supported in Acrobat you must use ap-
propriate functions in the JavaScript code. Table 3.4 lists the JavaScript function names
for the keystroke and format actions for all supported formats; the function parameters
are described in Table 3.5. These functions must be used similarly to the example above.
Table 3.4 JavaScript formatting functions for text fields
format
JavaScript functions to be used for keystroke and format actions
number
AFNumber_Keystroke(nDec, sepStyle, negStyle, currStyle, strCurrency, bCurrencyPrepend)
AFNumber_Format(nDec, sepStyle, negStyle, currStyle, strCurrency, bCurrencyPrepend)
percentage
AFPercent_Keystroke(ndec, sepStyle), AFPercent_Format(ndec, sepStyle)
date
AFDate_KeystrokeEx(cFormat), AFDate_FormatEx(cFormat)
time
AFTime_Keystroke(tFormat), AFTime_FormatEx(cFormat)
special
AFSpecial_Keystroke(psf), AFSpecial_Format(psf)
Table 3.5 Parameters for the JavaScript formatting functions
parameters
explanation and possible values
nDec
Number of decimal places