快速入门:使用IronPDF填写 PDF 表单
using IronPDF 填写和编辑 PDF 表单,只需几步即可完成。 本指南演示了如何查找表单字段、修改其值并保存更新后的文档。 非常适合将 PDF 表单编辑无缝集成到您的 C# 应用程序中。
1Install IronPDF with NuGet Package Manager
PM > Install-Package IronPdf
Install-Package IronPdf
2复制并运行这段代码。
var pdf = IronPdf.PdfDocument.FromFile("form.pdf");var field = pdf.Form.FindFormField("nameField");field.Value = "John Doe";pdf.SaveAs("updated_form.pdf");
var pdf = IronPdf.PdfDocument.FromFile("form.pdf");
var field = pdf.Form.FindFormField("nameField");
field.Value = "John Doe";
pdf.SaveAs("updated_form.pdf");
IronPDF 可毫不费力地编辑 PDF 文档中各种类型的现有表单字段。 无论是自动化文档工作流、创建批量表单提交,还是构建交互式 PDF 应用程序,IronPDF 的表单编辑功能都能提供您所需的灵活性和控制性。 该库支持所有标准的 PDF 表单字段类型,并能与现有的 .NET Standard 应用程序顺利集成,是企业文档管理系统的理想选择。
对于需要数字签名的复杂表单,IronPDF 可提供表单编辑和文档签名工作流之间的无缝集成。 您还可以设置 PDF 密码和权限,以保护您编辑的表单免受未经授权的修改。
using IronPdf;PdfDocument pdf = PdfDocument.FromFile("textAreaAndInputForm.pdf");// Set text input form valuespdf.Form.FindFormField("firstname").Value = "John";pdf.Form.FindFormField("lastname").Value = "Smith";// Set text area form valuespdf.Form.FindFormField("address").Value = "Iron Software LLC\r\n205 N. Michigan Ave.";pdf.SaveAs("textAreaAndInputFormEdited.pdf");
using IronPdf;
PdfDocument pdf = PdfDocument.FromFile("textAreaAndInputForm.pdf");
// Set text input form values
pdf.Form.FindFormField("firstname").Value = "John";
pdf.Form.FindFormField("lastname").Value = "Smith";
// Set text area form values
pdf.Form.FindFormField("address").Value = "Iron Software LLC\r\n205 N. Michigan Ave.";
pdf.SaveAs("textAreaAndInputFormEdited.pdf");
ImportsIronPdfDim pdf AsPdfDocument = PdfDocument.FromFile("textAreaAndInputForm.pdf")' Set text input form valuespdf.Form.FindFormField("firstname").Value = "John"pdf.Form.FindFormField("lastname").Value = "Smith"' Set text area form valuespdf.Form.FindFormField("address").Value = "Iron Software LLC" & vbCrLf & "205 N. Michigan Ave."pdf.SaveAs("textAreaAndInputFormEdited.pdf")
Imports IronPdf
Dim pdf As PdfDocument = PdfDocument.FromFile("textAreaAndInputForm.pdf")
' Set text input form values
pdf.Form.FindFormField("firstname").Value = "John"
pdf.Form.FindFormField("lastname").Value = "Smith"
' Set text area form values
pdf.Form.FindFormField("address").Value = "Iron Software LLC" & vbCrLf & "205 N. Michigan Ave."
pdf.SaveAs("textAreaAndInputFormEdited.pdf")
请注意,当填写多行文本区域时,可以使用\r\n进行换行,以正确格式化地址、评论或任何其他多行内容。 表单字段会根据原始 PDF 中定义的字段尺寸自动处理文本换行。
了解如何使用这些交互式表单元素对于构建动态 PDF 应用程序至关重要。 如果您需要从头开始创建 PDF 表单,IronPDF 提供了全面的工具,可为您的文档添加新的复选框、组合框和其他表单元素。
using IronPdf;using System;PdfDocument pdf = PdfDocument.FromFile("checkboxAndComboboxForm.pdf");var checkboxForm = pdf.Form.FindFormField("taskCompleted");// Check the checkbox formcheckboxForm.Value = "Yes";var comboboxForm = pdf.Form.FindFormField("priority");// Set the combobox valuecomboboxForm.Value = "Low";// Print out all the available choicesforeach (var choice in comboboxForm.Choices){Console.WriteLine(choice);}pdf.SaveAs("checkboxAndComboboxFormEdited.pdf");
using IronPdf;
using System;
PdfDocument pdf = PdfDocument.FromFile("checkboxAndComboboxForm.pdf");
var checkboxForm = pdf.Form.FindFormField("taskCompleted");
// Check the checkbox form
checkboxForm.Value = "Yes";
var comboboxForm = pdf.Form.FindFormField("priority");
// Set the combobox value
comboboxForm.Value = "Low";
// Print out all the available choices
foreach (var choice in comboboxForm.Choices)
{
Console.WriteLine(choice);
}
pdf.SaveAs("checkboxAndComboboxFormEdited.pdf");
ImportsIronPdfImportsSystemPrivate pdf AsPdfDocument = PdfDocument.FromFile("checkboxAndComboboxForm.pdf")Private checkboxForm = pdf.Form.FindFormField("taskCompleted")' Check the checkbox formcheckboxForm.Value = "Yes"Dim comboboxForm = pdf.Form.FindFormField("priority")' Set the combobox valuecomboboxForm.Value = "Low"' Print out all the available choicesFor Each choice In comboboxForm.ChoicesConsole.WriteLine(choice)Next choicepdf.SaveAs("checkboxAndComboboxFormEdited.pdf")
Imports IronPdf
Imports System
Private pdf As PdfDocument = PdfDocument.FromFile("checkboxAndComboboxForm.pdf")
Private checkboxForm = pdf.Form.FindFormField("taskCompleted")
' Check the checkbox form
checkboxForm.Value = "Yes"
Dim comboboxForm = pdf.Form.FindFormField("priority")
' Set the combobox value
comboboxForm.Value = "Low"
' Print out all the available choices
For Each choice In comboboxForm.Choices
Console.WriteLine(choice)
Next choice
pdf.SaveAs("checkboxAndComboboxFormEdited.pdf")
using IronPdf;using System;PdfDocument pdf = PdfDocument.FromFile("radioButtomForm.pdf");var radioForm = pdf.Form.FindFormField("traveltype");// Set the radio button valueradioForm.Value = "Airplane";// Print out all the available choicesforeach(var annotation in radioForm.Annotations){Console.WriteLine(annotation.OnAppearance);}pdf.SaveAs("radioButtomFormEdited.pdf");
using IronPdf;
using System;
PdfDocument pdf = PdfDocument.FromFile("radioButtomForm.pdf");
var radioForm = pdf.Form.FindFormField("traveltype");
// Set the radio button value
radioForm.Value = "Airplane";
// Print out all the available choices
foreach(var annotation in radioForm.Annotations)
{
Console.WriteLine(annotation.OnAppearance);
}
pdf.SaveAs("radioButtomFormEdited.pdf");
ImportsIronPdfImportsSystemPrivate pdf AsPdfDocument = PdfDocument.FromFile("radioButtomForm.pdf")Private radioForm = pdf.Form.FindFormField("traveltype")' Set the radio button valueradioForm.Value = "Airplane"' Print out all the available choicesFor Each annotation In radioForm.AnnotationsConsole.WriteLine(annotation.OnAppearance)Next annotationpdf.SaveAs("radioButtomFormEdited.pdf")
Imports IronPdf
Imports System
Private pdf As PdfDocument = PdfDocument.FromFile("radioButtomForm.pdf")
Private radioForm = pdf.Form.FindFormField("traveltype")
' Set the radio button value
radioForm.Value = "Airplane"
' Print out all the available choices
For Each annotation In radioForm.Annotations
Console.WriteLine(annotation.OnAppearance)
Next annotation
pdf.SaveAs("radioButtomFormEdited.pdf")
删除表单字段后,您可能希望以各种格式保存和导出 PDF 文档。 IronPDF 支持多种导出选项,包括保存到磁盘、内存流或直接通过网络应用程序提供 PDF。
using IronPdf;using IronSoftware.Forms;PdfDocument pdf = PdfDocument.FromFile("textAreaAndInputForm.pdf");// Remove FormIFormField targetForm = pdf.Form.FindFormField("firstname");pdf.Form.Remove(targetForm);pdf.SaveAs("removedForm.pdf");
using IronPdf;
using IronSoftware.Forms;
PdfDocument pdf = PdfDocument.FromFile("textAreaAndInputForm.pdf");
// Remove Form
IFormField targetForm = pdf.Form.FindFormField("firstname");
pdf.Form.Remove(targetForm);
pdf.SaveAs("removedForm.pdf");
IronPDF 让用 C# 填写 PDF 表单变得简单。使用 PdfDocument.FromFile 方法加载 PDF,然后使用 FindFormField 按名称查找特定的表单字段。一旦找到,您就可以修改字段的 Value 属性并保存更新后的文档。IronPDF 支持所有标准的 PDF 表单字段类型,包括文本字段、复选框、单选按钮和组合框。
可以编辑哪些类型的 PDF 表单字段?
IronPDF 支持编辑所有标准的 PDF 表单字段类型,包括文本区域、输入字段、复选框、单选按钮和组合框。该库提供了一致的应用程序接口(API),可通过简单的属性分配修改每种字段类型,从而轻松实现不同字段类型的表单自动填写。
如何查找和修改特定表单字段?
using IronPDF 的 FindFormField 方法,以字段名作为参数,查找 PDF 文档中的任何表单字段。找到后,您可以通过设置字段的 Value 属性来修改字段。例如:var field = pdf.Form.FindFormField("nameField"); field.Value = "John Doe";
能否保护已编辑的 PDF 表单免受未经授权的更改?
是的,IronPDF 允许您在编辑表单后设置 PDF 密码和权限。这可确保您填写的表单安全,并防止未经授权的修改。该库还支持数字签名,以实现额外的文档验证和完整性。
是否可以批量编辑 PDF 表单?
IronPDF 是批量表单处理和自动化的理想选择。您可以以编程方式遍历多个 PDF 文件,用数据库或其他来源的数据填充表单,并保存完成的文档。这使得 IronPDF 成为企业文档管理系统和自动化工作流程应用程序的完美选择。
我能否在编辑表单时更新文档元数据?
是的,IronPDF 允许您在更新表单值的同时更新 PDF 元数据。这对于跟踪表单提交日期、维护审计跟踪或为编辑的文档添加自定义属性以更好地组织和搜索非常有用。
How can IronPDF enhance security for PDF forms?
IronPDF allows you to implement security measures such as encrypting documents and setting permissions, helping to protect form data throughout its lifecycle within your application.
Is IronPDF suitable for creating fully interactive PDF applications in .NET?
Yes, IronPDF integrates into .NET projects, making it ideal for building interactive PDF applications with features that support all standard form field types and customizable PDF functionalities.
What is the recommended method for filling PDF forms with IronPDF?
The recommended method involves downloading IronPDF, loading the PDF with `FromFile`, modifying form fields using `FindFormField` and setting their `Value`, then saving the document with `SaveAs`.
How does IronPDF handle multiline text areas in forms?
For multiline text areas, IronPDF allows you to use '\r\n' for line breaks, ensuring the text wraps correctly based on the field dimensions in the original PDF.