ส่วนขยายฟิลด์ JavaScript

ส่วนขยายฟิลด์ 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)
พารามิเตอร์ชนิดคำอธิบาย
dateFormatstringรูปแบบวันที่สไตล์ Acrobat (เช่น "mm/dd/yyyy")
dateValuestringสตริงวันที่ดิบที่ต้องจัดรูปแบบ

ตัวอย่าง

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)
ParameterTypeDescription
precisionintจำนวนตำแหน่งทศนิยม
sepStyleintรูปแบบตัวคั่นหลักพัน (0 = เครื่องหมายจุลภาค, 1 = ไม่มี, 2 = จุด)
negStyleintรูปแบบตัวเลขลบ (0 = เครื่องหมายลบ, 1 = วงเล็บ, 2 = สีแดง)
currencySymbolstringสัญลักษณ์ที่วางหน้าหรือหลัง (เช่น "$")
isPrependCurrencyboolเมื่อ true สัญลักษณ์จะวางหน้า; มิฉะนั้นวางหลัง
fieldValuestringสตริงตัวเลขที่ต้องการจัดรูปแบบ

ตัวอย่าง

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 จัดรูปแบบสตริงตัวเลขเป็นเปอร์เซ็นต์โดยคูณค่าด้วย 100 ก่อนนำไปใช้รูปแบบ มันสอดคล้องกับฟังก์ชัน JavaScript AF_Percent_Format.

ลายเซ็นของเมธอด

public string Format(
    int precision,
    int sepStyle,
    bool isPrependPercent,
    string fieldValue)
ParameterTypeDescription
precisionintจำนวนตำแหน่งทศนิยม
sepStyleintรูปแบบตัวคั่นหลักพัน
isPrependPercentboolเมื่อ true สัญลักษณ์ % จะถูกวางไว้หน้าตัวเลข; มิฉะนั้นจะวางไว้หลัง
fieldValuestringสตริงตัวเลขเพื่อจัดรูปแบบ (คูณด้วย 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}");
        }
    }
}

ดูเพิ่มเติม

 ภาษาไทย