JavaScript Field Extensions

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)
ParameterTypeDescription
dateFormatstringAcrobat-style date format pattern (e.g. "mm/dd/yyyy")
dateValuestringRaw 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/2026

If 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)
ParameterTypeDescription
precisionintNumber of decimal places
sepStyleintThousands-separator style (0 = comma, 1 = none, 2 = dot)
negStyleintNegative-number style (0 = minus, 1 = parentheses, 2 = red)
currencySymbolstringSymbol prepended or appended (e.g. "$")
isPrependCurrencyboolWhen true the symbol is prepended; otherwise appended
fieldValuestringNumeric 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.50

FieldNumberPercentFormatter

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)
ParameterTypeDescription
precisionintNumber of decimal places
sepStyleintThousands-separator style
isPrependPercentboolWhen true the % symbol is prepended; otherwise appended
fieldValuestringNumeric 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}");
        }
    }
}

See Also

 English