C# 用JavaScript设置或验证 PDF中的文本域格式

 时间:2024-11-13 05:05:07

PDF中的文本域可以通过设置不同格式,用于显示数字、货币、日期、时间、邮政编码、电话号码和社保号等等。下面,通过后端C#程序代码展示如何来设置和验证PDF中的文本域格式。

工具/原料

Visual Studio 2017

Spire.PDF for .NET

PDF文档

引入dll

1、打开Visual Studio,新建一个控制台应用程序。打开“解决方案资源管理器”,鼠标右键点击“引用”,“管理NuGet包”:

C# 用JavaScript设置或验证 PDF中的文本域格式

2、选择“浏览”-在搜索框中输入“Spire.PDF”,选择搜索结果,点击“安装”:

C# 用JavaScript设置或验证 PDF中的文本域格式

3、点击“ok”,等待程序安装完成。

C# 用JavaScript设置或验证 PDF中的文本域格式

C#代码

1、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); } }}

2、执行程序,生成PDF文档。设置后的文本框域填写效果如图:

C# 用JavaScript设置或验证 PDF中的文本域格式
  • 水星路由器的固件升级教程
  • CentOS 7 下实现双网卡
  • 真女神转生4支线攻略
  • 怎么经营好一家土豆粉小吃店
  • 网页游戏代理和网页游戏平台怎么搭建?
  • 热门搜索
    防地震手抄报 普通话手抄报20字 奥运手抄报 文明上网手抄报 六年级语文手抄报 七一建党节手抄报 战争手抄报 中秋节英语手抄报 读书手抄报内容大全 勿忘国耻手抄报