How to: Specify the Format of a Form Field Value
- 4 minutes to read
The following example describes how to specify the value format of the new form field. You can specify the format for the following form fields:
- PdfAcroFormTextBoxField
- PdfGraphicsAcroFormTextBoxField
- PdfAcroFormComboBoxField
- PdfGraphicsAcroFormComboBoxField
Create Value Format
Use the ValueFormat
property to access the PdfAcroFormValueFormat object. Its methods allow you to specify the following formats:
Format | Method |
---|---|
Number format | PdfAcroFormValueFormat.CreateNumberFormat |
Date and Time format | PdfAcroFormValueFormat.CreateDateTimeFormat PdfAcroFormValueFormat.CreateTimeFormat |
Percent format | PdfAcroFormValueFormat.CreatePercentFormat |
Special format (postal codes, telephone numbers, etc.) | PdfAcroFormValueFormat.CreateSpecialFormat |
The code sample below shows how to specify formats to different form fields:
//Create a text box with a date value:
var textBox = new PdfAcroFormTextBoxField("date", 1, new PdfRectangle(10, 20, 100, 50));
textBox.ValueFormat =
PdfAcroFormValueFormat.CreateDateTimeFormat("mm/dd/yyyy");
textBox.Text = "01/20/2020";
//Create a text box with a monetary value:
var numberBox = new PdfAcroFormTextBoxField("number", 1, new PdfRectangle(10, 70, 100, 100));
numberBox.ValueFormat =
PdfAcroFormValueFormat.CreateNumberFormat(2, PdfAcroFormNumberSeparatorStyle.Dot, "$", PdfAcroFormCurrencyStyle.AfterWithSpace, PdfAcroFormNegativeNumberStyle.None);
numberBox.Text = "264.88 $";
//Create a text box with a postal code value:
var specialBox = new PdfAcroFormTextBoxField("special", 1, new PdfRectangle(10, 120, 100, 150));
specialBox.ValueFormat =
PdfAcroFormValueFormat.CreateSpecialFormat(PdfAcroFormSpecialFormatType.NineDigitZipCode);
specialBox.Text = "3000856";
Date and Time Format Codes
You can use the following codes to specify date and time format:
Code | Description | Sample Result |
---|---|---|
d | Displays the day as a number, 1 to 31 . | 5 |
dd | Displays the day as a number, 01 to 31 . | 05 |
ddd | Displays the 3-letter abbreviation for the day of the week according to the current culture. | Fri |
dddd | Displays the full day of the week according to the current culture. | Friday |
m | Displays the month as a number, 1 to 12. | 6 |
mm | Displays the month as a number, 01 to 12. | 06 |
mmm | Displays the 3-letter month abbreviation according to the current culture. | Nov |
mmmm | Displays the full month name according to the current culture. | November |
yy | Displays the 2-digit year. | 20 |
yyyy | Displays 4-digit year. | 2020 |
h | Displays the hour on 12-hour clock, 1 to 12 | 4 |
hh | Displays the hour on 12-hour clock, 01 to 12 | 04 |
H | Displays the hour on 24-hour clock, 0 to 23. | 4 or 20 |
HH | Displays the hour on 24-hour clock, 00 to 23. | 04 or 20 |
M | Displays minutes, 0 to 59. | 8 |
MM | Displays minutes, 00 to 59. | 08 |
tt | Displays time in am/pm notation. | 04:20 am |
ss | Displays seconds, 01 to 59. | 05 |
Use JavaScript to Specify Value Format
The following PdfAcroFormValueFormat properties allow you to use JavaScript to define the value format:
- PdfAcroFormValueFormat.FormatScript - specifies a script that is executed before the field is formatted.
- PdfAcroFormValueFormat.KeystrokeScript - specifies a script that is executed when the user modifies a character in a form field.
- PdfAcroFormValueFormat.ValidateScript - specifies a script that is executed to recalculate the value of this field when that of another field changes.
- PdfAcroFormValueFormat.CalculateScript - specifies a script that is executed when when the field’s value is changed.
Note
The scripts are in effect for PDF viewers that support JavaScript.
The code sample below shows how create a text box and specify the value format using JavaScript:
var scriptBox =
new PdfAcroFormTextBoxField("script", 1, new PdfRectangle(10, 170, 100, 200));
scriptBox.ValueFormat = new PdfAcroFormValueFormat();
//Specify a script that allows you to enter only numbers
//in the "x-x-x" format:
scriptBox.ValueFormat.FormatScript = "if (event.value != \"\") {" +
"var value = event.value; " +
"event.value = \"\"; " +
"for(var i = 0; i<value.length - 1; i++)" +
" event.value += value.charAt(i) + \"-\";" +
"event.value += value.charAt(value.length - 1);}";
scriptBox.ValueFormat.KeystrokeScript = "var re = /^[0-9]+$/;" +
"if (event.value != \"\") {" +
" event.rc = re.test(event.value);" +
"}";