JavaScript 字段扩展
JavaScript 字段扩展
Aspose.Pdf.Annotations.JavascriptExtensions 命名空间包含 Acrobat JavaScript AF_ 格式化函数的托管对应类。这些类让您能够在 .NET 应用程序中重现 PDF 字段显示逻辑,而无需执行 JavaScript。
FieldDateTimeFormatter 是一个静态类 — 直接在类型上调用 Format。FieldNumberCurrencyFormatter 和 FieldNumberPercentFormatter 是普通
类 — 创建实例后,再调用 Format。每个方法接受相同
的参数与相应的 Acrobat JavaScript 函数,并返回格式化的字符串。
字段日期时间格式化器
FieldDateTimeFormatter 使用 Acrobat 风格的日期格式模式格式化原始日期/时间字符串。它对应于 AF_Date_Format JavaScript 函数。
方法签名
public static string Format(string dateFormat, string dateValue)| Parameter | Type | Description |
|---|---|---|
dateFormat | string | Acrobat 样式的日期格式模式(例如 "mm/dd/yyyy") |
dateValue | string | 要格式化的原始日期字符串 |
示例
using Aspose.Pdf.Annotations.JavascriptExtensions;
string result = FieldDateTimeFormatter.Format("mm/dd/yyyy", "2026-06-11");
Console.WriteLine(result); // 06/11/2026如果无法解析输入值,格式化程序将返回原始值不变。
FieldNumberCurrencyFormatter
FieldNumberCurrencyFormatter 将数字字符串格式化为货币值。它对应于 AF_Number_Format JavaScript 函数。
方法签名
public string Format(
int precision,
int sepStyle,
int negStyle,
string currencySymbol,
bool isPrependCurrency,
string fieldValue)| Parameter | Type | Description |
|---|---|---|
precision | int | 小数位数 |
sepStyle | int | 千位分隔符样式(0 = 逗号,1 = 无,2 = 点) |
negStyle | int | 负数样式(0 = 负号,1 = 括号,2 = 红色) |
currencySymbol | string | 前置或后置符号(例如 "$") |
isPrependCurrency | bool | 当 true 时符号前置;否则后置 |
fieldValue | string | 要格式化的数字字符串 |
示例
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 将数值字符串格式化为百分比,在应用格式之前将值乘以 100。它对应 AF_Percent_Format JavaScript 函数。
方法签名
public string Format(
int precision,
int sepStyle,
bool isPrependPercent,
string fieldValue)| Parameter | Type | Description |
|---|---|---|
precision | int | 小数位数 |
sepStyle | int | 千位分隔符样式 |
isPrependPercent | bool | 当 true 时,% 符号前置;否则后置 |
fieldValue | string | 要格式化的数字字符串(乘以 100) |
示例
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%在表单处理循环中使用格式化程序
以下模式将货币格式应用于 PDF 表单中的所有文本字段值:
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}");
}
}
}