首页 > 编程语言 >使用C# 创建、填写、删除PDF表单域

使用C# 创建、填写、删除PDF表单域

时间:2024-04-22 16:12:46浏览次数:24  
标签:C# new 表单 baseY pdf PDF page

通常情况下,PDF文件是不可编辑的,但PDF表单提供了一些可编辑区域,允许用户填写和提交信息。PDF表单通常用于收集信息、反馈或进行在线申请,是许多行业中数据收集和交换的重要工具。

PDF表单可以包含各种类型的输入控件,如文本框、复选框、下拉菜单、单选按钮等。本文将介绍如何使用C# 和一个免费.NET库来操作PDF表单,包括以下三个示例:

  • 创建PDF表单域
  • 填写PDF表单域
  • 删除PDF表单域

安装免费.NET PDF库Free Spire.PDF for .NET (可通过 NuGet安装,或下载后手动引用dll)

PM> Install-Package FreeSpire.PDF

 

常见PDF表单域

Free Spire.PDF for .NET 支持创建、操作多种PDF表域,包括文本框、复选框、组合框、列表框和单选按钮等。下表列出了一些常见的域及其在该免费库中对应的类名。

表单域名  类名
文本域 PdfTextBoxField
复选框 PdfCheckBoxField
组合框  PdfComboBoxField
列表框 PdfListBoxField
单选按钮 PdfRadioButtonListField
普通按钮 PdfButtonField
签名域  PdfSignatureField


使用C# 创建PDF表单域

使用Free Spire.PDF制作表单域,需要先创建以上各表单域类的对象,然后通过 Bounds 属性设置表单域的位置和大小,最后再通过PdfFormFieldCollection.Add() 方法将表单域绘制到PDF页面指定位置处。

以下是如何在PDF中创建上述常见PDF表单域的C#代码:

复制代码
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Fields;
using Spire.Pdf.Graphics;
using System.Drawing;
 
namespace CreateFillableFormsInPdf
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建PdfDocument对象
            PdfDocument pdf = new PdfDocument();
 
            //添加一页
            PdfPageBase page = pdf.Pages.Add();
 
            //初始化x、y坐标
            float baseX = 60;
            float baseY = 20;
 
            //创建两个画刷
            PdfSolidBrush brush1 = new PdfSolidBrush(new PdfRGBColor(Color.Brown));
            PdfSolidBrush brush2 = new PdfSolidBrush(new PdfRGBColor(Color.Black));
 
            //创建字体
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("微软雅黑", 11f, FontStyle.Regular), true);
 
            //添加文本框 
            page.Canvas.DrawString("姓名:", font, brush1, new PointF(10, baseY));
            RectangleF tbxBounds = new RectangleF(baseX, baseY, 150, 18);
            PdfTextBoxField textBox = new PdfTextBoxField(page, "姓名");
            textBox.Bounds = tbxBounds;
            textBox.Font = font;
            pdf.Form.Fields.Add(textBox);
            baseY += 30;
 
            //添加两个复选框
            page.Canvas.DrawString("民族:", font, brush1, new PointF(10, baseY));
            RectangleF checkboxBound1 = new RectangleF(baseX, baseY, 15, 15);
            PdfCheckBoxField checkBoxField1 = new PdfCheckBoxField(page, "选项1");
            checkBoxField1.Bounds = checkboxBound1;
            checkBoxField1.Checked = false;
            page.Canvas.DrawString("汉族", font, brush2, new PointF(baseX + 20, baseY));
 
            RectangleF checkboxBound2 = new RectangleF(baseX + 70, baseY, 15, 15);
            PdfCheckBoxField checkBoxField2 = new PdfCheckBoxField(page, "选项2");
            checkBoxField2.Bounds = checkboxBound2;
            checkBoxField2.Checked = false;
            page.Canvas.DrawString("少数民族", font, brush2, new PointF(baseX + 90, baseY));
            pdf.Form.Fields.Add(checkBoxField1);
            pdf.Form.Fields.Add(checkBoxField2);
            baseY += 30;
 
            //添加列表框
            page.Canvas.DrawString("分公司:", font, brush1, new PointF(10, baseY));
            RectangleF listboxBound = new RectangleF(baseX, baseY, 150, 50);
            PdfListBoxField listBoxField = new PdfListBoxField(page, "分公司");
            listBoxField.Items.Add(new PdfListFieldItem("成都", "成都"));
            listBoxField.Items.Add(new PdfListFieldItem("武汉", "武汉"));
            listBoxField.Items.Add(new PdfListFieldItem("深圳", "深圳")); ;
            listBoxField.Bounds = listboxBound;
            listBoxField.Font = font;
            pdf.Form.Fields.Add(listBoxField);
            baseY += 60;
 
            //添加两个单选按钮
            page.Canvas.DrawString("性别:", font, brush1, new PointF(10, baseY));
            PdfRadioButtonListField radioButtonListField = new PdfRadioButtonListField(page, "性别");
            PdfRadioButtonListItem radioItem1 = new PdfRadioButtonListItem("选项1");
            RectangleF radioBound1 = new RectangleF(baseX, baseY, 15, 15);
            radioItem1.Bounds = radioBound1;
            page.Canvas.DrawString("男", font, brush2, new PointF(baseX + 20, baseY));
 
            PdfRadioButtonListItem radioItem2 = new PdfRadioButtonListItem("选项2");
            RectangleF radioBound2 = new RectangleF(baseX + 70, baseY, 15, 15);
            radioItem2.Bounds = radioBound2;
            page.Canvas.DrawString("女", font, brush2, new PointF(baseX + 90, baseY));
            radioButtonListField.Items.Add(radioItem1);
            radioButtonListField.Items.Add(radioItem2);
            pdf.Form.Fields.Add(radioButtonListField);
            baseY += 30;
 
            //添加组合框
            page.Canvas.DrawString("部门:", font, brush1, new PointF(10, baseY));
            RectangleF cmbBounds = new RectangleF(baseX, baseY, 150, 18);
            PdfComboBoxField comboBoxField = new PdfComboBoxField(page, "部门");
            comboBoxField.Bounds = cmbBounds;
            comboBoxField.Items.Add(new PdfListFieldItem("财务", "财务"));
            comboBoxField.Items.Add(new PdfListFieldItem("技术", "技术"));
            comboBoxField.Items.Add(new PdfListFieldItem("采购", "采购"));
            comboBoxField.Items.Add(new PdfListFieldItem("销售", "销售"));
            comboBoxField.Font = font;
            pdf.Form.Fields.Add(comboBoxField);
            baseY += 30;
 
            //添加签名域
            page.Canvas.DrawString("签名:", font, brush1, new PointF(10, baseY));
            PdfSignatureField sgnField = new PdfSignatureField(page, "签名域");
            RectangleF sgnBounds = new RectangleF(baseX, baseY, 150, 80);
            sgnField.Bounds = sgnBounds;
            pdf.Form.Fields.Add(sgnField);
            baseY += 90;
 
            //添加按钮
            page.Canvas.DrawString("按钮:", font, brush1, new PointF(10, baseY));
            RectangleF btnBounds = new RectangleF(baseX, baseY, 50, 18);
            PdfButtonField buttonField = new PdfButtonField(page, "按钮");
            buttonField.Bounds = btnBounds;
            buttonField.Text = "提交";
            buttonField.Font = font;
            PdfSubmitAction submitAction = new PdfSubmitAction("https://www.****.com");
            submitAction.DataFormat = SubmitDataFormat.Html;
            buttonField.Actions.MouseDown = submitAction;
            pdf.Form.Fields.Add(buttonField);
 
            //保存文件
            pdf.SaveToFile("PDF表单.pdf", FileFormat.PDF);
        }
    }
}
复制代码

生成文件:

 

使用C# 填写PDF表单域

填充表单域需要先获取PDF中的所有表单字段,然后确定其表单类型,最后再填写数据或从预定列表中选择值。

以下是如何填充现有PDF表单域的C#代码:

复制代码
using Spire.Pdf;
using Spire.Pdf.Fields;
using Spire.Pdf.Widget;
 
namespace FillFormFields
{
    class Program
    {
        static void Main(string[] args)
        {
            //加载PDF表单
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("PDF表单.pdf");
 
            //获取文档中的表单
            PdfFormWidget form = (PdfFormWidget)pdf.Form;
 
            //获取表单域集合
            PdfFormFieldWidgetCollection formWidgetCollection = form.FieldsWidget;
 
            //遍历表单域
            for (int i = 0; i < formWidgetCollection.Count; i++)
            {
                //获取指定域
                PdfField field = formWidgetCollection[i];
 
                //判断该表单域是否为文本框
                if (field is PdfTextBoxFieldWidget)
                {
                    if (field.Name == "姓名")
                    {
                        //填充文本
                        PdfTextBoxFieldWidget textBoxField = (PdfTextBoxFieldWidget)field;
                        textBoxField.Text = "张三";
                    }
                }
 
                //判断该表单域是否为单选按钮
                if (field is PdfRadioButtonListFieldWidget)
                {
                    if (field.Name == "性别")
                    {
                        //为单选按钮选定一个值
                        PdfRadioButtonListFieldWidget radioButtonListField = (PdfRadioButtonListFieldWidget)field;
                        radioButtonListField.SelectedIndex = 0;
                    }
                }
 
                //判断该表单域是否为组合框
                if (field is PdfComboBoxWidgetFieldWidget)
                {
                    if (field.Name == "部门")
                    {
                        //为组合框选定一个值
                        PdfComboBoxWidgetFieldWidget comboBoxField = (PdfComboBoxWidgetFieldWidget)field;
                        int[] index = { 1 };
                        comboBoxField.SelectedIndex = index;
                    }
                }
 
                //判断该表单域是否为复选框
                if (field is PdfCheckBoxWidgetFieldWidget)
                {
                    //设置复选框的"已选中"状态
                    PdfCheckBoxWidgetFieldWidget checkBoxField = (PdfCheckBoxWidgetFieldWidget)field;
                    switch (checkBoxField.Name)
                    {
                        case "选项1":
                            checkBoxField.Checked = true;
                            break;
                    }
                }
 
                //判断该表单域是否为列表框
                if (field is PdfListBoxWidgetFieldWidget)
                {
                    if (field.Name == "分公司")
                    {
                        //为列表框选定一个值
                        PdfListBoxWidgetFieldWidget listBox = (PdfListBoxWidgetFieldWidget)field;
                        int[] index = { 1 };
                        listBox.SelectedIndex = index;
                    }
                }
            }
 
            //保存文件
            pdf.SaveToFile("填充PDF表单域.pdf", FileFormat.PDF);
        }
    }
}
复制代码

输出结果:

 

使用C# 删除PDF表单域

Free Spire.PDF支持通过索引或名称删除指定的表单域或删除所有表单域。

以下是如何删除PDF表单域的C#代码:

复制代码
using Spire.Pdf;
using Spire.Pdf.Fields;
using Spire.Pdf.Widget;
 
namespace RemoveFormFields
{
    class Program
    {
        static void Main(string[] args)
        {
            //加载PDF表单
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("PDF表单.pdf");
 
            //获取文档中的表单域
            PdfFormWidget formWidget = pdf.Form as PdfFormWidget;
 
            //遍历表单域
            for (int i = formWidget.FieldsWidget.List.Count - 1; i >= 0; i--)
            {
                //获取指定表单域
                PdfField field = formWidget.FieldsWidget.List[i] as PdfField;
 
                //删除表单域
                formWidget.FieldsWidget.Remove(field);
            }
 
            //通过表单域名获取指定表单
            //PdfField field = formWidget.FieldsWidget["name"];
            //删除该表单域
            //formWidget.FieldsWidget.Remove(field);
 
            //保存PDF文件
            pdf.SaveToFile("删除PDF表单域.pdf");
        }
    }
}
复制代码

 


 

 

以上代码演示了PDF表单的基本操作,包括添加文本框、复选框、组合框、单选按钮等各种常见的表单域,填写现有PDF表单,以及删除PDF表单。Free Spire.PDF免费库该支持其他多种PDF元素,点击查看更多示例

 

2024-04-22 16:01:18【出处】:https://www.cnblogs.com/Yesi/p/18150797

=======================================================================================

标签:C#,new,表单,baseY,pdf,PDF,page
From: https://www.cnblogs.com/mq0036/p/18150805

相关文章

  • JTCR-泛型-12
    什么是泛型具有参数化类型的类、接口或方法。具体的类型在运行时才确定。在泛型出现前通过使用Object引用也可以达到泛型的效果,但是缺乏类型安全检查,泛型添加了这一点。简单的泛型例子//T是类型参数,作为实际类型的占位符classGen<T>{Tv;Gen(To){v=o;......
  • 深度解读《深度探索C++对象模型》之数据成员的存取效率分析(三)
    接下来我将持续更新“深度解读《深度探索C++对象模型》”系列,敬请期待,欢迎关注!也可以关注公众号:iShare爱分享,自动获得推文和全部的文章列表。前面两篇请通过这里查看:深度解读《深度探索C++对象模型》之数据成员的存取效率分析(一)深度解读《深度探索C++对象模型》之数据成员的......
  • re-vctf2024-vm
    vctf2024-vm一.vctf2024vm题的题解,一直没有整理,是赛后看大佬wp才知道是upx魔改+rc4的。。二.解题思路1.去upx魔改:VCTF2024ezvm(虚拟机逆向初探)_vctfvm-CSDN博客[原创]UPX源码学习和简单修改-加壳脱壳-看雪-安全社区|安全招聘|kanxue.com加壳流程:(博客总结)a.写入文件的......
  • 速览丨NTP授时服务器(北斗授时设备)在DCS系统应用
    速览丨NTP授时服务器(北斗授时设备)在DCS系统应用速览丨NTP授时服务器(北斗授时设备)在DCS系统应用京准电子科技官微——ahjzsz前言随着计算机和网络通信技术的飞速发展,各行业自动化系统数字化、网络化的时代已经到来。这一方面为各控制和信息系统之间的数据交换、分析和应用提供了......
  • oracel触发器
    1.创建测试表 首先,创建一个名为SAMPLE_TABLE的简单测试表,包含两个字段:ID和DATA。CREATETABLESAMPLE_TABLE(IDNUMBERPRIMARYKEY,DATAVARCHAR2(50));2.创建触发器对应的审计日志表及辅助序列 创建一个名为AUDIT_LOG的表,用于记录触发器触发时的相关信息。 这......
  • 使用C# 创建、填写、删除PDF表单域
    通常情况下,PDF文件是不可编辑的,但PDF表单提供了一些可编辑区域,允许用户填写和提交信息。PDF表单通常用于收集信息、反馈或进行在线申请,是许多行业中数据收集和交换的重要工具。PDF表单可以包含各种类型的输入控件,如文本框、复选框、下拉菜单、单选按钮等。本文将介绍如何使用C#......
  • 持续性学习-Day15(前端基础CSS3)
    参考教学视频:秦疆1.什么是CSSCascadingStyleSheet层叠样式表CSS3圆角、阴影、动画...浏览器兼容性CSS优势:内容和表现分离网页结构表现统一,可以实现复用样式十分的丰富建议使用独立html的css文件利用SEO,容易被搜索引擎收录2.入门<linkrel="styleshee......
  • JTCR-I/O,Try-with-Resources 及其他-11
    I/O基础Java的I/O操作通过流来实现。流是对输入、输出数据的抽象,每个流都和一个具体的物理实体关联,比如在输入中,流可以和键盘、磁盘文件或者网络输入等关联,虽然每个物理实体不同,但是流可以以同样的方式进行处理。Java定义了字节流和字符流。字节流处理的对象是二进制数据,以......
  • 【pytorch学习】之数据操作
    1数据操作为了能够完成各种数据操作,我们需要某种方法来存储和操作数据。通常,我们需要做两件重要的事:(1)获取数据;(2)将数据读入计算机后对其进行处理。如果没有某种方法来存储数据,那么获取数据是没有意义的。首先,我们介绍n维数组,也称为张量(tensor)。使用过Python中NumPy计算包的读者......
  • 文字动态地变大变小(CSS)
    要在CSS中动态地改变文字大小,可以使用@keyframes规则创建一个动画,然后将该动画应用到需要改变大小的文本元素上。以下是一个示例,演示如何使文字大小在一定时间内从小变大,然后再变小:css部分:/*定义一个名为resizeText的动画*/@keyframesresizeText{0%{font-si......