C# 設定或驗證 PDF中的文字域格式

語言: CN / TW / HK

概述

PDF中的文字域可以通過設定不同格式,用於顯示數字、貨幣、日期、時間、郵政編碼、電話號碼和社保號等等。Adobe Acrobat提供了許多固定的JavaScripts用來設定和驗證文字域的格式,如:AFNumber_Format(2, 0, 0, 0, "$", true)和AFNumber_Keystroke(2, 0, 0, 0, "$", true)。Format字尾的script是用來設定文字域顯示的格式,而Keystroke字尾的script是用來驗證輸入內容。

Spire.PDF for .NET提供了相應的方法來設定和驗證文字域格式。下面的表格羅列了常用的格式和Spire.PDF中對應的方法,可參考使用:

描述

示例

JavaScript

Spire.PDF 提供的方法

日期

01/05/2022

AFDate_FormatEx("mm/dd/yyyy"); AFDate_KeystrokeEx("mm/dd/yyyy");

GetDateFormatString("mm/dd/yyyy"); GetDateKeystrokeString("mm/dd/yyyy");

郵政編碼

12345

AFSpecial_Format(0); AFSpecial_Keystroke(0);

GetSpecialFormatString(0); GetSpecialKeystrokeString(0);

郵政編碼+4

12345-1234

AFSpecial_Format(1); AFSpecial_Keystroke(1);

GetSpecialFormatString(1); GetSpecialKeystrokeString(1);

電話號碼

(123)456-7890

AFSpecial_Format(2); AFSpecial_Keystroke(2);

GetSpecialFormatString(2); GetSpecialKeystrokeString(2);

貨幣

$12345.00 -$12345.00

AFNumber_Format(2,0,0,0,"$",true); AFNumber_Keystroke(2,0,0,0,"$",true);

GetNumberFormatString(2,0,0,0,"$",true); GetNumberKeystrokeString(2,0,0,0,"$",true);

驗證

1.5≤輸入值≤5.5

AFRange_Validate(true,1.5,true,5.5);

GetRangeValidateString(true,1.5,true,5.5);

引入dll

1. 通過 NuGet 安裝dll(2種方法)

1.1 可以在Visual Studio中開啟“解決方案資源管理器”,滑鼠右鍵點選“引用”,“管理NuGet包”,然後搜尋“Spire.PDF”,點選“安裝”。

1.2 將以下內容複製到PM控制檯安裝。

Install-Package Spire.PDF -Version 7.12.1

2. 手動新增dll引用

可通過手動 下載包 ,然後解壓,找到BIN資料夾下的Spire.Pdf.dll。在Visual Studio中開啟“解決方案資源管理器”,滑鼠右鍵點選“引用”,“新增引用”將本地路徑BIN資料夾下的dll檔案新增引用至程式。

程式碼(C#/VB.NET)

C#

using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Fields;
using System.Drawing;

namespace SetTextFormatInTextboxField
{
    class Program
    {
        static void Main(string[] args)
        {
            //新建PDF文件,並新增空白頁
            PdfDocument pdf = new PdfDocument();
            PdfPageBase page = pdf.Pages.Add();

            //定義座標變數
            float X = 10;
            float Y = 10;
            float width = 100;
            float height = 20;

            //例項化一個文字域物件,並設定它的位置和邊框樣式
            PdfTextBoxField textbox = new PdfTextBoxField(page, "Number-TextBox");           
            textbox.Bounds = new RectangleF(X, Y, width, height);
            textbox.BorderWidth = 0.75f;
            textbox.BorderStyle = PdfBorderStyle.Solid;

            //給文字域的鍵盤擊鍵事件設定一個JavaScript動作用於驗證輸入內容是否符合要求
            string js = PdfJavaScript.GetNumberKeystrokeString(2, 0, 0, 0, "$", true);
            PdfJavaScriptAction jsAction = new PdfJavaScriptAction(js);
            textbox.Actions.KeyPressed = jsAction;

            //設定文字域內容顯示為數字貨幣
            js = PdfJavaScript.GetNumberFormatString(2, 0, 0, 0, "$", true);
            jsAction = new PdfJavaScriptAction(js);
            textbox.Actions.Format = jsAction;

            //新增文字域到PDF中,並儲存文件
            pdf.Form.Fields.Add(textbox);



            //新增文字框,設定文字內容顯示為日期格式
            PdfTextBoxField textbox1 = new PdfTextBoxField(page, "DateFormat-TextBox");
            textbox1.Bounds = new RectangleF(X+200, Y, width, height);
            textbox1.BorderWidth = 0.75f;
            textbox1.BorderStyle = PdfBorderStyle.Solid;
            string js1 = PdfJavaScript.GetDateKeystrokeString("mm/dd/yyyy");
            PdfJavaScriptAction jsAction1 = new PdfJavaScriptAction(js1);
            textbox1.Actions.KeyPressed = jsAction1;
            js1 = PdfJavaScript.GetDateFormatString("mm/dd/yyyy");
            jsAction1 = new PdfJavaScriptAction(js1);
            textbox1.Actions.Format = jsAction1;
            pdf.Form.Fields.Add(textbox1);


            //新增文字框,設定文字內容顯示為郵政編碼格式
            PdfTextBoxField textbox2 = new PdfTextBoxField(page, "SpecialFormat0-1-TextBox");
            textbox2.Bounds = new RectangleF(X + 400, Y, width, height);
            textbox2.BorderWidth = 0.75f;
            textbox2.BorderStyle = PdfBorderStyle.Solid;
            //string js2 = PdfJavaScript.GetSpecialKeystrokeString(0);
            string js2 = PdfJavaScript.GetSpecialKeystrokeString(1);

            PdfJavaScriptAction jsAction2 = new PdfJavaScriptAction(js2);
            textbox2.Actions.KeyPressed = jsAction2;
            //js2 = PdfJavaScript.GetSpecialFormatString(0);
            js2 = PdfJavaScript.GetSpecialFormatString(1);
            jsAction2 = new PdfJavaScriptAction(js2);
            textbox2.Actions.Format = jsAction2;
            pdf.Form.Fields.Add(textbox2);


            //新增文字框,設定文字內容顯示為百分數
            PdfTextBoxField textbox3 = new PdfTextBoxField(page, "SpecialFormat2-TextBox");
            textbox3.Bounds = new RectangleF(X, Y+50, width, height);
            textbox3.BorderWidth = 0.75f;
            textbox3.BorderStyle = PdfBorderStyle.Solid;
            string js3 = PdfJavaScript.GetPercentKeystrokeString(1,0);
            PdfJavaScriptAction jsAction3 = new PdfJavaScriptAction(js3);
            textbox3.Actions.KeyPressed = jsAction3;
            js3 = PdfJavaScript.GetPercentFormatString(1, 0);
            jsAction3 = new PdfJavaScriptAction(js3);
            textbox3.Actions.Format = jsAction3;
            pdf.Form.Fields.Add(textbox3);

            //新增文字框,設定資料驗證
            PdfTextBoxField textbox4 = new PdfTextBoxField(page, "RangeValidate-TextBox");
            textbox4.Bounds = new RectangleF(X+200, Y + 50, width, height);
            textbox4.BorderWidth = 0.75f;
            textbox4.BorderStyle = PdfBorderStyle.Solid;
            string js4 = PdfJavaScript.GetRangeValidateString(true, -18, true, 18);
            PdfJavaScriptAction jsAction4 = new PdfJavaScriptAction(js4);
            textbox4.Actions.Format = jsAction4;
            pdf.Form.Fields.Add(textbox4);

            //儲存文件
            pdf.SaveToFile("FormatField.pdf", FileFormat.PDF);
        }
    }
}

VB.NET

Imports Spire.Pdf
Imports Spire.Pdf.Actions
Imports Spire.Pdf.Fields
Imports System.Drawing

Namespace SetTextFormatInTextboxField
    Class Program
        Private Shared Sub Main(args As String())
            '新建PDF文件,並新增空白頁
            Dim pdf As New PdfDocument()
            Dim page As PdfPageBase = pdf.Pages.Add()

            '定義座標變數
            Dim X As Single = 10
            Dim Y As Single = 10
            Dim width As Single = 100
            Dim height As Single = 20

            '例項化一個文字域物件,並設定它的位置和邊框樣式
            Dim textbox As New PdfTextBoxField(page, "Number-TextBox")
            textbox.Bounds = New RectangleF(X, Y, width, height)
            textbox.BorderWidth = 0.75F
            textbox.BorderStyle = PdfBorderStyle.Solid

            '給文字域的鍵盤擊鍵事件設定一個JavaScript動作用於驗證輸入內容是否符合要求
            Dim js As String = PdfJavaScript.GetNumberKeystrokeString(2, 0, 0, 0, "$", True)
            Dim jsAction As New PdfJavaScriptAction(js)
            textbox.Actions.KeyPressed = jsAction

            '設定文字域內容顯示為數字貨幣
            js = PdfJavaScript.GetNumberFormatString(2, 0, 0, 0, "$", True)
            jsAction = New PdfJavaScriptAction(js)
            textbox.Actions.Format = jsAction

            '新增文字域到PDF中,並儲存文件
            pdf.Form.Fields.Add(textbox)


            '新增文字框,設定文字內容顯示為日期格式
            Dim textbox1 As New PdfTextBoxField(page, "DateFormat-TextBox")
            textbox1.Bounds = New RectangleF(X + 200, Y, width, height)
            textbox1.BorderWidth = 0.75F
            textbox1.BorderStyle = PdfBorderStyle.Solid
            Dim js1 As String = PdfJavaScript.GetDateKeystrokeString("mm/dd/yyyy")
            Dim jsAction1 As New PdfJavaScriptAction(js1)
            textbox1.Actions.KeyPressed = jsAction1
            js1 = PdfJavaScript.GetDateFormatString("mm/dd/yyyy")
            jsAction1 = New PdfJavaScriptAction(js1)
            textbox1.Actions.Format = jsAction1
            pdf.Form.Fields.Add(textbox1)


            '新增文字框,設定文字內容顯示為郵政編碼格式
            Dim textbox2 As New PdfTextBoxField(page, "SpecialFormat0-1-TextBox")
            textbox2.Bounds = New RectangleF(X + 400, Y, width, height)
            textbox2.BorderWidth = 0.75F
            textbox2.BorderStyle = PdfBorderStyle.Solid
            'string js2 = PdfJavaScript.GetSpecialKeystrokeString(0);
            Dim js2 As String = PdfJavaScript.GetSpecialKeystrokeString(1)

            Dim jsAction2 As New PdfJavaScriptAction(js2)
            textbox2.Actions.KeyPressed = jsAction2
            'js2 = PdfJavaScript.GetSpecialFormatString(0);
            js2 = PdfJavaScript.GetSpecialFormatString(1)
            jsAction2 = New PdfJavaScriptAction(js2)
            textbox2.Actions.Format = jsAction2
            pdf.Form.Fields.Add(textbox2)


            '新增文字框,設定文字內容顯示為百分數
            Dim textbox3 As New PdfTextBoxField(page, "SpecialFormat2-TextBox")
            textbox3.Bounds = New RectangleF(X, Y + 50, width, height)
            textbox3.BorderWidth = 0.75F
            textbox3.BorderStyle = PdfBorderStyle.Solid
            Dim js3 As String = PdfJavaScript.GetPercentKeystrokeString(1, 0)
            Dim jsAction3 As New PdfJavaScriptAction(js3)
            textbox3.Actions.KeyPressed = jsAction3
            js3 = PdfJavaScript.GetPercentFormatString(1, 0)
            jsAction3 = New PdfJavaScriptAction(js3)
            textbox3.Actions.Format = jsAction3
            pdf.Form.Fields.Add(textbox3)

            '新增文字框,設定資料驗證
            Dim textbox4 As New PdfTextBoxField(page, "RangeValidate-TextBox")
            textbox4.Bounds = New RectangleF(X + 200, Y + 50, width, height)
            textbox4.BorderWidth = 0.75F
            textbox4.BorderStyle = PdfBorderStyle.Solid
            Dim js4 As String = PdfJavaScript.GetRangeValidateString(True, -18, True, 18)
            Dim jsAction4 As New PdfJavaScriptAction(js4)
            textbox4.Actions.Format = jsAction4
            pdf.Form.Fields.Add(textbox4)

            '儲存文件
            pdf.SaveToFile("FormatField.pdf", FileFormat.PDF)
        End Sub
    End Class
End Namespace

設定後的文字框域填寫效果如圖:

—End—