ส่วนขยายฟิลด์ JavaScript
ส่วนขยายฟิลด์ JavaScript
เนมสเปซ Aspose.Pdf.Annotations.JavascriptExtensions มีส่วนที่จัดการที่เป็นคู่มือของฟังก์ชันการจัดรูปแบบ AF_ ของ Acrobat JavaScript คลาสเหล่านี้ทำให้คุณสามารถจำลองตรรกะการแสดงฟิลด์ PDF ภายในแอปพลิเคชัน .NET ของคุณโดยไม่ต้องเรียกใช้ JavaScript.
FieldDateTimeFormatter เป็นคลาสแบบ static — เรียก Format โดยตรงบนประเภทนั้น.FieldNumberCurrencyFormatter และ FieldNumberPercentFormatter เป็นคลาสปกติ
คลาส — สร้างอินสแตนซ์แล้วเรียก Format. แต่ละเมธอดรับพารามิเตอร์เดียวกับ
พารามิเตอร์ของฟังก์ชัน Acrobat JavaScript ที่สอดคล้องและคืนค่าเป็น
สตริงที่จัดรูปแบบแล้ว.
FieldDateTimeFormatter
FieldDateTimeFormatter จัดรูปแบบสตริงวันที่/เวลาแบบดิบโดยใช้รูปแบบวันที่สไตล์ Acrobat. มันสอดคล้องกับฟังก์ชัน JavaScript AF_Date_Format.
ลายเซ็นของเมธอด
public static string Format(string dateFormat, string dateValue)| พารามิเตอร์ | ชนิด | คำอธิบาย |
|---|---|---|
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 จัดรูปแบบสตริงตัวเลขเป็นค่าเงิน. มันสอดคล้องกับฟังก์ชัน JavaScript AF_Number_Format.
ลายเซ็นของเมธอด
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.50FieldNumberPercentFormatter
FieldNumberPercentFormatter จัดรูปแบบสตริงตัวเลขเป็นเปอร์เซ็นต์โดยคูณค่าด้วย 100 ก่อนนำไปใช้รูปแบบ มันสอดคล้องกับฟังก์ชัน JavaScript AF_Percent_Format.
ลายเซ็นของเมธอด
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}");
}
}
}