.NET平台用C#在PDF中创建可交互的表单域(Form Field)
在日常办公系统开发中,涉及 PDF 处理相关的开发时,生成可填写的 PDF 表单是一种常见需求,例如员工信息登记表、用户注册表、问卷调查或协议确认页等。与静态 PDF 不同,带有**表单域(Form Field)**的文档支持用户直接在 PDF 内部输入、勾选、选择等交互操作,极大提升了表单使用体验。
本文将介绍如何使用 C# 为 PDF 添加各种类型的表单域,包括文本框、下拉框、复选框、单选框、列表框和按钮,并通过完整示例演示如何将这些域组合成一个实际可用的表单页。
本文所使用的方法需要用到Free Spire.PDF for .NET,NuGet安装:PM> Install-Package FreeSpire.PDF
。
文章目录
- 使用 PdfTextBoxField 添加文本输入域
- 使用 PdfComboBoxField 添加下拉选择域
- 使用 PdfCheckBoxField 添加复选框域
- 综合示例:包含所有类型表单域的 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 = checkBox1Bounds;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");listBox.Bounds = listBoxBounds;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 = SubmitDataFormat.Html;button.Actions.MouseDown = submitAction;doc.Form.Fields.Add(button);// 保存文档doc.SaveToFile("FillableForm.pdf", FileFormat.PDF);doc.Close();}
}
创建结果
表单域类型一览
表单域类型 | 说明 |
---|---|
PdfTextBoxField | 文本输入域,用户可键入任意内容 |
PdfCheckBoxField | 勾选框,可用于二选一逻辑判断 |
PdfComboBoxField | 下拉选择域,提供固定选项 |
PdfListBoxField | 多项列表,可启用多选模式 |
PdfRadioButtonListField | 单选按钮组,用户仅能选一项 |
PdfButtonField | 按钮,可设定执行特定操作 |
通过以上方式,开发者可以快速构建结构清晰、功能完备的 PDF 表单,实现用户信息采集、文档自动化交互等多种应用场景。
更多内容请参考:Spire.PDF 官方文档