通常情况下,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元素,点击查看更多示例

使用C# 创建、填写、删除PDF表单域的更多相关文章

  1. Java 创建、填充PDF表单域

    表单域,可以按用途分为多种不同的类型,常见的有文本框.多行文本框.密码框.隐藏域.复选框.单选框和下拉选择框等,目的是用于采集用户的输入或选择的数据.下面的示例中,将分享通过Java编程在PDF中添加 ...

  2. 对pdf 表单域 或文本框的操作---动态填充PDF 文件内容

    前提:需要pdf模板:并且模板内容以pdf 文本框的形式填写 package com.test;import java.io.File;import java.io.FileOutputStream; ...

  3. PDF表单域(FormField)在HTML显示与提交数据到服务器

    1.Adobe Arobat Pro等可以编辑表单域,只有几种控件: 2.展示PDF,可用PdfObject.js,Chrome自带? @{ViewBag.Title = @ViewBag.aaa;} ...

  4. php 填写pdf 表单

    最近接到新的任务,要求把pdf的文档,编辑后发邮件 首先pdf表单提交,需要用到这个东西pdftk,GitHub地址:https://github.com/mikehaertl/php-pdftk 首 ...

  5. [.Net] - 使用 iTextSharp 生成基于模板的 PDF,生成新文件并保留表单域

    背景 基于 PDF Template 预填充表单项,生成一份新的 PDF 文件,并保留表单域允许继续修改. 代码段 using iTextSharp.text.pdf; /* Code Snippet ...

  6. laravel使用创建的request作为表单验证类

    1.使用命令行工具创建request php artisan make request:validateLoginRequest 2.创建后进入app/Http/Requests目录下找到创建的文件 ...

  7. P​D​F​二​次​开​发​_​i​S​t​y​l​e​P​D​F​表​单​域​的​填​充

    wo讲到PDF表单,我们首先需要认识Adobe定义的PDF表单有哪些.以下是我从网上搜索到的简单介绍: PDF 表单简介 PDF 是可移植文档格式(Portable Document Format)的 ...

  8. Servlet之会话(Session)以及会话追踪技术(Cookie),(URL重写)和(隐藏表单域)

    Session 什么是会话? 会话: Web应用中的会话 指的是一个客户端浏览器与Web服务器之间连续发生的一系列请求和响应的过程 会话状态: Web服务器和浏览器在会话的过程中产生的状态信息 作用: ...

  9. 隐藏表单域、URL重写、cookie、session

    隐藏表单域: 隐藏域是用来收集或发送信息的不可见元素,对于网页的访问者来说,隐藏域是看不见的.当表单被提交时,隐藏域就会将信息用你设置时定义的名称和值发送到服务器上. 代码格式:<input t ...

  10. knockoutJS学习笔记08:表单域绑定

    前面的绑定都是用在基本标签上,这章主要讲表单域标签的绑定. 一.value 绑定 绑定标签:input text.textarea. <p>用户名:<input type=" ...

随机推荐

  1. 接入移动手机号一键登录类的封装,app应用,php服务端类的封装与调用

    需求:实现手机号一键登录,由于官方只有java的demo和jar包,没有php的sdk及demo <?php/* * 手机号一键登录加解密 */class Autophone{ const A_ ...

  2. autohotkey 设置快捷键 设置光标位置 (ctrl + alt + Numpad0)

    autohotkey 设置快捷键 设置光标位置 (ctrl + alt + Numpad0) 原因 3个屏幕,所以鼠标设置的灵敏度非常高,经常就找不到鼠标在哪了. 设置个快捷键,让鼠标每次都初始化一个 ...

  3. C++学习笔记之编程思想

    目录 编程思想 单例(Singleton)模式 观察者(Observer)模式 void*.NULL和nullptr C的类型转换 C++的类型转换 适配器(Adapter)模式 泛型编程的思想 模板 ...

  4. pandas DataFrame内存优化技巧:让数据处理更高效

    Pandas无疑是我们数据分析时一个不可或缺的工具,它以其强大的数据处理能力.灵活的数据结构以及易于上手的API赢得了广大数据分析师和机器学习工程师的喜爱. 然而,随着数据量的不断增长,如何高效.合理 ...

  5. cpprestsdk移植到mingw,项目上传至github

    如题 https://github.com/bbqz007/cpprestsdk4mingw 移植过程解决的问题,下面列出其中一些问题: 1. mingw对#pragma once支持不好. 须要在所 ...

  6. java unsigned int,int,long

    java 中没有unsigned int,处理这个要采用long. int x = (1<<31) 与int x= -(1<<31)答案是相同的 0xffff ffff 与0x ...

  7. 您真的了解Java中的锁吗?这7种不同维度下的锁知道吗?

    写在开头 在上几篇博文中,我们聊到过volatile关键字,用它修饰变量可以保证可见性与有序性,但它并不是锁,在使用时并不会阻塞线程,且不保证原子性,属于一种轻量级.高效的同步方式,因此,如果我们的使 ...

  8. Cesium之DrawCommand与绘制三角形

    1. 引言 Cesium中的Command对象包含执行的指令参数和执行方法,Command对象主要有三类: ClearCommand DrawCommand ComputeCommand DrawCo ...

  9. 记录-JS简单实现购物车图片局部放大预览效果

    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 一.实现效果 二.代码实现 代码不多,先看一下 HTML 里面结构很简单,初始化 MagnifyingGlass 对象来关联一个 IMG  ...

  10. Error in beforeDestroy hook: “Error: [ElementForm]unpected width “found in

    吹水,可忽略 当我尝试吧el-form中labelWidth设为auto时,刷新页面获取到了上面的错误 百思不得其解,我貌似没有在beforeDestroy进行操作,为何会报这个错误 果断各种百度,G ...