JavaScript Field Extensions
JavaScript Field Extensions
The Aspose.Pdf.Annotations.JavascriptExtensions namespace contains managed
counterparts to the Acrobat JavaScript AF_ formatting functions. These classes
let you reproduce PDF field-display logic inside your .NET application without
executing JavaScript.
FieldDateTimeFormatter is a static class — call Format directly on the type.
FieldNumberCurrencyFormatter and FieldNumberPercentFormatter are regular
classes — create an instance, then call Format. Each method accepts the same
parameters as the corresponding Acrobat JavaScript function and returns a
formatted string.
FieldDateTimeFormatter
FieldDateTimeFormatter formats a raw date/time string using an Acrobat-style
date format pattern. It corresponds to the AF_Date_Format JavaScript function.
Method signature
public static string Format(string dateFormat, string dateValue)| Parameter | Type | Description |
|---|---|---|
dateFormat | string | Acrobat-style date format pattern (e.g. "mm/dd/yyyy") |
dateValue | string | Raw date string to format |
Example
using Aspose.Pdf.Annotations.JavascriptExtensions;
string result = FieldDateTimeFormatter.Format("mm/dd/yyyy", "2026-06-11");
Console.WriteLine(result); // 06/11/2026If the input value cannot be parsed, the formatter returns the original value unchanged.
FieldNumberCurrencyFormatter
FieldNumberCurrencyFormatter formats a numeric string as a currency value.
It corresponds to the AF_Number_Format JavaScript function.
Method signature
public string Format(
int precision,
int sepStyle,
int negStyle,
string currencySymbol,
bool isPrependCurrency,
string fieldValue)| Parameter | Type | Description |
|---|---|---|
precision | int | Number of decimal places |
sepStyle | int | Thousands-separator style (0 = comma, 1 = none, 2 = dot) |
negStyle | int | Negative-number style (0 = minus, 1 = parentheses, 2 = red) |
currencySymbol | string | Symbol prepended or appended (e.g. "$") |
isPrependCurrency | bool | When true the symbol is prepended; otherwise appended |
fieldValue | string | Numeric string to format |
Example
using Aspose.Pdf.Annotations.JavascriptExtensions;
var formatter = new FieldNumberCurrencyFormatter();
string result = formatter.Format(
precision: 2,
sepStyle: 0,
negStyle: 0,
currencySymbol: "$",
isPrependCurrency: true,
fieldValue: "1234.5");
Console.WriteLine(result); // $1,234.50FieldNumberPercentFormatter
FieldNumberPercentFormatter formats a numeric string as a percentage,
multiplying the value by 100 before applying the format. It corresponds to
the AF_Percent_Format JavaScript function.
Method signature
public string Format(
int precision,
int sepStyle,
bool isPrependPercent,
string fieldValue)| Parameter | Type | Description |
|---|---|---|
precision | int | Number of decimal places |
sepStyle | int | Thousands-separator style |
isPrependPercent | bool | When true the % symbol is prepended; otherwise appended |
fieldValue | string | Numeric string to format (multiplied by 100) |
Example
using Aspose.Pdf.Annotations.JavascriptExtensions;
var formatter = new FieldNumberPercentFormatter();
string result = formatter.Format(
precision: 1,
sepStyle: 0,
isPrependPercent: false,
fieldValue: "0.1234");
Console.WriteLine(result); // 12.3%Using formatters in a form-processing loop
The following pattern applies currency formatting to all text field values in a PDF form:
using Aspose.Pdf;
using Aspose.Pdf.Annotations.JavascriptExtensions;
byte[] data = File.ReadAllBytes("form.pdf");
using var doc = Document.Open(data);
if (doc.Form is not null)
{
var currencyFormatter = new FieldNumberCurrencyFormatter();
foreach (var field in doc.Form.Fields)
{
if (field.Value is not null)
{
string formatted = currencyFormatter.Format(
precision: 2,
sepStyle: 0,
negStyle: 0,
currencySymbol: "$",
isPrependCurrency: true,
fieldValue: field.Value);
Console.WriteLine($"{field.Name}: {formatted}");
}
}
}