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

描述

示例

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—

 

引用来源:https://www.cnblogs.com/Yesi/p/15773986.html

版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
管理员
上一篇:微软的Serialize和Newtonsoft的SerializeObject比较
下一篇:WPF Command绑定并传参(以DataGrid示例)
评论列表

发表评论

评论内容
昵称:
验证码:
验证码
关联文章

C# 设置验证 PDF文本格式
C# 使用JavaScript给PDF文档设置过期时间
C# 将PDF转为线性化PDF
C# 在PDF文档应用多种不同字体
System.BadImageFormatException:“未能加载文件程序集 某一个依赖项。试图加载格式不正确程序
fastreport显示带[] 方括号文本表达式
Excel自定义格式千分符
C# 利用 SharpZipLib 对多个文本字符串进行多文件打包为RARZIP并进行下载
利用SelectPdf插件将网页生成PDF
Http请求Referer设置,CEFSharp带Referer请求
C# 7.0 - C# 7.3 新增功能
SAP S/4HANA MM模块培训 50 - 采购文本类型与文本采用:抬头、项目、复制规则与固定标识
NPOI生成Excel文件时设置一整列为文本类型
RSA PrivateKey私钥字符串转PEM格式证书
Windows server 解决“无法完成加入,原因是试图加入SID与本计算机SID相同
C#进化——C#发展史、C#1.0-10.0语法系统性梳理、C#与JAVA对比
使用 WPF 做个 PowerPoint 系列 文本 BodyProperties FontScale 与文本字号缩放
C# 扫描识别图片文字(.NET Framework)
fastreport文本旋转竖排
windows修改密码过期策略

热门标签
.NET Core .NET Reactor ag-grid AI发布 api安全 ASP.NET Core C#DLL加密 C#播放声音 C#代码混淆 C#代码加密 ChromeDriver Codex DateTime DBeaver devexpress devTool DLL混淆 edge.js EF EFCore Electron element-ui el-form el-table excel FastReport FileStream FolderBrowerDialog FolderSelectDialog form提交 git gridcontrol gridview input javascript json字符串 JS转换对象JSON jwt JWT授权 linq log Math MCP mitmproxy MVC MySQL Navicat netstat nginx node_modules NSwag Nuget Nuget镜像 number PowerShell pyinstaller python pythoncom python爬虫 python抓包 pywin32 redis Requests-html RestSharp Selenium sql SQL Server Swagger to-cms Visual Studio VSCode vue VueRouter vue路由 VUE页面通讯 Webpack Windows Windows服务 winform wmi xlrd yaml YESCMS YESWEB开发框架 白象 表单提交 播放声音 打开URL 代码混淆 弹窗提醒 端口占用 对象转换 分布式 公共字典 机器码 进程排查 静态资源 开发指南 路由参数 密钥 配置教程 配置文件 权限 人工智能 任务 任务调度 日期间隔 日志 日志记录 省市区 授权验证 数据库 四舍五入 文案 文件读取 文件夹选择 文件目录选择 问题排查 行政区域数据 页面通讯 中间件 CSharp 事务锁 工单系统 并发控制 重复提交 CMS Markdig Markdown markdown-it marked 技术选型 VS Code 开发工具 源代码管理 版本控制 Docker PostgreSQL 时区 部署排查 CMS架构 EF Core 主题系统 二次开发 插件系统 容器 运维命令 镜像清理 Linux NAS 远程挂载 飞牛 fnOS S/4HANA SAP GUI SAP HANA SAP R/3 SAP入门 SAP版本 ERP SAP SAP MM 库存管理 物料管理 采购管理 入门教程 SAP S/4HANA SPRO 企业结构 采购组织 MM01 物料主数据 物料类型 BP分组 业务伙伴 供应商主数据 ME41 RFQ 库存物料 采购流程 ME51 消耗性物料 科目分配 采购申请 AC03 ML81N 外部服务 服务主数据 Business Partner SAP培训 ME51N MM模块 Lean Services MM-SRV 外部服务采购 PIR 供应来源 采购主数据 采购信息记录 ME31K 框架协议 计划协议 采购合同 ME01 供应来源确定 货源清单 MEQ1 供应源确定 配额安排 配额评分 MD04 MD21 MRP 计划文件 需求计划 批量程序 MD01N MD02 MRP Live MD05 MM 物料计划 优化采购 供应源 采购订单 ME2A 供应商确认 采购监控 Flexible Workflow 凭证释放 采购审批 释放策略 实地盘点 物料凭证 货物移动 MIGO 收货 移动类型 已撤回 供应商退货 货物发出 STO 库存转储 转移过账 生产订单 预留 GR/IR MIRO 供应商发票 物流发票校验 OMR2 税码 FI PP SD 实操教程 MRBR OMR6 发票差异 交货成本 后续借记 MI01 实物盘点 盘点差异 公司代码 工厂 组织结构 OMS2 主数据定制 自动科目确定 BP角色 CVI 伙伴确定 编号范围 凭证类型 字段选择 FBN1 OMBT OMC2 会计凭证 OMJJ BOM 委外加工 项目类别L MRKO 供应商寄售 特殊库存K MRKON PIPE Pipeline 特殊库存P ERS MRIS 发票计划 周期性结算 里程碑付款 变更追踪 版本管理 采购凭证 SFTP WebDAV 网盘 飞牛fnOS AMPL HERS MPN 中文教程 库存确定 可用性检查 缺件检查 Output Management 消息确定 输出确定 分割评估 库存计价 评估类别 评估类型 PB00 RM0000 条件技术 采购定价 MM-FI集成 OBYC 库存估价 文本类型 文本采用 EFB EVO MSV SU3 用户参数 发票校验 合同参照 履约保留款 特别总账 预付款 Fiori Launchpad SAP Fiori 应用导航 用户体验 LSMW LTMC Migration Cockpit 数据迁移 BRFplus OPD Output Control My Inbox 审批流程 灵活工作流 SAP PP 外部加工 SAP QM 检验批 质量信息记录 采购收货 SAP PM 维护BOM 维护订单 SAP SD SAP Service 端到端流程 MM模块培训 FI-MM集成 供应商管理 审批配置 FICO入门 SAP FICO 财务配置 供应商税务 预扣税 House Bank 银行对账 客户清账 应收账款 FI控制 验证与替代 印度 GST 税务配置 F110 FBZP EWM入门 SAP EWM 仓库管理 OX14 成本核算 物料评估 后勤配置 物料组 价值更新 数量更新 PP-PI 流程制造 生产计划 容差配置 SAP事务码 SAP基础 TCODE Basis 事务代码 MMNR 编号区间 采购实操 组织架构 OMSF SAP实操 FI配置
联系我们
联系电话:15090125178(微信同号)
电子邮箱:garson_zhang@163.com
站长微信二维码
微信二维码