如何在 .NET 中使用 JavaScript 扩展

如何在 .NET 中使用 JavaScript 扩展

Aspose.PDF FOSS for .NET 包含通过 FieldDateTimeFormatter、FieldNumberCurrencyFormatter 和 FieldNumberPercentFormatter 类实现的 PDF JavaScript AF_Date_Format、AF_Number_Format 和 AF_Percent_Format 功能的服务器端实现。

dotnet add package Aspose.PDF.FOSS

使用这些类在服务器上格式化 AcroForm 字段值,无需 JavaScript 引擎。

分步指南

步骤 1:安装包

将 Aspose.PDF FOSS 添加到你的项目中:

dotnet add package Aspose.PDF.FOSS

步骤 2:导入所需类

using Aspose.Pdf.Annotations.JavascriptExtensions;

步骤 3:格式化日期/时间字段值

FieldDateTimeFormatter.Format(dateFormat, dateValue) 根据 Acrobat 样式的日期格式模式转换原始日期字符串。这对应于 PDF JavaScript AF_Date_Format 函数。

string formatted = FieldDateTimeFormatter.Format("mm/dd/yyyy", "2026-06-11");

参数 dateFormat 使用 Acrobat 日期格式标记(例如,mm、dd、yyyy、HH、MM)。当输入的 dateValue 无法解析时,方法返回空字符串。


步骤 4:将数值字段格式化为货币

FieldNumberCurrencyFormatter.Format(precision, sepStyle, negStyle, currencySymbol, isPrependCurrency, fieldValue) 将数值字符串格式化为货币值。这对应于 PDF JavaScript AF_Number_Format 函数。

var currencyFormatter = new FieldNumberCurrencyFormatter();
string formatted = currencyFormatter.Format(
    precision: 2,
    sepStyle: 0,
    negStyle: 0,
    currencySymbol: "$",
    isPrependCurrency: true,
    fieldValue: "1234.5");

参数:

  • precision - 小数位数
  • sepStyle - 千位分隔符样式(0 = 逗号,1 = 句点,2 = 无)
  • negStyle - 负数格式(0 = 括号,1 = 负号前缀)
  • currencySymbol - 货币符号字符串
  • isPrependCurrency - true 表示将符号放在数字前
  • fieldValue - 要格式化的原始数字字符串

步骤 5:将数值字段格式化为百分比

FieldNumberPercentFormatter.Format(precision, sepStyle, isPrependPercent, fieldValue) 将数值字符串格式化为百分比,首先将原始值乘以 100。这对应于 PDF JavaScript AF_Percent_Format 函数。

var percentFormatter = new FieldNumberPercentFormatter();
string formatted = percentFormatter.Format(
    precision: 1,
    sepStyle: 0,
    isPrependPercent: false,
    fieldValue: "0.875");

参数:

  • precision - 小数点后的小数位数
  • sepStyle - 千位分隔符样式(0 = 逗号,1 = 句点,2 = 无)
  • isPrependPercent - true 表示在前面添加 % 符号,false 表示在后面添加
  • fieldValue - 原始数值字符串(格式化器会乘以 100)

常见问题及解决方案

FieldDateTimeFormatter 返回空字符串 当…时会出现此情况 dateValue 参数无法被日期模式解析 dateFormat. Acrobat 日期标记与 .NET DateTime 格式字符串不同。

FieldNumberCurrencyFormatter 产生错误的小数分隔符 该 sepStyle 参数控制千位分隔符。若要使用句点作为千位分隔符,请传入 sepStyle: 1.

FieldNumberPercentFormatter 输出大了100倍 格式化程序将输入值乘以 100。如果你的 fieldValue 已经表示为百分比(例如,“87.5”而不是 “0.875”),在调用 Format 之前先除以 100。

安装包后未找到命名空间 确保您针对的是 net8.0 或更高版本。JavascriptExtensions 命名空间仅在 Aspose.Pdf.Foss 包中可用。

NullReferenceException 在空的 fieldValue 上 将 null 或空字符串作为 fieldValue 会导致解析异常。在调用 Format 之前请验证输入。

常见问题

“corresponds to AF_Date_Format” 是什么意思?

PDF AcroForm JavaScript 使用 AF_Date_Format 作为日期字段的内置格式化函数。FieldDateTimeFormatter 在 .NET 托管代码中复制了相同的格式化逻辑,这样您可以在不执行 JavaScript 的情况下使用相同的字段格式字符串。

我可以在 PDF 文档上下文之外使用这些格式化程序吗?

是的。FieldDateTimeFormatter、FieldNumberCurrencyFormatter和FieldNumberPercentFormatter是普通类,不依赖于Document实例。您可以在任何 .NET 8+ 环境中将它们用作实用格式化器。

FieldNumberCurrencyFormatter 和 FieldNumberPercentFormatter 有什么区别?

FieldNumberCurrencyFormatter 将数字格式化为货币金额,不进行乘法运算。FieldNumberPercentFormatter 将输入乘以 100,并在末尾或开头添加百分号。

有没有办法链式使用多个格式化器?

没有内置的链式功能。请在代码中顺序应用格式化器。

使用这些类是否需要完整的 PDF 文档?

不需要。格式化器在普通字符串上工作并返回格式化后的字符串。它们不需要打开的 Document 实例。

另请参阅

 中文