C#設(shè)置或驗(yàn)證PDF文本域格式的方法詳解
概述
PDF中的文本域可以通過設(shè)置不同格式,用于顯示數(shù)字、貨幣、日期、時(shí)間、郵政編碼、電話號碼和社保號等等。Adobe Acrobat提供了許多固定的JavaScripts用來設(shè)置和驗(yàn)證文本域的格式,如:AFNumber_Format(2, 0, 0, 0, "$", true)和AFNumber_Keystroke(2, 0, 0, 0, "$", true)。Format后綴的script是用來設(shè)置文本域顯示的格式,而Keystroke后綴的script是用來驗(yàn)證輸入內(nèi)容。
Spire.PDF for .NET提供了相應(yīng)的方法來設(shè)置和驗(yàn)證文本域格式。下面的表格羅列了常用的格式和Spire.PDF中對應(yīng)的方法,可參考使用:
引入dll
1.通過NuGet安裝dll(2種方法)
1.1 可以在Visual Studio中打開“解決方案資源管理器”,鼠標(biāo)右鍵點(diǎn)擊“引用”,“管理NuGet包”,然后搜索“Spire.PDF”,點(diǎn)擊“安裝”。
1.2 將以下內(nèi)容復(fù)制到PM控制臺安裝。
Install-Package Spire.PDF -Version 7.12.1
2.手動添加dll引用
可通過手動下載包,然后解壓,找到BIN文件夾下的Spire.Pdf.dll。在Visual Studio中打開“解決方案資源管理器”,鼠標(biāo)右鍵點(diǎn)擊“引用”,“添加引用”將本地路徑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(); //定義坐標(biāo)變量 float X = 10; float Y = 10; float width = 100; float height = 20; //實(shí)例化一個(gè)文本域?qū)ο?,并設(shè)置它的位置和邊框樣式 PdfTextBoxField textbox = new PdfTextBoxField(page, "Number-TextBox"); textbox.Bounds = new RectangleF(X, Y, width, height); textbox.BorderWidth = 0.75f; textbox.BorderStyle = PdfBorderStyle.Solid; //給文本域的鍵盤擊鍵事件設(shè)置一個(gè)JavaScript動作用于驗(yàn)證輸入內(nèi)容是否符合要求 string js = PdfJavaScript.GetNumberKeystrokeString(2, 0, 0, 0, "$", true); PdfJavaScriptAction jsAction = new PdfJavaScriptAction(js); textbox.Actions.KeyPressed = jsAction; //設(shè)置文本域內(nèi)容顯示為數(shù)字貨幣 js = PdfJavaScript.GetNumberFormatString(2, 0, 0, 0, "$", true); jsAction = new PdfJavaScriptAction(js); textbox.Actions.Format = jsAction; //添加文本域到PDF中,并保存文檔 pdf.Form.Fields.Add(textbox); //添加文本框,設(shè)置文本內(nèi)容顯示為日期格式 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); //添加文本框,設(shè)置文本內(nèi)容顯示為郵政編碼格式 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); //添加文本框,設(shè)置文本內(nèi)容顯示為百分?jǐn)?shù) 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); //添加文本框,設(shè)置數(shù)據(jù)驗(yàn)證 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() '定義坐標(biāo)變量 Dim X As Single = 10 Dim Y As Single = 10 Dim width As Single = 100 Dim height As Single = 20 '實(shí)例化一個(gè)文本域?qū)ο?,并設(shè)置它的位置和邊框樣式 Dim textbox As New PdfTextBoxField(page, "Number-TextBox") textbox.Bounds = New RectangleF(X, Y, width, height) textbox.BorderWidth = 0.75F textbox.BorderStyle = PdfBorderStyle.Solid '給文本域的鍵盤擊鍵事件設(shè)置一個(gè)JavaScript動作用于驗(yàn)證輸入內(nèi)容是否符合要求 Dim js As String = PdfJavaScript.GetNumberKeystrokeString(2, 0, 0, 0, "$", True) Dim jsAction As New PdfJavaScriptAction(js) textbox.Actions.KeyPressed = jsAction '設(shè)置文本域內(nèi)容顯示為數(shù)字貨幣 js = PdfJavaScript.GetNumberFormatString(2, 0, 0, 0, "$", True) jsAction = New PdfJavaScriptAction(js) textbox.Actions.Format = jsAction '添加文本域到PDF中,并保存文檔 pdf.Form.Fields.Add(textbox) '添加文本框,設(shè)置文本內(nèi)容顯示為日期格式 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) '添加文本框,設(shè)置文本內(nèi)容顯示為郵政編碼格式 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) '添加文本框,設(shè)置文本內(nèi)容顯示為百分?jǐn)?shù) 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) '添加文本框,設(shè)置數(shù)據(jù)驗(yàn)證 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
設(shè)置后的文本框域填寫效果如圖:
以上就是C#設(shè)置或驗(yàn)證PDF文本域格式的方法詳解的詳細(xì)內(nèi)容,更多關(guān)于C#設(shè)置 驗(yàn)證PDF文本域格式的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
實(shí)例詳解C#實(shí)現(xiàn)http不同方法的請求
本篇文章給大家分享了C#實(shí)現(xiàn)http不同方法的請求的相關(guān)知識點(diǎn)以及實(shí)例代碼,有需要的朋友參考下。2018-07-07C#實(shí)現(xiàn)漂亮的數(shù)字時(shí)鐘效果
這篇文章主要介紹了C#實(shí)現(xiàn)漂亮的數(shù)字時(shí)鐘效果,涉及時(shí)間函數(shù)的應(yīng)用及繪圖的方法,需要的朋友可以參考下2014-10-10C#實(shí)現(xiàn)根據(jù)數(shù)字序號輸出星期幾的簡單實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)根據(jù)數(shù)字序號輸出星期幾的簡單實(shí)例,代碼簡潔實(shí)用,也有助于初學(xué)者更好的理解C#的switch和if語句的流程控制,需要的朋友可以參考下2014-07-07C#統(tǒng)計(jì)字符串中數(shù)字個(gè)數(shù)的方法
這篇文章主要介紹了C#統(tǒng)計(jì)字符串中數(shù)字個(gè)數(shù)的方法,涉及C#遍歷字符串并判斷數(shù)字的技巧,需要的朋友可以參考下2015-06-06如何在Mac系統(tǒng)使用Visual Studio Code運(yùn)行Python
這篇文章主要介紹了Mac使用Visual Studio Code運(yùn)行Python環(huán)境的方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04SMTP客戶端未通過身份驗(yàn)證等多種錯(cuò)誤解決方案分享
這篇文章主要介紹了SMTP服務(wù)器要求安全連接或客戶端未通過身份驗(yàn)證的多種解決方案,感興趣的小伙伴們可以參考一下2016-05-05C# 實(shí)現(xiàn)對PPT文檔加密、解密及重置密碼的操作方法
這篇文章主要介紹了C# 實(shí)現(xiàn)對PPT文檔加密、解密及重置密碼的操作方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-11-11C#WinForm實(shí)現(xiàn)多語言切換的示例
本文主要介紹了C#WinForm實(shí)現(xiàn)多語言切換的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01