概述

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.DrawingNamespace SetTextFormatInTextboxFieldClass ProgramPrivate Shared Sub Main(args As String())'新建PDF文档,并添加空白页Dim pdf As New PdfDocument()Dim page As PdfPageBase = pdf.Pages.Add()'定义坐标变量Dim X As Single = 10Dim Y As Single = 10Dim width As Single = 100Dim height As Single = 20'实例化一个文本域对象,并设置它的位置和边框样式Dim textbox As New PdfTextBoxField(page, "Number-TextBox")textbox.Bounds = New RectangleF(X, Y, width, height)textbox.BorderWidth = 0.75Ftextbox.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.75Ftextbox1.BorderStyle = PdfBorderStyle.SolidDim js1 As String = PdfJavaScript.GetDateKeystrokeString("mm/dd/yyyy")Dim jsAction1 As New PdfJavaScriptAction(js1)textbox1.Actions.KeyPressed = jsAction1js1 = PdfJavaScript.GetDateFormatString("mm/dd/yyyy")jsAction1 = New PdfJavaScriptAction(js1)textbox1.Actions.Format = jsAction1pdf.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.75Ftextbox2.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 = jsAction2pdf.Form.Fields.Add(textbox2)'添加文本框,设置文本内容显示为百分数Dim textbox3 As New PdfTextBoxField(page, "SpecialFormat2-TextBox")textbox3.Bounds = New RectangleF(X, Y + 50, width, height)textbox3.BorderWidth = 0.75Ftextbox3.BorderStyle = PdfBorderStyle.SolidDim js3 As String = PdfJavaScript.GetPercentKeystrokeString(1, 0)Dim jsAction3 As New PdfJavaScriptAction(js3)textbox3.Actions.KeyPressed = jsAction3js3 = PdfJavaScript.GetPercentFormatString(1, 0)jsAction3 = New PdfJavaScriptAction(js3)textbox3.Actions.Format = jsAction3pdf.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.75Ftextbox4.BorderStyle = PdfBorderStyle.SolidDim js4 As String = PdfJavaScript.GetRangeValidateString(True, -18, True, 18)Dim jsAction4 As New PdfJavaScriptAction(js4)textbox4.Actions.Format = jsAction4pdf.Form.Fields.Add(textbox4)'保存文档pdf.SaveToFile("FormatField.pdf", FileFormat.PDF)End SubEnd Class
End Namespace

C# 设置或验证 PDF中的文本域格式相关推荐

  1. linux中将文本中的单词换掉的指令_为什么说从PDF中提取文本是一件困难的事?...

    PDF文档处理工作中,总是绕不开对文本提取的需求.很多用户觉得我们PDFlux好用,所以对其中的底层技术也非常感兴趣.也有人为认为,从PDF里抽取文本段落和表格,应该非常简单! 近期,我们会对PDF文 ...

  2. java读取pdf_Java 读取PDF中的文本和图片的方法

    本文将介绍通过Java程序来读取PDF文档中的文本和图片的方法.分别调用方法extractText()和extractImages()来读取. 使用工具:Free Spire.PDF for Java ...

  3. java解析pdf 图片文字_Java 读取PDF中的文本和图片

    本文将介绍通过Java程序来读取PDF文档中的文本和图片的方法.分别调用方法extractText()和extractImages()来读取. 使用工具:Free Spire.PDF for Java ...

  4. html 提取pdf,使用PDF.js从PDF中提取文本(2019)

    正如标题所说,我正在尝试使用由Mozilla维护的PDF.js从PDF中提取文本.我知道前面关于stackoverflow的问题,但我不知道从哪里开始. 我试着跟着这个 article 这件事我需要帮 ...

  5. php取tet文件内容,PHP中使用PDFlib TET提取PDF中的文本

    本文介绍了如何在PHP中使用PDFlib TET提取PDF中的文本: /* 全局参数表*/ $globaloptlist = "searchpath=../../../resource/cm ...

  6. 更改PDF中的文本框的文字的大小和颜色

    1,点击文本框工具,添加文本框,并输入内容 2,更改PDF中的文本框的文字的大小和颜色 键盘Ctrl+E,将弹出文本属性菜单,此时就可以更改文本的字体.大小和颜色了. 参考:https://jingy ...

  7. word文档怎么给数字加千分符_Word中如何将文档中的金额数值设置为财务数字中的千分位格式...

    Word中如何将文档中的金额数值设置为财务数字中的千分位格式 时间:2015-06-10   作者:snow   来源:互联网 如图6‑19所示为某公司给省商务厅上报的2013年农产品现代流通综合试点 ...

  8. java pdf添加图片水印图片_Java 在PDF中添加文本水印、图片水印(基于Spire.Cloud.SDK for Java)...

    Spire.Cloud.SDK for Java提供了接口pdfWartermarkApi可用于添加文本水印addTextWartermark()和图片水印addImageWartermark()到P ...

  9. 如何调整PDF中的文本方向

    我们在编写Word文档输入文字时是可以选择并调整文字方向的,比如水平或者垂直,那么在制作PDF文档时或者修改PDF文件中的文本时是否也能旋转文字的方向呢? 修改PDF文档需要用到PDF编辑器,所以我们 ...

  10. php 获取pdf 坐标,php – 如何从pdf中提取文本图层和背景图层?

    我和你一样走在同一条路上,甚至还有更复杂的任务. 在尝试了所有的东西之后,我最终在Mono下使用C#(因此它在linux上运行)使用了iTextSharp. 即使有一个非常完整的库,如iTextSha ...

最新文章

  1. 重磅日程公布!与百名大咖在线交流技术,2天20个AI论坛不可错过
  2. 自定义Sharepoint的登陆页面(2)
  3. 陈勋教授的脑电信号降噪视频与讲座总结
  4. 索引的匹配方式有哪些?
  5. 有什么事情是你当了程序员之后才知道的?
  6. sublime cscope使用方法
  7. 入口函数ufusr()与ufsta()的区别
  8. 斗战神总是显示连接服务器失败,全民斗战神怎么进不去 全民斗战神进不去的原因和解决办法...
  9. AndroidProject
  10. 博科交换机建立Zone基本配置
  11. php主机卫士,Bypass 360主机卫士SQL注入防御(多姿势)
  12. ngnix 端口映射
  13. python 限定数据范围_python取值范围
  14. Python爬虫爬小说《诡秘之主》
  15. 检测PE文件加壳信息用的特征码
  16. Excel获取指定数字格式文本——TEXT函数及其用法
  17. 2022年自考专业考试(公关关系)人际关系学练习题
  18. 数据可视化工具是什么
  19. 索引,主键,唯一索引,联合索引 的区别
  20. java批量上传文件_Java 批量大文件上传下载

热门文章

  1. 2020年 交通领域SCI期刊分区
  2. Android修真传之工厂模式
  3. Dalvik字节码和Smali基本语法
  4. 囊括3大MCU+DSP开发工程
  5. Android自定义View(CustomCalendar-定制日历控件)
  6. 关于图片的像素,dpi与实际尺寸的关系
  7. win8.1系统中迅雷看看闪退问题
  8. Python 01--介绍、基本语法、流程控制
  9. mac u盘linux 双系统安装教程,U盘安装MAC双系统的方法
  10. 上海科技大学和 计算机,上海容易被忽略的高校--上海科技大学,本科升学率近80%,力压复旦、上交!...