如何用 C# 创建 PDF 表单;
using IronPDF 的综合 API 在 C# 中创建交互式 PDF 表单,只需几行代码即可支持文本输入、复选框、单选按钮、组合框、图像表单和数字签名。
快速入门:使用IronPDF创建您的第一个 PDF 表单快速开始使用IronPDF创建交互式、可填写的PDF表单。 本指南向您展示如何使用简单的 C# 代码动态生成带有表单字段的 PDF。 通过利用IronPDF强大的API,您可以创建文本输入、复选框等,以便以最小的努力增强用户交互。 跟随指南查看定义表单元素并保存文档是多么简单。
-
1Install IronPDF with NuGet Package Manager
-
2复制并运行这段代码。
new IronPdf.ChromePdfRenderer { RenderingOptions = { CreatePdfFormsFromHtml = true } } .RenderHtmlAsPdf("<html><body><form>First name: <input type='text' name='firstname' value=''>Last name: <input type='text' name='lastname' value=''></form></body></html>") .SaveAs("editableForm.pdf");C# -
3部署到您的生产环境中进行测试
通过免费试用立即在您的项目中开始使用IronPDF
降低每年的 PDF 安全和合规成本。IronSecureDoc提供数字签名、编辑、加密和保护解决方案,只需一次性付费。查看 IronSecureDoc 文档
最小工作流程(5 个步骤)
- 下载 IronPDF C# 库。
- 使用 HTML 中的输入和文本区域标记生成表单
- 将类型设为 "复选框",并对复选框和组合框使用 select 标记
- 将单选按钮的类型改为 "单选"
- 检查生成的 PDF 输出
如何使用 IronPDF 创建 PDF 表单?
IronPDF 可创建内嵌各种类型表单字段的 PDF 文档。通过在静态 PDF 文档中添加动态表单元素,可以增强灵活性和交互性。 该库支持现代 PDF 应用程序所需的所有表单元素,从简单的文本输入到复杂的签名字段。 在创建表格之前,请确保您已经安装了 IronPDF 并配置了您的开发环境。
如何添加文本区域和输入表单?
如何从 HTML 渲染表单?
创建文本区域和输入表单,以捕捉 PDF 文档中的用户输入。 文本区域提供了较大的文本空间,而输入表单允许输入特定的值。 IronPDF 的 HTML 至 PDF 转换引擎支持 HTML 表单元素,简化了复杂表单的创建。
using IronPdf;
// Input and Text Area forms HTML
string FormHtml = @"
<html>
<body>
<h2>Editable PDF Form</h2>
<form>
First name: <br> <input type='text' name='firstname' value=''> <br>
Last name: <br> <input type='text' name='lastname' value=''> <br>
Address: <br> <textarea name='address' rows='4' cols='50'></textarea>
</form>
</body>
</html>
";
// Instantiate Renderer
ChromePdfRenderer Renderer = new ChromePdfRenderer();
Renderer.RenderingOptions.CreatePdfFormsFromHtml = true;
Renderer.RenderHtmlAsPdf(FormHtml).SaveAs("textAreaAndInputForm.pdf");Imports IronPdf
' Input and Text Area forms HTML
Private FormHtml As String = "
<html>
<body>
<h2>Editable PDF Form</h2>
<form>
First name: <br> <input type='text' name='firstname' value=''> <br>
Last name: <br> <input type='text' name='lastname' value=''> <br>
Address: <br> <textarea name='address' rows='4' cols='50'></textarea>
</form>
</body>
</html>
"
' Instantiate Renderer
Private Renderer As New ChromePdfRenderer()
Renderer.RenderingOptions.CreatePdfFormsFromHtml = True
Renderer.RenderHtmlAsPdf(FormHtml).SaveAs("textAreaAndInputForm.pdf")输出PDF文档
如何以编程方式添加文本表单?
通过代码添加文本表单字段,无需 HTML。 首先,使用所需参数实例化一个PdfFormField对象。 然后使用PdfForm方法添加表单。 当您需要编辑现有的 PDF 文件或在非 HTML 创建的文档中添加表单时,这种方法非常有效。
using IronPdf;
using IronSoftware.Forms;
// Instantiate ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h2>Editable PDF Form</h2>");
// Configure required parameters
string name = "firstname";
string value = "first name";
uint pageIndex = 0;
double x = 100;
double y = 700;
double width = 50;
double height = 15;
// Create text form field
var textForm = new TextFormField(name, value, pageIndex, x, y, width, height);
// Add form
pdf.Form.Add(textForm);
pdf.SaveAs("addTextForm.pdf");Imports IronPdf
Imports IronSoftware.Forms
' Instantiate ChromePdfRenderer
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h2>Editable PDF Form</h2>")
' Configure required parameters
Private name As String = "firstname"
Private value As String = "first name"
Private pageIndex As UInteger = 0
Private x As Double = 100
Private y As Double = 700
Private width As Double = 50
Private height As Double = 15
' Create text form field
Private textForm = New TextFormField(name, value, pageIndex, x, y, width, height)
' Add form
pdf.Form.Add(textForm)
pdf.SaveAs("addTextForm.pdf")输出PDF文档
使用AddLabel()方法为表单字段添加标签。 了解更多关于如何在PDF上绘制文本和位图。 探索自定义字体以增强表单外观。
如何创建复选框和组合框表单?
如何从 HTML 渲染复选框和组合框?
通过渲染包含这些元素的 HTML 字符串、文件或 Web URL,创建复选框和组合框表单。 设置EnableFormRendering属性为true以启用表单创建。 对于复杂的场景,转换包含服务器端表单控件的 ASPX 页面。
组合框表单提供下拉选择。 用户从可用选项中进行选择,在 PDF 文档中提供输入。
using IronPdf;
// Input and Text Area forms HTML
string FormHtml = @"
<html>
<body>
<h2>Editable PDF Form</h2>
<h2>Task Completed</h2>
<label>
<input type='checkbox' id='taskCompleted' name='taskCompleted'> Mark task as completed
</label>
<h2>Select Priority</h2>
<label for='priority'>Choose priority level:</label>
<select id='priority' name='priority'>
<option value='high'>High</option>
<option value='medium'>Medium</option>
<option value='low'>Low</option>
</select>
</body>
</html>
";
// Instantiate Renderer
ChromePdfRenderer Renderer = new ChromePdfRenderer();
Renderer.RenderingOptions.CreatePdfFormsFromHtml = true;
Renderer.RenderHtmlAsPdf(FormHtml).SaveAs("checkboxAndComboboxForm.pdf");Imports IronPdf
' Input and Text Area forms HTML
Private FormHtml As String = "
<html>
<body>
<h2>Editable PDF Form</h2>
<h2>Task Completed</h2>
<label>
<input type='checkbox' id='taskCompleted' name='taskCompleted'> Mark task as completed
</label>
<h2>Select Priority</h2>
<label for='priority'>Choose priority level:</label>
<select id='priority' name='priority'>
<option value='high'>High</option>
<option value='medium'>Medium</option>
<option value='low'>Low</option>
</select>
</body>
</html>
"
' Instantiate Renderer
Private Renderer As New ChromePdfRenderer()
Renderer.RenderingOptions.CreatePdfFormsFromHtml = True
Renderer.RenderHtmlAsPdf(FormHtml).SaveAs("checkboxAndComboboxForm.pdf")输出PDF文档
如何以编程方式添加复选框和组合框?
如何创建复选框表单字段?
要添加复选框,使用所需参数实例化一个CheckboxField对象。 value参数确定检查状态:"无"为未检查,"是"为已检查。 使用PdfForm方法添加表单。
using IronPdf;
using IronSoftware.Forms;
// Instantiate ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h2>Checkbox Form Field</h2>");
// Configure required parameters
string name = "checkbox";
string value = "no";
uint pageIndex = 0;
double x = 100;
double y = 700;
double width = 15;
double height = 15;
// Create checkbox form field
var checkboxForm = new CheckboxFormField(name, value, pageIndex, x, y, width, height);
// Add form
pdf.Form.Add(checkboxForm);
pdf.SaveAs("addCheckboxForm.pdf");Imports IronPdf
Imports IronSoftware.Forms
' Instantiate ChromePdfRenderer
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h2>Checkbox Form Field</h2>")
' Configure required parameters
Private name As String = "checkbox"
Private value As String = "no"
Private pageIndex As UInteger = 0
Private x As Double = 100
Private y As Double = 700
Private width As Double = 15
Private height As Double = 15
' Create checkbox form field
Private checkboxForm = New CheckboxFormField(name, value, pageIndex, x, y, width, height)
' Add form
pdf.Form.Add(checkboxForm)
pdf.SaveAs("addCheckboxForm.pdf")输出PDF文档
如何创建组合框表单字段?
要添加组合框,使用所需参数实例化一个ComboBoxField对象。 PdfForm方法添加表单。 在处理表单数据时,从现有 PDF 中提取文本和图像,动态填充组合框。
using IronPdf;
using IronSoftware.Forms;
using System.Collections.Generic;
// Instantiate ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h2>Combobox Form Field</h2>");
// Configure required parameters
string name = "combobox";
string value = "Car";
uint pageIndex = 0;
double x = 100;
double y = 700;
double width = 60;
double height = 15;
var choices = new List<string>() { "Car", "Bike", "Airplane" };
// Create combobox form field
var comboboxForm = new ComboboxFormField(name, value, pageIndex, x, y, width, height, choices);
// Add form
pdf.Form.Add(comboboxForm);
pdf.SaveAs("addComboboxForm.pdf");Imports IronPdf
Imports IronSoftware.Forms
Imports System.Collections.Generic
' Instantiate ChromePdfRenderer
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h2>Combobox Form Field</h2>")
' Configure required parameters
Private name As String = "combobox"
Private value As String = "Car"
Private pageIndex As UInteger = 0
Private x As Double = 100
Private y As Double = 700
Private width As Double = 60
Private height As Double = 15
Private choices = New List(Of String)() From {"Car", "Bike", "Airplane"}
' Create combobox form field
Private comboboxForm = New ComboboxFormField(name, value, pageIndex, x, y, width, height, choices)
' Add form
pdf.Form.Add(comboboxForm)
pdf.SaveAs("addComboboxForm.pdf")输出PDF文档
如何创建单选按钮表单?
如何从 HTML 渲染单选按钮?
同一组中的单选按钮包含在一个表单对象中。 使用其名称通过GetRadioGroup()方法检索表单。 SelectedValue属性包含选定的选项值,如果未选择则为"无"。 这反映了 HTML 单选按钮的行为。
using IronPdf;
// Radio buttons HTML
string FormHtml = @"
<html>
<body>
<h2>Editable PDF Form</h2>
Choose your preferred travel type: <br>
<input type='radio' name='traveltype' value='Bike'>
Bike <br>
<input type='radio' name='traveltype' value='Car'>
Car <br>
<input type='radio' name='traveltype' value='Airplane'>
Airplane
</body>
</html>
";
// Instantiate Renderer
ChromePdfRenderer Renderer = new ChromePdfRenderer();
Renderer.RenderingOptions.CreatePdfFormsFromHtml = true;
Renderer.RenderHtmlAsPdf(FormHtml).SaveAs("radioButtomForm.pdf");Imports IronPdf
' Radio buttons HTML
Private FormHtml As String = "
<html>
<body>
<h2>Editable PDF Form</h2>
Choose your preferred travel type: <br>
<input type='radio' name='traveltype' value='Bike'>
Bike <br>
<input type='radio' name='traveltype' value='Car'>
Car <br>
<input type='radio' name='traveltype' value='Airplane'>
Airplane
</body>
</html>
"
' Instantiate Renderer
Private Renderer As New ChromePdfRenderer()
Renderer.RenderingOptions.CreatePdfFormsFromHtml = True
Renderer.RenderHtmlAsPdf(FormHtml).SaveAs("radioButtomForm.pdf")输出PDF文档
如何以编程方式添加无线电表单?
通过代码添加单选按钮表单字段。 使用所需参数实例化一个RadioField对象。 使用PdfForm方法添加表单。 这种方法有助于 合并 PDF 和在各文档中添加一致的表单字段。
using IronPdf;
using IronSoftware.Forms;
// Instantiate ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h2>Editable PDF Form</h2>");
// Configure required parameters
string name = "choice";
string value = "yes";
uint pageIndex = 0;
double x = 100;
double y = 700;
double width = 15;
double height = 15;
// Create the first radio form
var yesRadioform = new RadioFormField(name, value, pageIndex, x, y, width, height);
value = "no";
x = 200;
// Create the second radio form
var noRadioform = new RadioFormField(name, value, pageIndex, x, y, width, height);
pdf.Form.Add(yesRadioform);
pdf.Form.Add(noRadioform);
pdf.SaveAs("addRadioForm.pdf");Imports IronPdf
Imports IronSoftware.Forms
' Instantiate ChromePdfRenderer
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h2>Editable PDF Form</h2>")
' Configure required parameters
Private name As String = "choice"
Private value As String = "yes"
Private pageIndex As UInteger = 0
Private x As Double = 100
Private y As Double = 700
Private width As Double = 15
Private height As Double = 15
' Create the first radio form
Private yesRadioform = New RadioFormField(name, value, pageIndex, x, y, width, height)
value = "no"
x = 200
' Create the second radio form
Dim noRadioform = New RadioFormField(name, value, pageIndex, x, y, width, height)
pdf.Form.Add(yesRadioform)
pdf.Form.Add(noRadioform)
pdf.SaveAs("addRadioForm.pdf")输出PDF文档
使用AddLabel()方法为单选按钮添加标签。 了解更多关于如何在PDF上绘制文本和位图。
如何在 PDF 中添加图像表单?
仅通过代码添加图像表单字段。 使用所需参数实例化一个ImageField对象。 使用PdfForm方法添加表单。 图像表单可以很好地实现照片识别或签名捕捉功能。
using IronPdf;
using IronSoftware.Forms;
// Instantiate ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h2>Editable PDF Form</h2>");
// Configure required parameters
string name = "image1";
uint pageIndex = 0;
double x = 100;
double y = 600;
double width = 200;
double height = 200;
// Create the image form
ImageFormField imageForm = new ImageFormField(name, pageIndex, x, y, width, height);
pdf.Form.Add(imageForm);
pdf.SaveAs("addImageForm.pdf");Imports IronPdf
Imports IronSoftware.Forms
' Instantiate ChromePdfRenderer
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h2>Editable PDF Form</h2>")
' Configure required parameters
Private name As String = "image1"
Private pageIndex As UInteger = 0
Private x As Double = 100
Private y As Double = 600
Private width As Double = 200
Private height As Double = 200
' Create the image form
Private imageForm As New ImageFormField(name, pageIndex, x, y, width, height)
pdf.Form.Add(imageForm)
pdf.SaveAs("addImageForm.pdf")输出PDF文档
输出PDF文件:查看图像表单PDF。 浏览器不支持图像表单; 在 Adobe Acrobat 中打开以测试此功能。

如何创建未签名的签名表单?
通过创建签名对象插入未签名或空签名字段。 访问目标PDF的AddSignatureField()方法插入签名。 导出带有空签名字段的 PDF。 这样就可以实现数字签名,以后可以用实际签名数据进行填充。
using IronPdf;
using IronSoftware.Forms;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>testing</h1>");
// Configure required parameters
string name = "cert";
uint pageIndex = 0;
double x = 100;
double y = 600;
double width = 300;
double height = 100;
// Create signature
SignatureFormField signature = new SignatureFormField(name, pageIndex, x, y, width, height);
// Add signature
pdf.Form.Add(signature);
pdf.SaveAs("signature.pdf");Imports IronPdf
Imports IronSoftware.Forms
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>testing</h1>")
' Configure required parameters
Private name As String = "cert"
Private pageIndex As UInteger = 0
Private x As Double = 100
Private y As Double = 600
Private width As Double = 300
Private height As Double = 100
' Create signature
Private signature As New SignatureFormField(name, pageIndex, x, y, width, height)
' Add signature
pdf.Form.Add(signature)
pdf.SaveAs("signature.pdf")输出PDF文档
输出PDF文件:signature.pdf。 浏览器不支持签名表单; 在 Adobe Acrobat 中打开以测试此功能。 要增强安全性,请探索 使用 HSM 签署 PDF 。

TextFormField
Add
Form
DrawText
CreatePdfFormsFromHtml
CheckboxFormField
Add
Form
ComboboxFormField
Add
Form
FindFormField
Value
RadioFormField
Add
Form
DrawText
ImageFormField
Add
Form
Form
Add
学习以编程方式填写和编辑 PDF 表单:" 如何填写和编辑 PDF 表单"。 有关全面的表单安全性,请参阅我们的PDF权限和密码指南。
准备探索更多内容? 查看我们的教程:Sign and Secure PDFs
常见问题解答
如何用 C# 创建可填写的 PDF 表单?
您可以使用 IronPDF 的综合 API 在 C# 中创建可填写的 PDF 表单。只需使用 ChromePdfRenderer,并将 CreatePdfFormsFromHtml 选项设置为 true,然后渲染包含输入标签、文本区域、复选框和单选按钮等表单元素的 HTML。IronPDF 会自动将这些 HTML 表单元素转换为交互式 PDF 表单字段。
我可以在 PDF 中添加哪些类型的表单字段?
IronPDF 支持添加各种表单字段类型,包括文本输入、文本区域、复选框、单选按钮、组合框、图像表单和数字签名。您可以通过渲染 HTML 表单或使用 IronPDF 的 API 以编程方式添加表单字段来创建这些表单字段。
不使用 HTML 能否创建 PDF 表单?
是的,IronPDF 允许您不使用 HTML 以编程方式添加表单字段。您可以实例化带有所需参数的表单字段对象(如 TextFormField),然后使用表单属性的 Add 方法将其直接添加到 PDF 文档中。这种方法是编辑现有 PDF 或动态创建表单时的理想选择。
在将 HTML 转换为 PDF 时,如何启用表单创建功能?
要在使用 IronPDF 将 HTML 转换为 PDF 的过程中创建表格,请将 ChromePdfRenderer 的 RenderingOptions 中的 CreatePdfFormsFromHtml 属性设置为 true。这样,IronPDF 就会在渲染过程中将 HTML 表单元素转换为交互式 PDF 表单字段。
创建简单 PDF 表单的最快方法是什么?
using IronPDF 创建 PDF 表单的最快方法只需一行代码:新建 ChromePdfRenderer,并将 CreatePdfFormsFromHtml 设置为 true,然后调用 RenderHtmlAsPdf 并保存结果。这会自动将 HTML 输入元素转换为可填写的 PDF 表单字段。
Is it possible to integrate images as form fields in PDFs with IronPDF?
Yes, IronPDF allows you to integrate images as form fields by using the `ImageFormField` object. This feature is useful for photo identification or signature captures.
How do I manage checkbox and combobox forms in PDFs with IronPDF?
Checkbox and combobox forms can be created from HTML or programmatically using IronPDF. For checkboxes, instantiate a `CheckboxFormField`, and for comboboxes, use the `ComboboxFormField` object, then add these forms using the `Add()` method.
What options does IronPDF offer for enhancing the security and compliance of PDF forms?
IronPDF supports features such as digital signing, redaction, encryption, and protection to enhance the security and compliance of PDF forms, reducing yearly costs associated with PDF handling.
Can I dynamically populate form fields like comboboxes in PDFs with IronPDF?
Yes, IronPDF allows you to dynamically populate form fields such as comboboxes by using methods to extract data and set `ComboboxFormField` choices programmatically or through HTML rendering.
How can I ensure compatibility of interactive PDF forms across different PDF readers?
When using IronPDF to create interactive forms, it's advised to test features like signature and image fields in Adobe Acrobat, as some browsers may not support all interactive elements. IronPDF's comprehensive features ensure broad compatibility.

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。