引言
在日常办公系统开发中,涉及 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 = 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 表单,实现用户信息采集、文档自动化交互等多种应用场景。
到此这篇关于在.net平台使用c#为pdf添加各种类型的表单域的方法的文章就介绍到这了,更多相关c#为pdf添加表单域内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论