在.NET平台使用C#为PDF添加各种类型的表单域的方法

2025-05-03 18:50

本文主要是介绍在.NET平台使用C#为PDF添加各种类型的表单域的方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

《在.NET平台使用C#为PDF添加各种类型的表单域的方法》在日常办公系统开发中,涉及PDF处理相关的开发时,生成可填写的PDF表单是一种常见需求,与静态PDF不同,带有**表单域的文档支持用户直接在...

引言

在日常办公系统开发中,涉及 PDF 处理相关的开发时,生成可填写的 PDF 表单是一种常见需求,例如员工信息登记表、用户注册表、问卷调查或协议确认页等。与静态 PDF 不同,带有**表单域(Form Field)**的文档支持用户直接在 PDF 内部输入、勾选、选择等交互操作,极大提升了表单使用体验。

本文将介绍如何使用 C# 为 PDF 添加各种类型的表单域,包括文本框、下拉框、复选框、单选框、列表框和按钮,并通过完整示例演示如何将这些域组合成一个实际可用的表单页。

本文所使用的方法需要用到Free Spire.PDF for .NET,NuGet安装PM> Install-Package FreeSpire.PDF

使用 PdfTextBoxField 添加文本输入域

PdfTextBoxField 表示文本输入域,适用于姓名、地址、日期等自由输入内容。

PdfTextBoxField textBox = new PdfTextBoxField(page, "textBox");
textBox.Bounds = new RectangleF(100, 50, 150, 20);
textBox.Text = "Enter your name";
textBox.Font = new PdfFont(PdfFontFamily.Helvetica, 12f);
doc.Form.Fields.Add(textBox);

使用 PdfComboBoxField 添加下拉选择域

PdfComboBoxField 是用于显示可选列表的下拉框,适合性别、部门、国籍等字段。

PdfComboBoxField comboBox = new PdfComboBoxField(page, "comboBox");
comboBox.Bounds = new RectangleF(100, 110, 150, 20);
comboBox.Items.Add(new PdfListFieldItem("Option A", "A"));
comboBox.Items.Add(new PdfListFieldItem("Option B", "B"));
comboBox.Items.Add(new PdfListFieldItem("Option C", "C"));
comboBox.SelectedIndex = 0;
comboBox.Font = new PdfFont(PdfFontFamily.Helvetica, 12f);
doc.Form.Fields.Add(comboBox);

使用 PdfCheckBoxField 添加复选框域

PdfCheckBoxField 表示复选框,适用于“是否同意”、“是否接收通知”等二元布尔选项。

PdfCheckBoxField checkBox = new PdfCheckBoxField(page, "checkBox");
checkBox.Bounds = new RectangleF(100, 80, 15, 15);
checkBox.Checked = false;
doc.Form.Fields.Add(checkBox);

综合示例:包含所有类型表单域的 PDF 表单

以下代码创建了一个“用户信息登记表”,整合了所有常见表单域类型,包括文本框、下拉框、复选框、列表框、单选按钮和按钮。

using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Fields;
using Spire.Pdf.Graphics;
using System.Drawing;

class Program
{
    static void Main(string[] args)
    {
        // 创建文档和页面
        PdfDocument doc = new PdfDocument();
        PdfPageBase page = doc.Pages.Add();

        // 坐标和样式初始化
        float baseX = 100;
        float baseY = 30;
        PdfSolidBrush titleBrush = new PdfSolidBrush(new PdfRGBColor(Color.Blue));
        PdfSolidBrush labelBrush = new PdfSolidBrush(new PdfRGBColor(Color.Black));
        PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Regular);

        // 文本框
        page.Canvas.DrawString("TextBox:", font, titleBrush, new PointF(10, baseY));
        RectangleF textBoxBounds = new RectangleF(baseX, baseY, 150, 15);
        PdfTextBoxField textBox = new PdfTextBoxField(page, "textbox");
        textBox.Bounds = textBoxBounds;
        textBox.Text = "Hello World";
        textBox.Font = font;
        doc.Form.Fields.Add(textBox);
        baseY += 25;

        // 复选框
        page.Canvas.DrawString("CheckBox:", font, titleBrush, new PointF(10, baseY));
        RectangleF checkBox1Bounds = new RectangleF(baseX, baseY, 15, 15);
        PdfCheckBoxField checkBox1 = new PdfCheckBoxField(page, "checkbox1");
        checkBox1.Bounds = checkBox1China编程Bounds;
        checkBox1.Checked = false;
        page.Canvas.DrawString("Option 1", font, labelBrush, new PointF(baseX + 20, baseY));

        RectangleF checkBox2Bounds = new RectangleF(baseX + 70, baseY, 15, 15);
        PdfCheckBoxField checkBox2 = new PdfCheckBoxField(page, "checkbox2");
        checkBox2.Bounds = checkBox2Bounds;
        checkBox2.Checked = false;
        page.Canvas.DrawString("Option 2", font, labelBrush, new PointF(baseX + 90, baseY));

        doc.Form.Fields.Add(checkBox1);
        doc.Form.Fields.Add(checkBox2);
        baseY += 25;

        // 下拉列表框
        page.Canvas.DrawString("ComboBox:", font, titleBrush, new PointF(10, baseY));
        RectangleF comboBoxBounds = new RectangleF(baseX, baseY, 150, 15);
        PdfComboBoxField comboBox = new PdfComboBoxField(page, "combobox");
        comboBox.Bounds = comboBoxBounds;
        comboBox.Items.Add(new PdfListFieldItem("Item 1", "item1"));
        comboBox.Items.Add(new PdfListFieldItem("Item 2", "item2"));
        comboBox.Items.Add(new PdfListFieldItem("Item 3", "item3"));
        comboBox.SelectedIndex = 0;
        comboBox.Font = font;
        doc.Form.Fields.Add(comboBox);
        baseY += 25;

        // 列表框
        page.Canvas.DrawString("ListBox:", font, titleBrush, new PointF(10, baseY));
        RectangleF listBoxBounds = new RectangleF(baseX, baseY, 150, 50);
        PdfListBoxField listBox = new PdfListBoxField(page, "listbox");
        listhttp://www.chinasem.cnBox.Bounds = listBoxBojsunds;
        listBox.Items.Add(new PdfListFieldItem("Item 1", "item1"));
        listBox.Items.Add(new PdfListFieldItem("Item 2", "item2"));
        listBox.Items.Add(new PdfListFieldItem("Item 3", "item3"));
        listBox.SelectedIndex = 0;
        listBox.Font = font;
        doc.Form.Fields.Add(listBox);
        baseY += 60;

        // 单选按钮
        page.Canvas.DrawString("RadioButton:", font, titleBrush, new PointF(10, baseY));
        PdfRadioButtonListField radioGroup = new PdfRadioButtonListField(page, "radioGroup");
        PdfRadioButtonListItem radio1 = new PdfRadioButtonListItem("Option1");
        radio1.Bounds = new RectangleF(baseX, baseY, 15, 15);
        page.Canvas.DrawString("Option 1", font, labelBrush, new PointF(baseX + 20, baseY));

        PdfRadioButtonListItem radio2 = new PdfRadioButtonListItem("Option2");
        radio2.Bounds = new RectangleF(baseX + 70, baseY, 15, 15);
        page.Canvas.DrawString("Option 2", font, labelBrush, new PointF(baseX + 90, baseY));

        radioGroup.Items.Add(radio1);
        radioGroup.Items.Add(radio2);
        radioGroup.SelectedIndex = 0;
        doc.Form.Fields.Add(radioGroup);
        baseY += 25;

        // 签名域
        page.Canvas.DrawString("Signature Field:", font, titleBrush, new PointF(10, baseY));
        RectangleF signatureBounds = new RectangleF(baseX, baseY, 150, 80);
        PdfSignatureField signatureField = new PdfSignatureField(page, "signatureField");
        signatureField.Bounds = signatureBounds;
        doc.Form.Fields.Add(signatureField);
        baseY += 90;

        // 按钮
        page.Canvas.DrawString("Button:", font, titleBrush, new PointF(10, baseY));
        RectangleF buttonBounds = new RectangleF(baseX, baseY, 50, 15);
        PdfButtonField button = new PdfButtonField(page, "submitButton");
        button.Bounds = buttonBounds;
        button.Text = "Submit";
        button.Font = font;
        PdfSubmitAction submitAction = new PdfSubmitAction("https://www.google.com/");
        submitAction.DataFormat = SubmitDjavascriptataFormat.html;
        button.Actions.MouseDown = submitAction;
        doc.Form.Fields.Add(button);

        // 保存文档
        doc.SaveToFile("FillableForm.pdf", FileFormat.PDF);
        doc.Close();
    }
}

创建结果

在.NET平台使用C#为PDF添加各种类型的表单域的方法

表单域类型一览

表单域类型说明
PdfTextBoxField文本输入域,用户可键入任意内容
PdfCheckBoxField勾选框,可用于二选一逻辑判断
PdfComboBoxField下拉选择域,提供固定选项
PdfListBoxF编程ield多项列表,可启用多选模式
PdfRadioButtonListField单选按钮组,用户仅能选一项
PdfButtonField按钮,可设定执行特定操作

通过以上方式,开发者可以快速构建结构清晰、功能完备的 PDF 表单,实现用户信息采集、文档自动化交互等多种应用场景。

到此这篇关于在.NET平台使用C#为PDF添加各种类型的表单域的方法的文章就介绍到这了,更多相关C#为PDF添加表单域内容请搜索编程China编程(www.chinasem.cn)以前的文章或继续浏览下面的相关文章希望大家以后多多支持China编程(www.chinasem.cn)!

这篇关于在.NET平台使用C#为PDF添加各种类型的表单域的方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1154464

相关文章

python使用库爬取m3u8文件的示例

《python使用库爬取m3u8文件的示例》本文主要介绍了python使用库爬取m3u8文件的示例,可以使用requests、m3u8、ffmpeg等库,实现获取、解析、下载视频片段并合并等步骤,具有... 目录一、准备工作二、获取m3u8文件内容三、解析m3u8文件四、下载视频片段五、合并视频片段六、错误

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三

javax.net.ssl.SSLHandshakeException:异常原因及解决方案

《javax.net.ssl.SSLHandshakeException:异常原因及解决方案》javax.net.ssl.SSLHandshakeException是一个SSL握手异常,通常在建立SS... 目录报错原因在程序中绕过服务器的安全验证注意点最后多说一句报错原因一般出现这种问题是因为目标服务器

Python打印对象所有属性和值的方法小结

《Python打印对象所有属性和值的方法小结》在Python开发过程中,调试代码时经常需要查看对象的当前状态,也就是对象的所有属性和对应的值,然而,Python并没有像PHP的print_r那样直接提... 目录python中打印对象所有属性和值的方法实现步骤1. 使用vars()和pprint()2. 使

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解