服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#设置或验证PDF文本域格式的方法详解

C#设置或验证PDF文本域格式的方法详解

2022-12-16 12:08E-iceblue C#

PDF中的文本域可以通过设置不同格式,用于显示数字、货币、日期、时间、邮政编码、电话号码和社保号等等。本文将介绍如何通过C#设置或验证PDF文本域格式,需要的可以参考一下

概述

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中对应的方法,可参考使用:

C#设置或验证PDF文本域格式的方法详解

引入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#

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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

设置后的文本框域填写效果如图:

C#设置或验证PDF文本域格式的方法详解

以上就是C#设置或验证PDF文本域格式的方法详解的详细内容,更多关于C#设置 验证PDF文本域格式的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/Yesi/p/15773986.html

延伸 · 阅读

精彩推荐
  • C#C#拷贝整个文件夹及子目录和其中文件的方法

    C#拷贝整个文件夹及子目录和其中文件的方法

    这篇文章主要介绍了C#拷贝整个文件夹以及子目录和其中文件,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下...

    zhyue936312022-07-27
  • C#C# interface与delegate效能比较的深入解析

    C# interface与delegate效能比较的深入解析

    本篇文章是对C#中interface与delegate的效能比较进行了详细的分析介绍,需要的朋友参考下...

    C#教程网5042020-12-14
  • C#C#使用HttpWebRequest与HttpWebResponse模拟用户登录

    C#使用HttpWebRequest与HttpWebResponse模拟用户登录

    这篇文章主要为大家详细介绍了C#使用HttpWebRequest与HttpWebResponse模拟用户登录,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    道.玄11562021-12-31
  • C#使用Spire.Barcode程序库生成二维码的实例解析

    使用Spire.Barcode程序库生成二维码的实例解析

    这篇文章主要介绍了使用Spire.Barcode程序库生成二维码的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下...

    Eiceblue4812021-12-13
  • C#C#自定义IP输入框控件

    C#自定义IP输入框控件

    这篇文章主要为大家详细介绍了C#自定义IP输入框控件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    程序猿老黄4962022-02-23
  • C#如何实现定时推送的具体方案

    如何实现定时推送的具体方案

    在工作当中遇到了一个需要定时向客户端推送新闻、文章等内容。小项目又用不了大框架,这个时候在网上搜了很久没有找到合适的解决方案,直到看到了一...

    dotNet源计划4422022-11-15
  • C#完成OSS.Http底层HttpClient重构封装 支持标准库

    完成OSS.Http底层HttpClient重构封装 支持标准库

    OSS.Http项目对于.Net Standard标准库的支持已经迁移完毕,OSS开源系列两个最底层的类库已经具备跨运行时支持的能力。本篇文章主要包含 1. HttpClient的介绍,...

    KevinCC6782021-12-24
  • C#c# 实现发送邮件的功能

    c# 实现发送邮件的功能

    这篇文章主要介绍了c# 如何实现发送邮件的功能,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下...

    再学亿点点-young4132022-09-22