Back to Devexpress

How to: Specify the Format of a Form Field Value with DevExpress PDF Document API

officefileapi-401764-pdf-document-api-examples-interactive-form-how-to-change-the-format-of-the-form-field-value.md

latest7.4 KB
Original Source

How to: Specify the Format of a Form Field Value with DevExpress PDF Document API

  • Sep 22, 2025
  • 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:

Create Value Format

Use the ValueFormat property to access the PdfAcroFormValueFormat object. Its methods allow you to specify the following formats:

FormatMethod
Number formatPdfAcroFormValueFormat.CreateNumberFormat
Date and Time formatPdfAcroFormValueFormat.CreateDateTimeFormat
PdfAcroFormValueFormat.CreateTimeFormat
Percent formatPdfAcroFormValueFormat.CreatePercentFormat
Special format (postal codes, telephone numbers, etc.)PdfAcroFormValueFormat.CreateSpecialFormat

The following code snippet specifies formats to different form fields:

csharp
//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";
vb
'Create a text box with a date value:
Dim 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:
Dim 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:
Dim 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:

CodeDescriptionSample Result
dDisplays the day as a number, 1 to 31 .5
ddDisplays the day as a number, 01 to 31 .05
dddDisplays the 3-letter abbreviation for the day of the week according to the current culture.Fri
ddddDisplays the full day of the week according to the current culture.Friday
mDisplays the month as a number, 1 to 12.6
mmDisplays the month as a number, 01 to 12.06
mmmDisplays the 3-letter month abbreviation according to the current culture.Nov
mmmmDisplays the full month name according to the current culture.November
yyDisplays the 2-digit year.20
yyyyDisplays 4-digit year.2020
hDisplays the hour on 12-hour clock, 1 to 124
hhDisplays the hour on 12-hour clock, 01 to 1204
HDisplays the hour on 24-hour clock, 0 to 23.4 or 20
HHDisplays the hour on 24-hour clock, 00 to 23.04 or 20
MDisplays minutes, 0 to 59.8
MMDisplays minutes, 00 to 59.08
ttDisplays time in am/pm notation.04:20 am
ssDisplays 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:

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:

csharp
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);" +
    "}";
vb
Dim 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