C#中的PDF表单:创建、填写和处理交互式表单

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronPDF for .NET 中的 C# .NET PDF AcroForms 可让开发人员对交互式表单字段进行全面的编程控制,从读取和填写现有政府表单到从 HTML 创建新表单、提取已提交数据以及批量处理数千份文档。 IronPDF 可处理所有标准字段类型,包括文本输入、复选框、单选按钮、下拉菜单和签名字段,让您可以使用已有的 HTML 和 CSS 技能自动完成整个 PDF 表单生命周期

as-heading:2(TL;DR:快速入门指南)

本教程涵盖在 C# .NET 中创建、填充和处理交互式 PDF 表单,从读取现有表单字段到批量生成完成文档。

  • 适用对象:为政府、医疗保健、金融或人力资源流程自动化 PDF 表单工作流程的 .NET 开发人员。
  • 您将构建的内容:读取和填写 PDF 表单(文本、复选框、单选按钮、下拉菜单)、从 HTML 创建 PDF 表单、提取提交的数据、验证输入、扁平化表单以锁定值、自动处理 W-9 以及批量生成完成的表单。
  • 运行环境: .NET 10、.NET 8 LTS、.NET Framework 4.6.2+ 和 .NET Standard 2.0。输出可在 Adobe Acrobat、Preview 和所有主流 PDF 阅读器中运行。
  • 何时使用此方法:当您的应用程序需要大规模填写、创建或处理 PDF 表单而无需手动输入数据时。
  • 技术上的原因: AcroForm 字段是结构化对象,可以通过编程方式进行读写。 IronPdf 还可将 HTML 表单元素转换为 AcroForm 的对应元素,以实现充分的设计灵活性。

只需几行代码,即可填写您的第一个 PDF 表单:

Nuget Icon立即开始使用 NuGet 创建 PDF 文件:

  1. 使用 NuGet 包管理器安装 IronPDF

    PM > Install-Package IronPdf

  2. 复制并运行这段代码。

    var pdf = IronPdf.PdfDocument.FromFile("form.pdf");
    pdf.Form.FindFormField("name").Value = "John Smith";
    pdf.SaveAs("filled-form.pdf");
  3. 部署到您的生产环境中进行测试

    立即开始在您的项目中使用 IronPDF,免费试用!
    arrow pointer

购买或注册 IronPDF 30 天试用版后,请在应用程序的开头添加许可证密钥。

IronPdf.License.LicenseKey = "KEY";
IronPdf.License.LicenseKey = "KEY";
Imports IronPdf

IronPdf.License.LicenseKey = "KEY"
$vbLabelText   $csharpLabel

今天在您的项目中使用 IronPDF,免费试用。

第一步:
green arrow pointer
NuGet 使用 NuGet 安装

PM >  Install-Package IronPdf

IronPDF 上查看 NuGet 快速安装。超过 1000 万次下载,它正以 C# 改变 PDF 开发。 您也可以下载 DLLWindows 安装程序

as-heading:2(目录)

为什么交互式 PDF 表单在 2025 年仍然必不可少?

尽管网络表单和基于云的文档解决方案不断涌现,但 PDF 表单仍在关键业务流程中占据主导地位。 政府机构、医疗保健提供商、金融机构和法律公司都非常依赖 PDF 格式的官方文档。 仅在医疗保健领域,就有 88% 的病历以 PDF 格式存储或共享。 全球税务机关每年要处理数十亿份表格提交,其中绝大多数是 PDF 电子文档。

既然已经有了看似更现代的替代方案,为什么这种格式仍然存在?答案在于法律要求、通用兼容性和文档完整性的结合。 无论使用何种设备或操作系统查看 PDF 表单,都要保持准确的格式。 在 Windows 台式机上填写的表格,在 Mac 笔记本电脑上打开或在政府办公室打印时,都会显示出相同的效果。在处理法律文件、监管文件和官方记录时,这种一致性非常重要,因为精确的格式会影响有效性。

网络表单当然有它的用武之地,但由于一些原因,它们不能完全取代 PDF 表单。 许多监管机构特别要求提交 PDF 文件。 法律程序通常要求文件格式能够在多年后可靠地存档和验证。 对于现场工作人员、偏远地区以及无法保证互联网连接的情况,离线访问仍然非常重要。 此外,PDF 表单可以进行数字签名、加密和跟踪,以提供合规性所必需的审计跟踪。

PDF 软件市场反映了这一持续的需求,其价值在 2024 年达到 48 亿美元,预计到 2030 年将以每年 8% 到 11% 的速度增长。对于 .NET 开发人员来说,这既是一个技术挑战,也是一个重大机遇。 掌握 PDF 表单自动化意味着能够简化涉及各行各业数百万份文档的工作流程。


什么是 PDF AcroForms,它们如何工作?

AcroForms 代表 PDF 规范中内置的标准交互式表单技术。 AcroForms 由 Adobe 开发,现已成为 ISO 标准,它允许 PDF 文档包含可填写字段,用户可以使用任何兼容的 PDF 阅读器完成填写。 当您在 Adobe Acrobat 或预览版中打开税表并在字段中输入内容时,您是在与嵌入在该文档中的 AcroForm 元素进行交互。

AcroForm 中的每个表单字段都有几个关键属性。 字段名称是程序用来定位和操作字段的唯一标识符。 字段类型决定了字段接受的输入类型,是文本、复选框、单选按钮、下拉选择还是签名。 value 属性保存字段的当前内容,可以通过编程方式读取或写入。 附加属性可控制外观、验证规则和默认值。

AcroForms 支持几种不同的字段类型,涵盖了大多数数据收集需求。 文本字段接受自由格式文本输入,可配置为单行或多行。 复选框字段表示二进制选择,可以进行分组以允许多个选择。 单选按钮字段在组中一起工作,选择一个选项会自动取消选择其他选项。 组合框和列表框提供了预定义的选择选项。 签名栏为数字签名提供指定区域。 了解这些字段类型及其行为方式对于实现有效的表单自动化至关重要。

AcroForms 的结构非常适合编程操作。 由于每个字段都有一个名称和一个值,因此您可以遍历文档中的所有字段,读取其当前值,并使用简单的代码设置新值。 这种可预测的结构使本指南中涵盖的自动化工作流程成为可能。


如何打开 PDF 并列出所有表单字段?

在以编程方式填写 PDF 表单之前,您需要了解其结构。 打开文档并枚举其字段,就会显示用于设置值的字段名以及字段类型和任何现有内容。 在使用第三方表单时,如果没有关于内部字段结构的文档,这一发现步骤就显得尤为重要。

输入PDF

在本示例中,我们将使用一个名为 application-form.pdf 的现有 PDF 表单。 这可能是任何包含交互式表单字段的 PDF:政府表格、您所在组织的应用程序模板或您需要处理的第三方文档。 下面的代码将加载该文件并遍历其所有交互式表单字段。

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-forms-csharp/list-form-fields.cs
using IronPdf;

// Load an existing PDF form document
PdfDocument pdfForm = PdfDocument.FromFile("application-form.pdf");

// Access the form object and iterate through all fields
foreach (var field in pdfForm.Form)
{
    // Access field properties: field.Name, field.Type, field.Value, field.ReadOnly
}
Imports IronPdf

' Load an existing PDF form document
Dim pdfForm As PdfDocument = PdfDocument.FromFile("application-form.pdf")

' Access the form object and iterate through all fields
For Each field In pdfForm.Form
    ' Access field properties: field.Name, field.Type, field.Value, field.ReadOnly
Next
$vbLabelText   $csharpLabel

PdfDocument.FromFile() 方法将 PDF 加载到内存中,Form 集合可访问文档中的每个交互式字段。 每个字段都暴露了以下属性:Name(用于编程访问的唯一标识符)、Type(文本字段、复选框、单选按钮、下拉或签名)、Value(当前内容)和ReadOnly(字段是否可以修改)。 针对不熟悉的表单运行此枚举,您就能获得所需的完整字段清单,从而编写出能正确填写表单的代码。

对于字段较多的表单,您可能需要按类型筛选或搜索特定字段名称。 FindFormField 方法根据字段的确切名称查找字段,如果没有匹配的字段,则会出现异常。 当您不确定某个字段是否存在时,请使用 try-catch 块。

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-forms-csharp/find-form-field.cs
using IronPdf;

PdfDocument pdfForm = PdfDocument.FromFile("employee-onboarding.pdf");

// Find specific fields by name - use try-catch for fields that may not exist
try
{
    var firstNameField = pdfForm.Form.FindFormField("firstName");
    // Use firstNameField.Value to get or set the field value
}
catch (Exception)
{
    // Field not found in this document
}

// For fields you know exist, you can access them directly
var lastNameField = pdfForm.Form.FindFormField("lastName");
var departmentField = pdfForm.Form.FindFormField("department");
Imports IronPdf

Dim pdfForm As PdfDocument = PdfDocument.FromFile("employee-onboarding.pdf")

' Find specific fields by name - use try-catch for fields that may not exist
Try
    Dim firstNameField = pdfForm.Form.FindFormField("firstName")
    ' Use firstNameField.Value to get or set the field value
Catch ex As Exception
    ' Field not found in this document
End Try

' For fields you know exist, you can access them directly
Dim lastNameField = pdfForm.Form.FindFormField("lastName")
Dim departmentField = pdfForm.Form.FindFormField("department")
$vbLabelText   $csharpLabel

有关读取表单字段值的详细信息,请参阅 Extract PDF Form Fields in C# 指南。


如何填充文本字段、复选框和下拉菜单?

一旦您知道了 PDF 表单中的字段名称,只需几行代码即可完成填写。 每个字段类型都接受特定格式的值。 文本字段直接获取字符串值。 复选框的选中状态用 "Yes "表示,未选中状态用 "No "表示。 单选按钮接受所选选项的值。 下拉字段可接受其预定义选项列表中的任何值。

输入PDF

我们将处理一份客户注册表(customer-registration.pdf),其中包含几种字段类型:姓名、电子邮件、电话和地址的文本字段; 用于选择加入时事通讯的复选框; 以及用于选择账户类型的下拉菜单。 该示例将加载表单,并在每个字段中填写客户数据样本。

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-forms-csharp/fill-text-checkbox-dropdown.cs
using IronPdf;

// Load the form document
PdfDocument pdf = PdfDocument.FromFile("customer-registration.pdf");

// Fill text fields with customer information
pdf.Form.FindFormField("fullName").Value = "Sarah Johnson";
pdf.Form.FindFormField("email").Value = "sarah.johnson@example.com";
pdf.Form.FindFormField("phone").Value = "(555) 123-4567";
pdf.Form.FindFormField("address").Value = "742 Evergreen Terrace\r\nSpringfield, IL 62701";

// Check a checkbox field
pdf.Form.FindFormField("newsletterOptIn").Value = "Yes";

// Select an option from a dropdown
pdf.Form.FindFormField("accountType").Value = "Premium";

// Save the filled form
pdf.SaveAs("customer-registration-completed.pdf");
Imports IronPdf

' Load the form document
Dim pdf As PdfDocument = PdfDocument.FromFile("customer-registration.pdf")

' Fill text fields with customer information
pdf.Form.FindFormField("fullName").Value = "Sarah Johnson"
pdf.Form.FindFormField("email").Value = "sarah.johnson@example.com"
pdf.Form.FindFormField("phone").Value = "(555) 123-4567"
pdf.Form.FindFormField("address").Value = "742 Evergreen Terrace" & vbCrLf & "Springfield, IL 62701"

' Check a checkbox field
pdf.Form.FindFormField("newsletterOptIn").Value = "Yes"

' Select an option from a dropdown
pdf.Form.FindFormField("accountType").Value = "Premium"

' Save the filled form
pdf.SaveAs("customer-registration-completed.pdf")
$vbLabelText   $csharpLabel

输出示例

FindFormField()方法可根据每个字段的确切名称找到该字段,设置Value属性可填充字段内容。 请注意在地址栏中使用 \r\n 作为换行符。 多行文本区域可以正确解释这些转义序列,使您可以格式化地址、注释和其他跨多行的内容。 对于复选框,值 "是 "和 "否 "可以切换选中状态,而下拉框则接受符合其预定义选项之一的任何值。

使用单选按钮时,需要了解一个组中的所有按钮都共享相同的字段名。 设置值会选择匹配的选项,并取消选择该组中的所有其他选项。

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-forms-csharp/radio-button-group.cs
using IronPdf;

PdfDocument pdf = PdfDocument.FromFile("survey-form.pdf");

// Get a radio button group and examine its options
var satisfactionField = pdf.Form.FindFormField("satisfactionLevel");

// Access available options from the radio button annotations
foreach (var annotation in satisfactionField.Annotations)
{
    // annotation.OnAppearance contains the option value
}

// Select one option from the group
satisfactionField.Value = "Very Satisfied";

pdf.SaveAs("survey-completed.pdf");
Imports IronPdf

Dim pdf As PdfDocument = PdfDocument.FromFile("survey-form.pdf")

' Get a radio button group and examine its options
Dim satisfactionField = pdf.Form.FindFormField("satisfactionLevel")

' Access available options from the radio button annotations
For Each annotation In satisfactionField.Annotations
    ' annotation.OnAppearance contains the option value
Next

' Select one option from the group
satisfactionField.Value = "Very Satisfied"

pdf.SaveAs("survey-completed.pdf")
$vbLabelText   $csharpLabel

下拉字段的工作原理类似。 在设置值之前,您可以通过 "选择 "属性检查可用的选项,确保您的代码只尝试选择有效的选项。

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-forms-csharp/dropdown-choices.cs
using IronPdf;

PdfDocument pdf = PdfDocument.FromFile("order-form.pdf");

var shippingMethod = pdf.Form.FindFormField("shippingMethod");

// Access all available shipping options via shippingMethod.Choices

// Select express shipping
shippingMethod.Value = "Express (2-3 days)";

pdf.SaveAs("order-form-filled.pdf");
Imports IronPdf

Dim pdf As PdfDocument = PdfDocument.FromFile("order-form.pdf")

Dim shippingMethod = pdf.Form.FindFormField("shippingMethod")

' Access all available shipping options via shippingMethod.Choices

' Select express shipping
shippingMethod.Value = "Express (2-3 days)"

pdf.SaveAs("order-form-filled.pdf")
$vbLabelText   $csharpLabel

有关填写所有表单字段类型的综合指南,请参阅 Fill & Edit PDF Forms in C#Programmatically Fill PDF Forms


如何从数据对象和字典中填写表格?

现实世界的应用程序很少硬编码表单值。 在翻译过程中,译文中的数据不是来自数据库、API 响应、用户输入或配置文件,而是来自.NET、Java、Python 或 Node js。 在字典或自定义对象中组织这些数据可使您的表单填写代码更具可维护性和可重用性。

输入PDF

考虑一份贷款申请表(loan-application.pdf),其中包含申请人信息字段:姓名、出生日期、社会保险号、地址字段、就业状况、年收入和条款协议复选框。 以下代码使用存储在 Dictionary<string, string> 中的数据填充表单,其中每个字典键都与表单字段名匹配。

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-forms-csharp/fill-from-dictionary.cs
using IronPdf;
using System.Collections.Generic;

// Form data stored in a dictionary
var formData = new Dictionary<string, string>
{
    { "applicantName", "Michael Chen" },
    { "dateOfBirth", "1985-03-15" },
    { "ssn", "XXX-XX-1234" },
    { "streetAddress", "456 Oak Avenue" },
    { "city", "Portland" },
    { "state", "OR" },
    { "zipCode", "97201" },
    { "employmentStatus", "Full-Time" },
    { "annualIncome", "75000" },
    { "agreeToTerms", "Yes" }
};

PdfDocument pdf = PdfDocument.FromFile("loan-application.pdf");

// Iterate through the dictionary and fill matching fields
foreach (var entry in formData)
{
    var field = pdf.Form.FindFormField(entry.Key);
    if (field != null)
    {
        field.Value = entry.Value;
    }
}

pdf.SaveAs("loan-application-filled.pdf");
Imports IronPdf
Imports System.Collections.Generic

' Form data stored in a dictionary
Dim formData As New Dictionary(Of String, String) From {
    {"applicantName", "Michael Chen"},
    {"dateOfBirth", "1985-03-15"},
    {"ssn", "XXX-XX-1234"},
    {"streetAddress", "456 Oak Avenue"},
    {"city", "Portland"},
    {"state", "OR"},
    {"zipCode", "97201"},
    {"employmentStatus", "Full-Time"},
    {"annualIncome", "75000"},
    {"agreeToTerms", "Yes"}
}

Dim pdf As PdfDocument = PdfDocument.FromFile("loan-application.pdf")

' Iterate through the dictionary and fill matching fields
For Each entry In formData
    Dim field = pdf.Form.FindFormField(entry.Key)
    If field IsNot Nothing Then
        field.Value = entry.Value
    End If
Next

pdf.SaveAs("loan-application-filled.pdf")
$vbLabelText   $csharpLabel

输出示例

基于字典的方法将数据与逻辑清晰地分开。 foreach 循环遍历每个键值对,使用 FindFormField() 查找相应的 PDF 字段,并在字段存在的情况下赋值。 空检查(if (field != null) )可防止字典中的键与文档中的任何字段不匹配时出现异常。 在处理可能包含表单中不存在的额外字段的数据源时,这一点尤其有用。

对于更复杂的情况,您可以定义一个表示表单数据的类,然后使用反射或映射函数将属性值转移到表单字段。

using IronPdf;
using System;

// Define a strongly-typed class for the form data
public class EmployeeRecord
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string EmployeeId { get; set; }
    public string Department { get; set; }
    public DateTime HireDate { get; set; }
    public string JobTitle { get; set; }
    public bool DirectDeposit { get; set; }
}

public class FormFiller
{
    public void FillEmployeeForm(EmployeeRecord employee, string templatePath, string outputPath)
    {
        PdfDocument pdf = PdfDocument.FromFile(templatePath);

        // Map object properties to form fields
        pdf.Form.FindFormField("firstName").Value = employee.FirstName;
        pdf.Form.FindFormField("lastName").Value = employee.LastName;
        pdf.Form.FindFormField("employeeId").Value = employee.EmployeeId;
        pdf.Form.FindFormField("department").Value = employee.Department;
        pdf.Form.FindFormField("hireDate").Value = employee.HireDate.ToString("MM/dd/yyyy");
        pdf.Form.FindFormField("jobTitle").Value = employee.JobTitle;
        pdf.Form.FindFormField("directDeposit").Value = employee.DirectDeposit ? "Yes" : "No";

        pdf.SaveAs(outputPath);
    }
}

// Usage example
class Program
{
    static void Main()
    {
        var employee = new EmployeeRecord
        {
            FirstName = "Jennifer",
            LastName = "Martinez",
            EmployeeId = "EMP-2024-0892",
            Department = "Engineering",
            HireDate = new DateTime(2024, 6, 15),
            JobTitle = "Senior Developer",
            DirectDeposit = true
        };

        var filler = new FormFiller();
        filler.FillEmployeeForm(employee, "new-hire-form.pdf", "jennifer-martinez-onboarding.pdf");
    }
}
using IronPdf;
using System;

// Define a strongly-typed class for the form data
public class EmployeeRecord
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string EmployeeId { get; set; }
    public string Department { get; set; }
    public DateTime HireDate { get; set; }
    public string JobTitle { get; set; }
    public bool DirectDeposit { get; set; }
}

public class FormFiller
{
    public void FillEmployeeForm(EmployeeRecord employee, string templatePath, string outputPath)
    {
        PdfDocument pdf = PdfDocument.FromFile(templatePath);

        // Map object properties to form fields
        pdf.Form.FindFormField("firstName").Value = employee.FirstName;
        pdf.Form.FindFormField("lastName").Value = employee.LastName;
        pdf.Form.FindFormField("employeeId").Value = employee.EmployeeId;
        pdf.Form.FindFormField("department").Value = employee.Department;
        pdf.Form.FindFormField("hireDate").Value = employee.HireDate.ToString("MM/dd/yyyy");
        pdf.Form.FindFormField("jobTitle").Value = employee.JobTitle;
        pdf.Form.FindFormField("directDeposit").Value = employee.DirectDeposit ? "Yes" : "No";

        pdf.SaveAs(outputPath);
    }
}

// Usage example
class Program
{
    static void Main()
    {
        var employee = new EmployeeRecord
        {
            FirstName = "Jennifer",
            LastName = "Martinez",
            EmployeeId = "EMP-2024-0892",
            Department = "Engineering",
            HireDate = new DateTime(2024, 6, 15),
            JobTitle = "Senior Developer",
            DirectDeposit = true
        };

        var filler = new FormFiller();
        filler.FillEmployeeForm(employee, "new-hire-form.pdf", "jennifer-martinez-onboarding.pdf");
    }
}
Imports IronPdf
Imports System

' Define a strongly-typed class for the form data
Public Class EmployeeRecord
    Public Property FirstName As String
    Public Property LastName As String
    Public Property EmployeeId As String
    Public Property Department As String
    Public Property HireDate As DateTime
    Public Property JobTitle As String
    Public Property DirectDeposit As Boolean
End Class

Public Class FormFiller
    Public Sub FillEmployeeForm(employee As EmployeeRecord, templatePath As String, outputPath As String)
        Dim pdf As PdfDocument = PdfDocument.FromFile(templatePath)

        ' Map object properties to form fields
        pdf.Form.FindFormField("firstName").Value = employee.FirstName
        pdf.Form.FindFormField("lastName").Value = employee.LastName
        pdf.Form.FindFormField("employeeId").Value = employee.EmployeeId
        pdf.Form.FindFormField("department").Value = employee.Department
        pdf.Form.FindFormField("hireDate").Value = employee.HireDate.ToString("MM/dd/yyyy")
        pdf.Form.FindFormField("jobTitle").Value = employee.JobTitle
        pdf.Form.FindFormField("directDeposit").Value = If(employee.DirectDeposit, "Yes", "No")

        pdf.SaveAs(outputPath)
    End Sub
End Class

' Usage example
Module Program
    Sub Main()
        Dim employee As New EmployeeRecord With {
            .FirstName = "Jennifer",
            .LastName = "Martinez",
            .EmployeeId = "EMP-2024-0892",
            .Department = "Engineering",
            .HireDate = New DateTime(2024, 6, 15),
            .JobTitle = "Senior Developer",
            .DirectDeposit = True
        }

        Dim filler As New FormFiller()
        filler.FillEmployeeForm(employee, "new-hire-form.pdf", "jennifer-martinez-onboarding.pdf")
    End Sub
End Module
$vbLabelText   $csharpLabel

输出示例


如何在填写之前验证表单数据?

在填充 PDF 表单之前,验证数据有助于及早发现错误,并确保生成的文档符合要求。 虽然 PDF 表单可以包含自己的验证规则,但仅靠这些规则意味着错误只能在处理结束时才会出现。 在 C# 代码中实施验证可让您获得更多控制和更好的错误信息。

using IronPdf;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public class FormValidationResult
{
    public bool IsValid { get; set; }
    public List<string> Errors { get; set; } = new List<string>();
}

public class FormValidator
{
    public FormValidationResult ValidateApplicationData(Dictionary<string, string> formData, PdfDocument pdf)
    {
        var result = new FormValidationResult { IsValid = true };

        // Check that all required fields have values
        var requiredFields = new[] { "applicantName", "email", "phone", "ssn" };
        foreach (var fieldName in requiredFields)
        {
            if (!formData.ContainsKey(fieldName) || string.IsNullOrWhiteSpace(formData[fieldName]))
            {
                result.Errors.Add($"Required field '{fieldName}' is missing or empty");
                result.IsValid = false;
            }
        }

        // Validate email format
        if (formData.ContainsKey("email"))
        {
            var emailPattern = @"^[^@\s]+@[^@\s]+\.[^@\s]+$";
            if (!Regex.IsMatch(formData["email"], emailPattern))
            {
                result.Errors.Add("Email address format is invalid");
                result.IsValid = false;
            }
        }

        // Validate phone number format
        if (formData.ContainsKey("phone"))
        {
            var phonePattern = @"^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$";
            if (!Regex.IsMatch(formData["phone"], phonePattern))
            {
                result.Errors.Add("Phone number format is invalid");
                result.IsValid = false;
            }
        }

        // Verify that form fields exist in the PDF
        foreach (var fieldName in formData.Keys)
        {
            try
            {
                var field = pdf.Form.FindFormField(fieldName);
                // Field exists
            }
            catch
            {
                result.Errors.Add($"Field '{fieldName}' does not exist in the PDF form");
                result.IsValid = false;
            }
        }

        // Check dropdown values against available choices
        if (formData.ContainsKey("state"))
        {
            try
            {
                var stateField = pdf.Form.FindFormField("state");
                if (stateField.Choices != null)
                {
                    bool validChoice = false;
                    foreach (var choice in stateField.Choices)
                    {
                        if (choice == formData["state"])
                        {
                            validChoice = true;
                            break;
                        }
                    }
                    if (!validChoice)
                    {
                        result.Errors.Add($"'{formData["state"]}' is not a valid option for the state field");
                        result.IsValid = false;
                    }
                }
            }
            catch
            {
                // State field doesn't exist, skip validation
            }
        }

        return result;
    }
}

// Usage example
class Program
{
    static void Main()
    {
        var formData = new Dictionary<string, string>
        {
            { "applicantName", "John Doe" },
            { "email", "invalid-email" },
            { "phone", "555-1234" },
            { "state", "XX" }
        };

        PdfDocument pdf = PdfDocument.FromFile("application.pdf");
        var validator = new FormValidator();
        var validationResult = validator.ValidateApplicationData(formData, pdf);

        if (validationResult.IsValid)
        {
            // Proceed with form fill
        }
        else
        {
            // Handle validation errors in validationResult.Errors
        }
    }
}
using IronPdf;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public class FormValidationResult
{
    public bool IsValid { get; set; }
    public List<string> Errors { get; set; } = new List<string>();
}

public class FormValidator
{
    public FormValidationResult ValidateApplicationData(Dictionary<string, string> formData, PdfDocument pdf)
    {
        var result = new FormValidationResult { IsValid = true };

        // Check that all required fields have values
        var requiredFields = new[] { "applicantName", "email", "phone", "ssn" };
        foreach (var fieldName in requiredFields)
        {
            if (!formData.ContainsKey(fieldName) || string.IsNullOrWhiteSpace(formData[fieldName]))
            {
                result.Errors.Add($"Required field '{fieldName}' is missing or empty");
                result.IsValid = false;
            }
        }

        // Validate email format
        if (formData.ContainsKey("email"))
        {
            var emailPattern = @"^[^@\s]+@[^@\s]+\.[^@\s]+$";
            if (!Regex.IsMatch(formData["email"], emailPattern))
            {
                result.Errors.Add("Email address format is invalid");
                result.IsValid = false;
            }
        }

        // Validate phone number format
        if (formData.ContainsKey("phone"))
        {
            var phonePattern = @"^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$";
            if (!Regex.IsMatch(formData["phone"], phonePattern))
            {
                result.Errors.Add("Phone number format is invalid");
                result.IsValid = false;
            }
        }

        // Verify that form fields exist in the PDF
        foreach (var fieldName in formData.Keys)
        {
            try
            {
                var field = pdf.Form.FindFormField(fieldName);
                // Field exists
            }
            catch
            {
                result.Errors.Add($"Field '{fieldName}' does not exist in the PDF form");
                result.IsValid = false;
            }
        }

        // Check dropdown values against available choices
        if (formData.ContainsKey("state"))
        {
            try
            {
                var stateField = pdf.Form.FindFormField("state");
                if (stateField.Choices != null)
                {
                    bool validChoice = false;
                    foreach (var choice in stateField.Choices)
                    {
                        if (choice == formData["state"])
                        {
                            validChoice = true;
                            break;
                        }
                    }
                    if (!validChoice)
                    {
                        result.Errors.Add($"'{formData["state"]}' is not a valid option for the state field");
                        result.IsValid = false;
                    }
                }
            }
            catch
            {
                // State field doesn't exist, skip validation
            }
        }

        return result;
    }
}

// Usage example
class Program
{
    static void Main()
    {
        var formData = new Dictionary<string, string>
        {
            { "applicantName", "John Doe" },
            { "email", "invalid-email" },
            { "phone", "555-1234" },
            { "state", "XX" }
        };

        PdfDocument pdf = PdfDocument.FromFile("application.pdf");
        var validator = new FormValidator();
        var validationResult = validator.ValidateApplicationData(formData, pdf);

        if (validationResult.IsValid)
        {
            // Proceed with form fill
        }
        else
        {
            // Handle validation errors in validationResult.Errors
        }
    }
}
Imports IronPdf
Imports System
Imports System.Collections.Generic
Imports System.Text.RegularExpressions

Public Class FormValidationResult
    Public Property IsValid As Boolean
    Public Property Errors As List(Of String) = New List(Of String)()
End Class

Public Class FormValidator
    Public Function ValidateApplicationData(formData As Dictionary(Of String, String), pdf As PdfDocument) As FormValidationResult
        Dim result As New FormValidationResult With {.IsValid = True}

        ' Check that all required fields have values
        Dim requiredFields = New String() {"applicantName", "email", "phone", "ssn"}
        For Each fieldName In requiredFields
            If Not formData.ContainsKey(fieldName) OrElse String.IsNullOrWhiteSpace(formData(fieldName)) Then
                result.Errors.Add($"Required field '{fieldName}' is missing or empty")
                result.IsValid = False
            End If
        Next

        ' Validate email format
        If formData.ContainsKey("email") Then
            Dim emailPattern = "^[^@\s]+@[^@\s]+\.[^@\s]+$"
            If Not Regex.IsMatch(formData("email"), emailPattern) Then
                result.Errors.Add("Email address format is invalid")
                result.IsValid = False
            End If
        End If

        ' Validate phone number format
        If formData.ContainsKey("phone") Then
            Dim phonePattern = "^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$"
            If Not Regex.IsMatch(formData("phone"), phonePattern) Then
                result.Errors.Add("Phone number format is invalid")
                result.IsValid = False
            End If
        End If

        ' Verify that form fields exist in the PDF
        For Each fieldName In formData.Keys
            Try
                Dim field = pdf.Form.FindFormField(fieldName)
                ' Field exists
            Catch
                result.Errors.Add($"Field '{fieldName}' does not exist in the PDF form")
                result.IsValid = False
            End Try
        Next

        ' Check dropdown values against available choices
        If formData.ContainsKey("state") Then
            Try
                Dim stateField = pdf.Form.FindFormField("state")
                If stateField.Choices IsNot Nothing Then
                    Dim validChoice = False
                    For Each choice In stateField.Choices
                        If choice = formData("state") Then
                            validChoice = True
                            Exit For
                        End If
                    Next
                    If Not validChoice Then
                        result.Errors.Add($"'{formData("state")}' is not a valid option for the state field")
                        result.IsValid = False
                    End If
                End If
            Catch
                ' State field doesn't exist, skip validation
            End Try
        End If

        Return result
    End Function
End Class

' Usage example
Class Program
    Shared Sub Main()
        Dim formData = New Dictionary(Of String, String) From {
            {"applicantName", "John Doe"},
            {"email", "invalid-email"},
            {"phone", "555-1234"},
            {"state", "XX"}
        }

        Dim pdf As PdfDocument = PdfDocument.FromFile("application.pdf")
        Dim validator As New FormValidator()
        Dim validationResult = validator.ValidateApplicationData(formData, pdf)

        If validationResult.IsValid Then
            ' Proceed with form fill
        Else
            ' Handle validation errors in validationResult.Errors
        End If
    End Sub
End Class
$vbLabelText   $csharpLabel

该验证层可检查是否缺少必填字段、验证常见数据类型的格式模式、确认表单选项中是否存在指定的下拉值,以及验证 PDF 中是否确实存在数据中的所有字段名称。 在尝试填写表格之前抓住这些问题,可以防止部分填写,使调试变得更加容易。


如何在 PDF 表单中创建文本输入字段?

从零开始创建 PDF 表单,您可以完全控制表单的结构和外观。 IronPDF 的 HTML-to-PDF 渲染引擎可以解释标准的 HTML 表单元素,并将其转换为 PDF AcroForm 字段。 通过这种方法,您可以使用熟悉的网络技术(包括用于样式设计的 CSS)来设计表单。 有关创建表格的完整指南,请参阅 Create PDF Forms in C#

以下代码使用 HTML 和 CSS 定义表单结构和样式,创建了一个包含文本输入字段和文本区域的客户反馈表单。 输出的 PDF 文件包含可填写的表单字段,用户可以在任何 PDF 阅读器中完成填写。

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-forms-csharp/create-text-input-form.cs
using IronPdf;

// Define the form layout using HTML with form elements
string formHtml = @"
<!DOCTYPE html>
<html>
<head>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 40px auto;
            padding: 20px;
        }
        h1 {
            color: #333;
            border-bottom: 2px solid #4CAF50;
            padding-bottom: 10px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        input[type='text'], textarea {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        textarea {
            height: 100px;
            resize: none;
        }
        .required::after {
            content: ' *';
            color: red;
        }
    </style>
</head>
<body>
    <h1>Customer Feedback Form</h1>
    <form>
        <div class='form-group'>
            <label class='required'>Full Name</label>
            <input type='text' name='customerName' />
        </div>
        <div class='form-group'>
            <label class='required'>Email Address</label>
            <input type='text' name='customerEmail' />
        </div>
        <div class='form-group'>
            <label>Phone Number</label>
            <input type='text' name='customerPhone' />
        </div>
        <div class='form-group'>
            <label>Order Number</label>
            <input type='text' name='orderNumber' />
        </div>
        <div class='form-group'>
            <label class='required'>Your Feedback</label>
            <textarea name='feedbackText'></textarea>
        </div>
    </form>
</body>
</html>";

// Create the renderer and enable form creation
ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.CreatePdfFormsFromHtml = true;

// Render the HTML to a PDF with interactive form fields
PdfDocument pdf = renderer.RenderHtmlAsPdf(formHtml);
pdf.SaveAs("customer-feedback-form.pdf");
Imports IronPdf

' Define the form layout using HTML with form elements
Dim formHtml As String = "
<!DOCTYPE html>
<html>
<head>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 40px auto;
            padding: 20px;
        }
        h1 {
            color: #333;
            border-bottom: 2px solid #4CAF50;
            padding-bottom: 10px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        input[type='text'], textarea {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        textarea {
            height: 100px;
            resize: none;
        }
        .required::after {
            content: ' *';
            color: red;
        }
    </style>
</head>
<body>
    <h1>Customer Feedback Form</h1>
    <form>
        <div class='form-group'>
            <label class='required'>Full Name</label>
            <input type='text' name='customerName' />
        </div>
        <div class='form-group'>
            <label class='required'>Email Address</label>
            <input type='text' name='customerEmail' />
        </div>
        <div class='form-group'>
            <label>Phone Number</label>
            <input type='text' name='customerPhone' />
        </div>
        <div class='form-group'>
            <label>Order Number</label>
            <input type='text' name='orderNumber' />
        </div>
        <div class='form-group'>
            <label class='required'>Your Feedback</label>
            <textarea name='feedbackText'></textarea>
        </div>
    </form>
</body>
</html>"

' Create the renderer and enable form creation
Dim renderer As New ChromePdfRenderer()
renderer.RenderingOptions.CreatePdfFormsFromHtml = True

' Render the HTML to a PDF with interactive form fields
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(formHtml)
pdf.SaveAs("customer-feedback-form.pdf")
$vbLabelText   $csharpLabel

输出示例

关键设置是 RenderingOptions 上的 CreatePdfFormsFromHtml = true,它指示 ChromePdfRenderer 将 HTML <input><textarea> 元素转换为 PDF AcroForm 对应元素。 如果没有这样的设置,输入内容将呈现为静态的视觉元素,没有交互性。 每个输入元素上的 name 属性将在生成的 PDF 中成为字段名称。 这些是您在以编程方式填写表格时使用的标识符。 选择描述性强、前后一致的名称,使您的表格填写代码具有可读性和可维护性。 有关 HTML 到 IronPDF 转换的更多信息,请参阅 Convert HTML to PDF in C# 教程。


如何在 PDF 表单中添加复选框和单选按钮?

复选框和单选按钮捕捉基于选择的输入。 在 HTML 中,复选框使用 type='checkbox' 而单选按钮使用 type='radio' 。 名称属性相同的单选按钮会自动形成一个互斥组。

该代码段创建了一个活动注册表单,其中有多个复选框用于选择饮食偏好,单选按钮用于选择门票类型。 HTML 结构定义表单布局,CSS 提供样式。

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-forms-csharp/create-checkbox-radio-form.cs
using IronPdf;

string formHtml = @"
<!DOCTYPE html>
<html>
<head>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 40px auto;
            padding: 20px;
        }
        h1 {
            color: #2c3e50;
        }
        h2 {
            color: #34495e;
            font-size: 16px;
            margin-top: 25px;
        }
        .option-group {
            margin: 10px 0;
        }
        .option-group label {
            margin-left: 8px;
            cursor: pointer;
        }
        .checkbox-section {
            background: #f9f9f9;
            padding: 15px;
            border-radius: 5px;
            margin: 15px 0;
        }
    </style>
</head>
<body>
    <h1>Event Registration</h1>
    <form>
        <h2>Select Your Ticket Type</h2>
        <div class='option-group'>
            <input type='radio' name='ticketType' value='General' id='general' />
            <label for='general'>General Admission ($50)</label>
        </div>
        <div class='option-group'>
            <input type='radio' name='ticketType' value='VIP' id='vip' />
            <label for='vip'>VIP Access ($150)</label>
        </div>
        <div class='option-group'>
            <input type='radio' name='ticketType' value='Premium' id='premium' />
            <label for='premium'>Premium Package ($300)</label>
        </div>

        <h2>Which Sessions Will You Attend?</h2>
        <div class='checkbox-section'>
            <div class='option-group'>
                <input type='checkbox' name='sessionMorning' value='Yes' id='morning' />
                <label for='morning'>Morning Keynote (9:00 AM)</label>
            </div>
            <div class='option-group'>
                <input type='checkbox' name='sessionWorkshop' value='Yes' id='workshop' />
                <label for='workshop'>Afternoon Workshop (2:00 PM)</label>
            </div>
            <div class='option-group'>
                <input type='checkbox' name='sessionNetworking' value='Yes' id='networking' />
                <label for='networking'>Evening Networking (6:00 PM)</label>
            </div>
        </div>

        <h2>Dietary Requirements</h2>
        <div class='option-group'>
            <input type='checkbox' name='dietVegetarian' value='Yes' id='vegetarian' />
            <label for='vegetarian'>Vegetarian</label>
        </div>
        <div class='option-group'>
            <input type='checkbox' name='dietVegan' value='Yes' id='vegan' />
            <label for='vegan'>Vegan</label>
        </div>
        <div class='option-group'>
            <input type='checkbox' name='dietGlutenFree' value='Yes' id='glutenfree' />
            <label for='glutenfree'>Gluten-Free</label>
        </div>
    </form>
</body>
</html>";

ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.CreatePdfFormsFromHtml = true;

PdfDocument pdf = renderer.RenderHtmlAsPdf(formHtml);
pdf.SaveAs("event-registration-form.pdf");
Imports IronPdf

Dim formHtml As String = "
<!DOCTYPE html>
<html>
<head>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 40px auto;
            padding: 20px;
        }
        h1 {
            color: #2c3e50;
        }
        h2 {
            color: #34495e;
            font-size: 16px;
            margin-top: 25px;
        }
        .option-group {
            margin: 10px 0;
        }
        .option-group label {
            margin-left: 8px;
            cursor: pointer;
        }
        .checkbox-section {
            background: #f9f9f9;
            padding: 15px;
            border-radius: 5px;
            margin: 15px 0;
        }
    </style>
</head>
<body>
    <h1>Event Registration</h1>
    <form>
        <h2>Select Your Ticket Type</h2>
        <div class='option-group'>
            <input type='radio' name='ticketType' value='General' id='general' />
            <label for='general'>General Admission ($50)</label>
        </div>
        <div class='option-group'>
            <input type='radio' name='ticketType' value='VIP' id='vip' />
            <label for='vip'>VIP Access ($150)</label>
        </div>
        <div class='option-group'>
            <input type='radio' name='ticketType' value='Premium' id='premium' />
            <label for='premium'>Premium Package ($300)</label>
        </div>

        <h2>Which Sessions Will You Attend?</h2>
        <div class='checkbox-section'>
            <div class='option-group'>
                <input type='checkbox' name='sessionMorning' value='Yes' id='morning' />
                <label for='morning'>Morning Keynote (9:00 AM)</label>
            </div>
            <div class='option-group'>
                <input type='checkbox' name='sessionWorkshop' value='Yes' id='workshop' />
                <label for='workshop'>Afternoon Workshop (2:00 PM)</label>
            </div>
            <div class='option-group'>
                <input type='checkbox' name='sessionNetworking' value='Yes' id='networking' />
                <label for='networking'>Evening Networking (6:00 PM)</label>
            </div>
        </div>

        <h2>Dietary Requirements</h2>
        <div class='option-group'>
            <input type='checkbox' name='dietVegetarian' value='Yes' id='vegetarian' />
            <label for='vegetarian'>Vegetarian</label>
        </div>
        <div class='option-group'>
            <input type='checkbox' name='dietVegan' value='Yes' id='vegan' />
            <label for='vegan'>Vegan</label>
        </div>
        <div class='option-group'>
            <input type='checkbox' name='dietGlutenFree' value='Yes' id='glutenfree' />
            <label for='glutenfree'>Gluten-Free</label>
        </div>
    </form>
</body>
</html>"

Dim renderer As New ChromePdfRenderer()
renderer.RenderingOptions.CreatePdfFormsFromHtml = True

Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(formHtml)
pdf.SaveAs("event-registration-form.pdf")
$vbLabelText   $csharpLabel

输出示例

请注意,三个单选按钮共享 name='ticketType' ,创建了一个只能选择一个选项的组。 每个复选框都有一个独特的名称,因为它们代表了可以任意组合选择的独立选项。


如何在 PDF 表单中创建下拉列表?

下拉列表节省空间,同时提供多个预定义选项。 带有 <option> 子元素的 HTML <select> 元素创建了一个 PDF 组合框字段。 用户点击字段,显示选项列表并选择其中一个。

在这里,我们创建了一个职位申请表,其中包含多个下拉菜单,用于选择部门、经验水平、开始日期偏好和工作地点。 每个 <select> 元素都包含预定义的 <option> 值。

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-forms-csharp/create-dropdown-form.cs
using IronPdf;

string formHtml = @"
<!DOCTYPE html>
<html>
<head>
    <style>
        body {
            font-family: 'Segoe UI', Arial, sans-serif;
            max-width: 650px;
            margin: 30px auto;
            padding: 25px;
            background: #fff;
        }
        h1 {
            color: #1a5f7a;
            margin-bottom: 30px;
        }
        .form-row {
            display: flex;
            gap: 20px;
            margin-bottom: 20px;
        }
        .form-group {
            flex: 1;
        }
        label {
            display: block;
            margin-bottom: 6px;
            font-weight: 600;
            color: #333;
        }
        select, input[type='text'] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
            font-size: 14px;
            box-sizing: border-box;
        }
    </style>
</head>
<body>
    <h1>Job Application Form</h1>
    <form>
        <div class='form-row'>
            <div class='form-group'>
                <label>First Name</label>
                <input type='text' name='firstName' />
            </div>
            <div class='form-group'>
                <label>Last Name</label>
                <input type='text' name='lastName' />
            </div>
        </div>

        <div class='form-row'>
            <div class='form-group'>
                <label>Department</label>
                <select name='department'>
                    <option value=''>Select Department</option>
                    <option value='Engineering'>Engineering</option>
                    <option value='Marketing'>Marketing</option>
                    <option value='Sales'>Sales</option>
                    <option value='Human Resources'>Human Resources</option>
                    <option value='Finance'>Finance</option>
                    <option value='Operations'>Operations</option>
                </select>
            </div>
            <div class='form-group'>
                <label>Experience Level</label>
                <select name='experienceLevel'>
                    <option value=''>Select Level</option>
                    <option value='Entry'>Entry Level (0-2 years)</option>
                    <option value='Mid'>Mid Level (3-5 years)</option>
                    <option value='Senior'>Senior (6-10 years)</option>
                    <option value='Executive'>Executive (10+ years)</option>
                </select>
            </div>
        </div>

        <div class='form-row'>
            <div class='form-group'>
                <label>Preferred Start Date</label>
                <select name='startDate'>
                    <option value='Immediate'>Immediately</option>
                    <option value='TwoWeeks'>In 2 weeks</option>
                    <option value='OneMonth'>In 1 month</option>
                    <option value='Flexible'>Flexible</option>
                </select>
            </div>
            <div class='form-group'>
                <label>Work Location Preference</label>
                <select name='workLocation'>
                    <option value='OnSite'>On-Site</option>
                    <option value='Remote'>Fully Remote</option>
                    <option value='Hybrid'>Hybrid</option>
                </select>
            </div>
        </div>
    </form>
</body>
</html>";

ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.CreatePdfFormsFromHtml = true;

PdfDocument pdf = renderer.RenderHtmlAsPdf(formHtml);
pdf.SaveAs("job-application-form.pdf");
Imports IronPdf

Dim formHtml As String = "
<!DOCTYPE html>
<html>
<head>
    <style>
        body {
            font-family: 'Segoe UI', Arial, sans-serif;
            max-width: 650px;
            margin: 30px auto;
            padding: 25px;
            background: #fff;
        }
        h1 {
            color: #1a5f7a;
            margin-bottom: 30px;
        }
        .form-row {
            display: flex;
            gap: 20px;
            margin-bottom: 20px;
        }
        .form-group {
            flex: 1;
        }
        label {
            display: block;
            margin-bottom: 6px;
            font-weight: 600;
            color: #333;
        }
        select, input[type='text'] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
            font-size: 14px;
            box-sizing: border-box;
        }
    </style>
</head>
<body>
    <h1>Job Application Form</h1>
    <form>
        <div class='form-row'>
            <div class='form-group'>
                <label>First Name</label>
                <input type='text' name='firstName' />
            </div>
            <div class='form-group'>
                <label>Last Name</label>
                <input type='text' name='lastName' />
            </div>
        </div>

        <div class='form-row'>
            <div class='form-group'>
                <label>Department</label>
                <select name='department'>
                    <option value=''>Select Department</option>
                    <option value='Engineering'>Engineering</option>
                    <option value='Marketing'>Marketing</option>
                    <option value='Sales'>Sales</option>
                    <option value='Human Resources'>Human Resources</option>
                    <option value='Finance'>Finance</option>
                    <option value='Operations'>Operations</option>
                </select>
            </div>
            <div class='form-group'>
                <label>Experience Level</label>
                <select name='experienceLevel'>
                    <option value=''>Select Level</option>
                    <option value='Entry'>Entry Level (0-2 years)</option>
                    <option value='Mid'>Mid Level (3-5 years)</option>
                    <option value='Senior'>Senior (6-10 years)</option>
                    <option value='Executive'>Executive (10+ years)</option>
                </select>
            </div>
        </div>

        <div class='form-row'>
            <div class='form-group'>
                <label>Preferred Start Date</label>
                <select name='startDate'>
                    <option value='Immediate'>Immediately</option>
                    <option value='TwoWeeks'>In 2 weeks</option>
                    <option value='OneMonth'>In 1 month</option>
                    <option value='Flexible'>Flexible</option>
                </select>
            </div>
            <div class='form-group'>
                <label>Work Location Preference</label>
                <select name='workLocation'>
                    <option value='OnSite'>On-Site</option>
                    <option value='Remote'>Fully Remote</option>
                    <option value='Hybrid'>Hybrid</option>
                </select>
            </div>
        </div>
    </form>
</body>
</html>"

Dim renderer As New ChromePdfRenderer()
renderer.RenderingOptions.CreatePdfFormsFromHtml = True

Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(formHtml)
pdf.SaveAs("job-application-form.pdf")
$vbLabelText   $csharpLabel

输出示例

每个选择元素中的第一个 <option>(空值)将作为占位符提示,如 "选择部门"。 在渲染 PDF 时,这些内容会变成组合框字段,用户可以点击显示完整的选择列表。 在以编程方式填写该表单时,请将字段值设置为与选项值完全匹配,如 "工程 "或 "远程"。


如何在 PDF 表单中添加签名字段?

签名字段指定了用户可以应用数字签名的区域。 虽然 HTML 没有本地签名输入类型,但 IronPDF 允许您以编程方式在任何 PDF 文档中添加签名字段。

下面的代码首先用 HTML 创建了一个保密协议文档,然后在页面的特定位置以编程方式添加了一个 SignatureFormField 字段。 签名栏使用 PDF 坐标(x、y、宽、高,以点为单位)定位。

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-forms-csharp/add-signature-field.cs
using IronPdf;
using IronSoftware.Forms;

// First create the base form using HTML
string formHtml = @"
<!DOCTYPE html>
<html>
<head>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 40px auto;
            padding: 30px;
        }
        h1 {
            text-align: center;
            color: #2c3e50;
        }
        .agreement-text {
            background: #f5f5f5;
            padding: 20px;
            border-radius: 5px;
            margin: 20px 0;
            line-height: 1.6;
        }
        .signature-section {
            margin-top: 40px;
            padding-top: 20px;
            border-top: 1px solid #ddd;
        }
        .signature-line {
            margin-top: 60px;
            border-bottom: 1px solid #333;
            width: 300px;
        }
        .signature-label {
            font-size: 12px;
            color: #666;
            margin-top: 5px;
        }
        .date-field {
            margin-top: 20px;
        }
        .date-field label {
            font-weight: bold;
        }
        .date-field input {
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 4px;
            width: 150px;
        }
    </style>
</head>
<body>
    <h1>Non-Disclosure Agreement</h1>

    <div class='agreement-text'>
        <p>By signing this document, I acknowledge that I have read and understood
        the terms of the Non-Disclosure Agreement dated as of the date signed below.
        I agree to maintain the confidentiality of all proprietary information
        disclosed to me during my engagement with the Company.</p>

        <p>I understand that violation of this agreement may result in legal action
        and that I am bound by these terms for a period of five (5) years from the
        date of signature.</p>
    </div>

    <div class='signature-section'>
        <div class='date-field'>
            <label>Date:</label>
            <input type='text' name='signatureDate' />
        </div>

        <div class='signature-line'></div>
        <div class='signature-label'>Authorized Signature</div>
    </div>
</body>
</html>";

ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.CreatePdfFormsFromHtml = true;

PdfDocument pdf = renderer.RenderHtmlAsPdf(formHtml);

// Add a signature field programmatically
// Parameters: name, page index, x position, y position, width, height
SignatureFormField signatureField = new SignatureFormField(
    "authorizedSignature",  // Field name
    0,                      // Page index (first page)
    72,                     // X position in points from left
    200,                    // Y position in points from bottom
    250,                    // Width in points
    60                      // Height in points
);

pdf.Form.Add(signatureField);
pdf.SaveAs("nda-with-signature.pdf");
Imports IronPdf
Imports IronSoftware.Forms

' First create the base form using HTML
Dim formHtml As String = "
<!DOCTYPE html>
<html>
<head>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 40px auto;
            padding: 30px;
        }
        h1 {
            text-align: center;
            color: #2c3e50;
        }
        .agreement-text {
            background: #f5f5f5;
            padding: 20px;
            border-radius: 5px;
            margin: 20px 0;
            line-height: 1.6;
        }
        .signature-section {
            margin-top: 40px;
            padding-top: 20px;
            border-top: 1px solid #ddd;
        }
        .signature-line {
            margin-top: 60px;
            border-bottom: 1px solid #333;
            width: 300px;
        }
        .signature-label {
            font-size: 12px;
            color: #666;
            margin-top: 5px;
        }
        .date-field {
            margin-top: 20px;
        }
        .date-field label {
            font-weight: bold;
        }
        .date-field input {
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 4px;
            width: 150px;
        }
    </style>
</head>
<body>
    <h1>Non-Disclosure Agreement</h1>

    <div class='agreement-text'>
        <p>By signing this document, I acknowledge that I have read and understood
        the terms of the Non-Disclosure Agreement dated as of the date signed below.
        I agree to maintain the confidentiality of all proprietary information
        disclosed to me during my engagement with the Company.</p>

        <p>I understand that violation of this agreement may result in legal action
        and that I am bound by these terms for a period of five (5) years from the
        date of signature.</p>
    </div>

    <div class='signature-section'>
        <div class='date-field'>
            <label>Date:</label>
            <input type='text' name='signatureDate' />
        </div>

        <div class='signature-line'></div>
        <div class='signature-label'>Authorized Signature</div>
    </div>
</body>
</html>"

Dim renderer As New ChromePdfRenderer()
renderer.RenderingOptions.CreatePdfFormsFromHtml = True

Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(formHtml)

' Add a signature field programmatically
' Parameters: name, page index, x position, y position, width, height
Dim signatureField As New SignatureFormField(
    "authorizedSignature",  ' Field name
    0,                      ' Page index (first page)
    72,                     ' X position in points from left
    200,                    ' Y position in points from bottom
    250,                    ' Width in points
    60                      ' Height in points
)

pdf.Form.Add(signatureField)
pdf.SaveAs("nda-with-signature.pdf")
$vbLabelText   $csharpLabel

输出示例

SignatureFormField 构造函数需要六个参数:字段名称("authorizedSignature")、页面索引(0 表示第一页)以及以点为单位的位置/大小(x=72, y=200, width=250, height=60)。 PDF 坐标以页面左下角为起点,每英寸 72 点。 在支持数字签名的 PDF 阅读器中,签名栏会显示为一个交互区域。 用户可以单击此字段应用基于证书的签名。

有关数字签名(包括基于证书的签名)的更多信息,请参阅 C# PDF 数字签名指南C# PDF 数字签名示例


如何设置表单元素的样式和位置?

从 HTML 创建 PDF 时,CSS 可对表单外观进行广泛控制。 您可以调整颜色、字体、边框、间距和布局,以匹配贵组织的品牌或满足特定的设计要求。

该代码段使用先进的 CSS 功能(包括 Google 字体、渐变背景、用于双栏布局的 CSS 网格,以及带有圆角和焦点状态的自定义表单字段样式)创建了一个视觉上精美的联系请求表单。

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-forms-csharp/styled-form-css.cs
using IronPdf;

string formHtml = @"
<!DOCTYPE html>
<html>
<head>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap');

        body {
            font-family: 'Inter', sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            margin: 0;
            padding: 40px;
            box-sizing: border-box;
        }

        .form-container {
            background: white;
            max-width: 550px;
            margin: 0 auto;
            padding: 40px;
            border-radius: 12px;
            box-shadow: 0 20px 60px rgba(0,0,0,0.3);
        }

        h1 {
            margin: 0 0 10px 0;
            color: #1a1a2e;
            font-weight: 600;
        }

        .subtitle {
            color: #666;
            margin-bottom: 30px;
            font-size: 14px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #333;
            font-size: 14px;
        }

        input[type='text'], select, textarea {
            width: 100%;
            padding: 12px 16px;
            border: 2px solid #e1e1e1;
            border-radius: 8px;
            font-size: 14px;
            transition: border-color 0.3s;
            box-sizing: border-box;
            font-family: 'Inter', sans-serif;
        }

        input[type='text']:focus, select:focus, textarea:focus {
            border-color: #667eea;
            outline: none;
        }

        textarea {
            height: 120px;
            resize: none;
        }

        .checkbox-group {
            display: flex;
            align-items: center;
            gap: 10px;
            padding: 12px;
            background: #f8f9fa;
            border-radius: 8px;
        }

        .checkbox-group input[type='checkbox'] {
            width: 20px;
            height: 20px;
            accent-color: #667eea;
        }

        .two-column {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 15px;
        }

        .footer-note {
            margin-top: 30px;
            padding-top: 20px;
            border-top: 1px solid #eee;
            font-size: 12px;
            color: #888;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class='form-container'>
        <h1>Contact Request</h1>
        <p class='subtitle'>Fill out the form below and we will get back to you within 24 hours.</p>

        <form>
            <div class='two-column'>
                <div class='form-group'>
                    <label>First Name</label>
                    <input type='text' name='firstName' />
                </div>
                <div class='form-group'>
                    <label>Last Name</label>
                    <input type='text' name='lastName' />
                </div>
            </div>

            <div class='form-group'>
                <label>Email Address</label>
                <input type='text' name='email' />
            </div>

            <div class='form-group'>
                <label>Subject</label>
                <select name='subject'>
                    <option value=''>Choose a topic...</option>
                    <option value='General'>General Inquiry</option>
                    <option value='Support'>Technical Support</option>
                    <option value='Sales'>Sales Question</option>
                    <option value='Partnership'>Partnership Opportunity</option>
                </select>
            </div>

            <div class='form-group'>
                <label>Message</label>
                <textarea name='message'></textarea>
            </div>

            <div class='form-group'>
                <div class='checkbox-group'>
                    <input type='checkbox' name='newsletter' value='Yes' id='newsletter' />
                    <label for='newsletter' style='margin: 0; font-weight: normal;'>
                        Subscribe to our newsletter for updates
                    </label>
                </div>
            </div>
        </form>

        <p class='footer-note'>Your information is secure and will never be shared with third parties.</p>
    </div>
</body>
</html>";

ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.CreatePdfFormsFromHtml = true;

// Set page size and margins
renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A4;
renderer.RenderingOptions.MarginTop = 0;
renderer.RenderingOptions.MarginBottom = 0;
renderer.RenderingOptions.MarginLeft = 0;
renderer.RenderingOptions.MarginRight = 0;

PdfDocument pdf = renderer.RenderHtmlAsPdf(formHtml);
pdf.SaveAs("styled-contact-form.pdf");
Imports IronPdf

Dim formHtml As String = "
<!DOCTYPE html>
<html>
<head>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap');

        body {
            font-family: 'Inter', sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            margin: 0;
            padding: 40px;
            box-sizing: border-box;
        }

        .form-container {
            background: white;
            max-width: 550px;
            margin: 0 auto;
            padding: 40px;
            border-radius: 12px;
            box-shadow: 0 20px 60px rgba(0,0,0,0.3);
        }

        h1 {
            margin: 0 0 10px 0;
            color: #1a1a2e;
            font-weight: 600;
        }

        .subtitle {
            color: #666;
            margin-bottom: 30px;
            font-size: 14px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #333;
            font-size: 14px;
        }

        input[type='text'], select, textarea {
            width: 100%;
            padding: 12px 16px;
            border: 2px solid #e1e1e1;
            border-radius: 8px;
            font-size: 14px;
            transition: border-color 0.3s;
            box-sizing: border-box;
            font-family: 'Inter', sans-serif;
        }

        input[type='text']:focus, select:focus, textarea:focus {
            border-color: #667eea;
            outline: none;
        }

        textarea {
            height: 120px;
            resize: none;
        }

        .checkbox-group {
            display: flex;
            align-items: center;
            gap: 10px;
            padding: 12px;
            background: #f8f9fa;
            border-radius: 8px;
        }

        .checkbox-group input[type='checkbox'] {
            width: 20px;
            height: 20px;
            accent-color: #667eea;
        }

        .two-column {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 15px;
        }

        .footer-note {
            margin-top: 30px;
            padding-top: 20px;
            border-top: 1px solid #eee;
            font-size: 12px;
            color: #888;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class='form-container'>
        <h1>Contact Request</h1>
        <p class='subtitle'>Fill out the form below and we will get back to you within 24 hours.</p>

        <form>
            <div class='two-column'>
                <div class='form-group'>
                    <label>First Name</label>
                    <input type='text' name='firstName' />
                </div>
                <div class='form-group'>
                    <label>Last Name</label>
                    <input type='text' name='lastName' />
                </div>
            </div>

            <div class='form-group'>
                <label>Email Address</label>
                <input type='text' name='email' />
            </div>

            <div class='form-group'>
                <label>Subject</label>
                <select name='subject'>
                    <option value=''>Choose a topic...</option>
                    <option value='General'>General Inquiry</option>
                    <option value='Support'>Technical Support</option>
                    <option value='Sales'>Sales Question</option>
                    <option value='Partnership'>Partnership Opportunity</option>
                </select>
            </div>

            <div class='form-group'>
                <label>Message</label>
                <textarea name='message'></textarea>
            </div>

            <div class='form-group'>
                <div class='checkbox-group'>
                    <input type='checkbox' name='newsletter' value='Yes' id='newsletter' />
                    <label for='newsletter' style='margin: 0; font-weight: normal;'>
                        Subscribe to our newsletter for updates
                    </label>
                </div>
            </div>
        </form>

        <p class='footer-note'>Your information is secure and will never be shared with third parties.</p>
    </div>
</body>
</html>"

Dim renderer As New ChromePdfRenderer()
renderer.RenderingOptions.CreatePdfFormsFromHtml = True

' Set page size and margins
renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A4
renderer.RenderingOptions.MarginTop = 0
renderer.RenderingOptions.MarginBottom = 0
renderer.RenderingOptions.MarginLeft = 0
renderer.RenderingOptions.MarginRight = 0

Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(formHtml)
pdf.SaveAs("styled-contact-form.pdf")
$vbLabelText   $csharpLabel

输出示例

HTML 使用了 Google 字体中的 Inter 字体(@import url(...)),为正文应用了渐变背景,并为表单容器设计了圆角和阴影样式。 RenderingOptions 将所有页边距设置为零,并使用 A4 纸张大小以确保渐变填充整个页面。 尽管采用了复杂的样式,但在生成的 PDF 中,所有表单字段仍保持完全交互。 用户可以点击并键入每个字段。

有关 CSS 呈现选项和响应式布局的更多信息,请参阅 Convert HTML to PDF with Responsive CSSUse Rendering Options in PDF


如何从填写的 PDF 表单中提取数据?

用户完成 PDF 表单后,您的应用程序需要检索输入的值,以便进行处理、存储或传输到其他系统。 提取表单数据的过程与填写过程相反,是从字段中读取数值,而不是向字段中写入数值。

输入PDF

想象一下,您收到了一份用户已经填写完毕的申请表(completed-application.pdf)。 这是一种常见的情况,即您通过电子邮件、文件上传或文档管理系统收到提交的表单,并需要提取数据用于数据库存储或进一步处理。 下面的代码会加载已填写的表单,并将所有字段值提取到字典中。

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-forms-csharp/extract-form-data.cs
using IronPdf;
using System.Collections.Generic;

// Load a filled PDF form
PdfDocument filledForm = PdfDocument.FromFile("completed-application.pdf");

// Extract all form data into a dictionary
var extractedData = new Dictionary<string, string>();

foreach (var field in filledForm.Form)
{
    extractedData[field.Name] = field.Value ?? string.Empty;
}

// Access extracted data
// extractedData.GetValueOrDefault("firstName")
// extractedData.GetValueOrDefault("lastName")
// extractedData.GetValueOrDefault("email")
// extractedData.GetValueOrDefault("department")
Imports IronPdf
Imports System.Collections.Generic

' Load a filled PDF form
Dim filledForm As PdfDocument = PdfDocument.FromFile("completed-application.pdf")

' Extract all form data into a dictionary
Dim extractedData As New Dictionary(Of String, String)()

For Each field In filledForm.Form
    extractedData(field.Name) = If(field.Value, String.Empty)
Next

' Access extracted data
' extractedData.TryGetValue("firstName", Nothing)
' extractedData.TryGetValue("lastName", Nothing)
' extractedData.TryGetValue("email", Nothing)
' extractedData.TryGetValue("department", Nothing)
$vbLabelText   $csharpLabel

提取过程遍历 Form 集合,读取每个字段的 NameValue 属性。 空凝聚运算符 (??? string.Empty)可确保空字段或未设置字段返回空字符串而非 null,从而防止在下游处理中出现空引用问题。 提取字典后,可以使用字典名称作为键,轻松访问任何字段值。

对于结构化程度更高的提取,您可以将表单域直接映射到强类型对象。

using IronPdf;
using System;

public class ApplicationData
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public string Department { get; set; }
    public string ExperienceLevel { get; set; }
    public bool AcceptsTerms { get; set; }
}

public class FormDataExtractor
{
    public ApplicationData ExtractApplicationData(string pdfPath)
    {
        PdfDocument pdf = PdfDocument.FromFile(pdfPath);

        return new ApplicationData
        {
            FirstName = GetFieldValue(pdf, "firstName"),
            LastName = GetFieldValue(pdf, "lastName"),
            Email = GetFieldValue(pdf, "email"),
            Phone = GetFieldValue(pdf, "phone"),
            Department = GetFieldValue(pdf, "department"),
            ExperienceLevel = GetFieldValue(pdf, "experienceLevel"),
            AcceptsTerms = GetFieldValue(pdf, "acceptTerms") == "Yes"
        };
    }

    private string GetFieldValue(PdfDocument pdf, string fieldName)
    {
        var field = pdf.Form.FindFormField(fieldName);
        return field?.Value ?? string.Empty;
    }
}

// Usage
class Program
{
    static void Main()
    {
        var extractor = new FormDataExtractor();
        var application = extractor.ExtractApplicationData("submitted-form.pdf");

        // Access application.FirstName, application.LastName, application.Department, etc.
    }
}
using IronPdf;
using System;

public class ApplicationData
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public string Department { get; set; }
    public string ExperienceLevel { get; set; }
    public bool AcceptsTerms { get; set; }
}

public class FormDataExtractor
{
    public ApplicationData ExtractApplicationData(string pdfPath)
    {
        PdfDocument pdf = PdfDocument.FromFile(pdfPath);

        return new ApplicationData
        {
            FirstName = GetFieldValue(pdf, "firstName"),
            LastName = GetFieldValue(pdf, "lastName"),
            Email = GetFieldValue(pdf, "email"),
            Phone = GetFieldValue(pdf, "phone"),
            Department = GetFieldValue(pdf, "department"),
            ExperienceLevel = GetFieldValue(pdf, "experienceLevel"),
            AcceptsTerms = GetFieldValue(pdf, "acceptTerms") == "Yes"
        };
    }

    private string GetFieldValue(PdfDocument pdf, string fieldName)
    {
        var field = pdf.Form.FindFormField(fieldName);
        return field?.Value ?? string.Empty;
    }
}

// Usage
class Program
{
    static void Main()
    {
        var extractor = new FormDataExtractor();
        var application = extractor.ExtractApplicationData("submitted-form.pdf");

        // Access application.FirstName, application.LastName, application.Department, etc.
    }
}
Imports IronPdf
Imports System

Public Class ApplicationData
    Public Property FirstName As String
    Public Property LastName As String
    Public Property Email As String
    Public Property Phone As String
    Public Property Department As String
    Public Property ExperienceLevel As String
    Public Property AcceptsTerms As Boolean
End Class

Public Class FormDataExtractor
    Public Function ExtractApplicationData(pdfPath As String) As ApplicationData
        Dim pdf As PdfDocument = PdfDocument.FromFile(pdfPath)

        Return New ApplicationData With {
            .FirstName = GetFieldValue(pdf, "firstName"),
            .LastName = GetFieldValue(pdf, "lastName"),
            .Email = GetFieldValue(pdf, "email"),
            .Phone = GetFieldValue(pdf, "phone"),
            .Department = GetFieldValue(pdf, "department"),
            .ExperienceLevel = GetFieldValue(pdf, "experienceLevel"),
            .AcceptsTerms = GetFieldValue(pdf, "acceptTerms") = "Yes"
        }
    End Function

    Private Function GetFieldValue(pdf As PdfDocument, fieldName As String) As String
        Dim field = pdf.Form.FindFormField(fieldName)
        Return If(field?.Value, String.Empty)
    End Function
End Class

' Usage
Class Program
    Shared Sub Main()
        Dim extractor As New FormDataExtractor()
        Dim application = extractor.ExtractApplicationData("submitted-form.pdf")

        ' Access application.FirstName, application.LastName, application.Department, etc.
    End Sub
End Class
$vbLabelText   $csharpLabel

有关读取表单数据的更多示例,请参阅 C# PDF 表单示例Extract PDF Form Fields Guide


如何扁平化 PDF 表单以锁定字段值?

将 PDF 表单扁平化,将交互式字段转换为静态内容。 视觉外观保持一致,但用户不能再编辑数值。 这种技术有几个用途:创建已完成表格的永久记录、准备存档文件以及防止提交后意外或故意修改。

输入PDF

在这里,我们将使用一个合同模板(contract-template.pdf),其中包含客户名称、合同日期、合同价值和付款条件等字段。 这反映了一个典型的工作流程,即在模板中填写特定的交易信息,然后锁定字段以创建一个最终确定的防篡改文档。 以下代码加载模板,填写合同细节,并将所有字段设置为只读。

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-forms-csharp/flatten-form-readonly.cs
using IronPdf;

// Load and fill the form
PdfDocument pdf = PdfDocument.FromFile("contract-template.pdf");

pdf.Form.FindFormField("clientName").Value = "Acme Corporation";
pdf.Form.FindFormField("contractDate").Value = "January 15, 2025";
pdf.Form.FindFormField("contractValue").Value = "$50,000";
pdf.Form.FindFormField("paymentTerms").Value = "Net 30";

// Make all form fields read-only to prevent further editing
foreach (var field in pdf.Form)
{
    field.ReadOnly = true;
}

// Save the document with locked fields
pdf.SaveAs("contract-acme-signed.pdf");
Imports IronPdf

' Load and fill the form
Dim pdf As PdfDocument = PdfDocument.FromFile("contract-template.pdf")

pdf.Form.FindFormField("clientName").Value = "Acme Corporation"
pdf.Form.FindFormField("contractDate").Value = "January 15, 2025"
pdf.Form.FindFormField("contractValue").Value = "$50,000"
pdf.Form.FindFormField("paymentTerms").Value = "Net 30"

' Make all form fields read-only to prevent further editing
For Each field In pdf.Form
    field.ReadOnly = True
Next

' Save the document with locked fields
pdf.SaveAs("contract-acme-signed.pdf")
$vbLabelText   $csharpLabel

输出示例

代码首先将合同详细信息填充到 clientName, contractDate, contractValuepaymentTerms 等字段中。 然后,foreach 循环遍历表单中的每个字段,并设置 field.ReadOnly = true,从而锁定每个字段,防止进一步编辑。 保存后,用户仍可在任何 PDF 阅读器中查看表单值,但点击字段将不允许修改。 这种技术可以创建适合存档或法律文件的永久记录。

有关包括密码保护在内的其他 PDF 安全选项,请参阅 Sign and Secure PDFs TutorialPDF Password Protection Guide


如何为最终用户预填 PDF 表单?

在表单中预先填写已知信息可减少数据输入,从而改善用户体验。 当您的系统中已经有客户信息时,您可以在发送文档前填充表单字段,让用户只查看和填写剩余的字段。

using IronPdf;
using System;

public class CustomerPortalService
{
    public byte[] GeneratePreFilledRenewalForm(int customerId)
    {
        // Simulate fetching customer data from database
        var customer = GetCustomerById(customerId);

        // Load the renewal form template
        PdfDocument pdf = PdfDocument.FromFile("templates/subscription-renewal.pdf");

        // Pre-fill known customer information
        pdf.Form.FindFormField("customerId").Value = customer.Id.ToString();
        pdf.Form.FindFormField("customerName").Value = customer.FullName;
        pdf.Form.FindFormField("email").Value = customer.Email;
        pdf.Form.FindFormField("phone").Value = customer.Phone;
        pdf.Form.FindFormField("address").Value = customer.Address;
        pdf.Form.FindFormField("currentPlan").Value = customer.SubscriptionPlan;
        pdf.Form.FindFormField("renewalDate").Value = DateTime.Now.AddDays(30).ToString("MM/dd/yyyy");

        // Leave editable fields empty for customer input:
        // - newPlan (dropdown for plan selection)
        // - paymentMethod (radio buttons)
        // - additionalNotes (textarea)
        // - signature (signature field)

        // Return as byte array for web download or email attachment
        return pdf.BinaryData;
    }

    // Simulated data access
    private Customer GetCustomerById(int id)
    {
        return new Customer
        {
            Id = id,
            FullName = "Robert Williams",
            Email = "robert.williams@email.com",
            Phone = "(555) 987-6543",
            Address = "123 Business Park Drive\r\nSuite 400\r\nChicago, IL 60601",
            SubscriptionPlan = "Professional"
        };
    }
}

public class Customer
{
    public int Id { get; set; }
    public string FullName { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public string Address { get; set; }
    public string SubscriptionPlan { get; set; }
}
using IronPdf;
using System;

public class CustomerPortalService
{
    public byte[] GeneratePreFilledRenewalForm(int customerId)
    {
        // Simulate fetching customer data from database
        var customer = GetCustomerById(customerId);

        // Load the renewal form template
        PdfDocument pdf = PdfDocument.FromFile("templates/subscription-renewal.pdf");

        // Pre-fill known customer information
        pdf.Form.FindFormField("customerId").Value = customer.Id.ToString();
        pdf.Form.FindFormField("customerName").Value = customer.FullName;
        pdf.Form.FindFormField("email").Value = customer.Email;
        pdf.Form.FindFormField("phone").Value = customer.Phone;
        pdf.Form.FindFormField("address").Value = customer.Address;
        pdf.Form.FindFormField("currentPlan").Value = customer.SubscriptionPlan;
        pdf.Form.FindFormField("renewalDate").Value = DateTime.Now.AddDays(30).ToString("MM/dd/yyyy");

        // Leave editable fields empty for customer input:
        // - newPlan (dropdown for plan selection)
        // - paymentMethod (radio buttons)
        // - additionalNotes (textarea)
        // - signature (signature field)

        // Return as byte array for web download or email attachment
        return pdf.BinaryData;
    }

    // Simulated data access
    private Customer GetCustomerById(int id)
    {
        return new Customer
        {
            Id = id,
            FullName = "Robert Williams",
            Email = "robert.williams@email.com",
            Phone = "(555) 987-6543",
            Address = "123 Business Park Drive\r\nSuite 400\r\nChicago, IL 60601",
            SubscriptionPlan = "Professional"
        };
    }
}

public class Customer
{
    public int Id { get; set; }
    public string FullName { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public string Address { get; set; }
    public string SubscriptionPlan { get; set; }
}
Imports IronPdf
Imports System

Public Class CustomerPortalService
    Public Function GeneratePreFilledRenewalForm(customerId As Integer) As Byte()
        ' Simulate fetching customer data from database
        Dim customer = GetCustomerById(customerId)

        ' Load the renewal form template
        Dim pdf As PdfDocument = PdfDocument.FromFile("templates/subscription-renewal.pdf")

        ' Pre-fill known customer information
        pdf.Form.FindFormField("customerId").Value = customer.Id.ToString()
        pdf.Form.FindFormField("customerName").Value = customer.FullName
        pdf.Form.FindFormField("email").Value = customer.Email
        pdf.Form.FindFormField("phone").Value = customer.Phone
        pdf.Form.FindFormField("address").Value = customer.Address
        pdf.Form.FindFormField("currentPlan").Value = customer.SubscriptionPlan
        pdf.Form.FindFormField("renewalDate").Value = DateTime.Now.AddDays(30).ToString("MM/dd/yyyy")

        ' Leave editable fields empty for customer input:
        ' - newPlan (dropdown for plan selection)
        ' - paymentMethod (radio buttons)
        ' - additionalNotes (textarea)
        ' - signature (signature field)

        ' Return as byte array for web download or email attachment
        Return pdf.BinaryData
    End Function

    ' Simulated data access
    Private Function GetCustomerById(id As Integer) As Customer
        Return New Customer With {
            .Id = id,
            .FullName = "Robert Williams",
            .Email = "robert.williams@email.com",
            .Phone = "(555) 987-6543",
            .Address = "123 Business Park Drive" & vbCrLf & "Suite 400" & vbCrLf & "Chicago, IL 60601",
            .SubscriptionPlan = "Professional"
        }
    End Function
End Class

Public Class Customer
    Public Property Id As Integer
    Public Property FullName As String
    Public Property Email As String
    Public Property Phone As String
    Public Property Address As String
    Public Property SubscriptionPlan As String
End Class
$vbLabelText   $csharpLabel

输出示例

这种模式适用于更新表格、应用程序更新、年度审查以及系统中已存在部分信息的任何情况。 用户收到的文档中已经填入了他们的详细信息,只需提供新信息或更改信息即可。


如何构建表单数据处理管道?

大规模处理表单提交需要一种结构化的方法来处理提取、验证、转换和存储。 简单的流水线模式使翻译过程井井有条,易于维护。

using IronPdf;
using System;
using System.Collections.Generic;
using System.IO;

public class FormSubmission
{
    public string SubmissionId { get; set; }
    public DateTime ReceivedAt { get; set; }
    public string SourceFile { get; set; }
    public Dictionary<string, string> FormData { get; set; }
    public bool IsValid { get; set; }
    public List<string> ValidationErrors { get; set; }
}

public class FormProcessingPipeline
{
    public FormSubmission ProcessSubmission(string pdfPath)
    {
        var submission = new FormSubmission
        {
            SubmissionId = Guid.NewGuid().ToString("N").Substring(0, 8).ToUpper(),
            ReceivedAt = DateTime.UtcNow,
            SourceFile = Path.GetFileName(pdfPath),
            FormData = new Dictionary<string, string>(),
            ValidationErrors = new List<string>()
        };

        // Step 1: Extract form data
        PdfDocument pdf = PdfDocument.FromFile(pdfPath);
        foreach (var field in pdf.Form)
        {
            submission.FormData[field.Name] = field.Value ?? string.Empty;
        }

        // Step 2: Validate required fields
        var requiredFields = new[] { "fullName", "email", "signatureDate" };
        foreach (var fieldName in requiredFields)
        {
            if (!submission.FormData.ContainsKey(fieldName) || 
                string.IsNullOrWhiteSpace(submission.FormData[fieldName]))
            {
                submission.ValidationErrors.Add($"Missing required field: {fieldName}");
            }
        }

        submission.IsValid = submission.ValidationErrors.Count == 0;

        // Step 3: Return the processed submission
        return submission;
    }

    public void ProcessBatch(string folderPath)
    {
        var pdfFiles = Directory.GetFiles(folderPath, "*.pdf");
        var results = new List<FormSubmission>();

        foreach (var file in pdfFiles)
        {
            try
            {
                var submission = ProcessSubmission(file);
                results.Add(submission);
            }
            catch (Exception ex)
            {
                // Handle error for file: ex.Message
            }
        }

        // Summary
        int validCount = 0;
        int invalidCount = 0;
        foreach (var r in results)
        {
            if (r.IsValid) validCount++;
            else invalidCount++;
        }

        // Results summary: results.Count processed, validCount valid, invalidCount invalid
    }
}
using IronPdf;
using System;
using System.Collections.Generic;
using System.IO;

public class FormSubmission
{
    public string SubmissionId { get; set; }
    public DateTime ReceivedAt { get; set; }
    public string SourceFile { get; set; }
    public Dictionary<string, string> FormData { get; set; }
    public bool IsValid { get; set; }
    public List<string> ValidationErrors { get; set; }
}

public class FormProcessingPipeline
{
    public FormSubmission ProcessSubmission(string pdfPath)
    {
        var submission = new FormSubmission
        {
            SubmissionId = Guid.NewGuid().ToString("N").Substring(0, 8).ToUpper(),
            ReceivedAt = DateTime.UtcNow,
            SourceFile = Path.GetFileName(pdfPath),
            FormData = new Dictionary<string, string>(),
            ValidationErrors = new List<string>()
        };

        // Step 1: Extract form data
        PdfDocument pdf = PdfDocument.FromFile(pdfPath);
        foreach (var field in pdf.Form)
        {
            submission.FormData[field.Name] = field.Value ?? string.Empty;
        }

        // Step 2: Validate required fields
        var requiredFields = new[] { "fullName", "email", "signatureDate" };
        foreach (var fieldName in requiredFields)
        {
            if (!submission.FormData.ContainsKey(fieldName) || 
                string.IsNullOrWhiteSpace(submission.FormData[fieldName]))
            {
                submission.ValidationErrors.Add($"Missing required field: {fieldName}");
            }
        }

        submission.IsValid = submission.ValidationErrors.Count == 0;

        // Step 3: Return the processed submission
        return submission;
    }

    public void ProcessBatch(string folderPath)
    {
        var pdfFiles = Directory.GetFiles(folderPath, "*.pdf");
        var results = new List<FormSubmission>();

        foreach (var file in pdfFiles)
        {
            try
            {
                var submission = ProcessSubmission(file);
                results.Add(submission);
            }
            catch (Exception ex)
            {
                // Handle error for file: ex.Message
            }
        }

        // Summary
        int validCount = 0;
        int invalidCount = 0;
        foreach (var r in results)
        {
            if (r.IsValid) validCount++;
            else invalidCount++;
        }

        // Results summary: results.Count processed, validCount valid, invalidCount invalid
    }
}
Imports IronPdf
Imports System
Imports System.Collections.Generic
Imports System.IO

Public Class FormSubmission
    Public Property SubmissionId As String
    Public Property ReceivedAt As DateTime
    Public Property SourceFile As String
    Public Property FormData As Dictionary(Of String, String)
    Public Property IsValid As Boolean
    Public Property ValidationErrors As List(Of String)
End Class

Public Class FormProcessingPipeline
    Public Function ProcessSubmission(pdfPath As String) As FormSubmission
        Dim submission As New FormSubmission With {
            .SubmissionId = Guid.NewGuid().ToString("N").Substring(0, 8).ToUpper(),
            .ReceivedAt = DateTime.UtcNow,
            .SourceFile = Path.GetFileName(pdfPath),
            .FormData = New Dictionary(Of String, String)(),
            .ValidationErrors = New List(Of String)()
        }

        ' Step 1: Extract form data
        Dim pdf As PdfDocument = PdfDocument.FromFile(pdfPath)
        For Each field In pdf.Form
            submission.FormData(field.Name) = If(field.Value, String.Empty)
        Next

        ' Step 2: Validate required fields
        Dim requiredFields As String() = {"fullName", "email", "signatureDate"}
        For Each fieldName In requiredFields
            If Not submission.FormData.ContainsKey(fieldName) OrElse
               String.IsNullOrWhiteSpace(submission.FormData(fieldName)) Then
                submission.ValidationErrors.Add($"Missing required field: {fieldName}")
            End If
        Next

        submission.IsValid = submission.ValidationErrors.Count = 0

        ' Step 3: Return the processed submission
        Return submission
    End Function

    Public Sub ProcessBatch(folderPath As String)
        Dim pdfFiles As String() = Directory.GetFiles(folderPath, "*.pdf")
        Dim results As New List(Of FormSubmission)()

        For Each file In pdfFiles
            Try
                Dim submission As FormSubmission = ProcessSubmission(file)
                results.Add(submission)
            Catch ex As Exception
                ' Handle error for file: ex.Message
            End Try
        Next

        ' Summary
        Dim validCount As Integer = 0
        Dim invalidCount As Integer = 0
        For Each r In results
            If r.IsValid Then
                validCount += 1
            Else
                invalidCount += 1
            End If
        Next

        ' Results summary: results.Count processed, validCount valid, invalidCount invalid
    End Sub
End Class
$vbLabelText   $csharpLabel

此流水线结构可扩展至包括数据库存储、电子邮件通知、与外部 API 集成或您的工作流程所需的任何其他处理步骤。 关键是要保持提取、验证和下游处理之间的分离。

关于高性能批处理,请参阅 Async PDF 生成指南,其中涵盖了多线程和并行处理模式。


如何自动生成 W-9 等 IRS 税表?

政府表格是一些 PDF 处理量最大的场景。 仅 W-9(纳税人识别号申请表)一项,企业每年就需要填写数百万次,用于录用供应商和承包商。 每家向独立承包商支付报酬的公司都需要将 W-9 归档,而将这一收集流程自动化可以节省大量的管理时间。

using IronPdf;
using System;

public class W9FormData
{
    public string Name { get; set; }
    public string BusinessName { get; set; }
    public string FederalTaxClassification { get; set; }
    public string ExemptPayeeCode { get; set; }
    public string FatcaExemptionCode { get; set; }
    public string Address { get; set; }
    public string CityStateZip { get; set; }
    public string AccountNumbers { get; set; }
    public string TaxpayerIdNumber { get; set; }
    public DateTime SignatureDate { get; set; }
}

public class W9FormProcessor
{
    public void FillW9Form(W9FormData data, string templatePath, string outputPath)
    {
        PdfDocument pdf = PdfDocument.FromFile(templatePath);

        // The W-9 form has specific field names defined by the IRS
        // These correspond to the official form structure

        // Line 1: Name as shown on your income tax return
        SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].f1_1[0]", data.Name);

        // Line 2: Business name/disregarded entity name
        SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].f1_2[0]", data.BusinessName);

        // Line 3: Federal tax classification (checkbox selection)
        // The W-9 uses checkbox fields for classification
        switch (data.FederalTaxClassification)
        {
            case "Individual":
                SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].FederalClassification[0].c1_1[0]", "1");
                break;
            case "CCorporation":
                SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].FederalClassification[0].c1_1[1]", "2");
                break;
            case "SCorporation":
                SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].FederalClassification[0].c1_1[2]", "3");
                break;
            case "Partnership":
                SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].FederalClassification[0].c1_1[3]", "4");
                break;
            case "LLC":
                SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].FederalClassification[0].c1_1[5]", "6");
                break;
        }

        // Line 5: Address
        SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].Address[0].f1_7[0]", data.Address);

        // Line 6: City, state, and ZIP code
        SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].Address[0].f1_8[0]", data.CityStateZip);

        // Part I: Taxpayer Identification Number (SSN or EIN)
        // For security, typically only last 4 digits are pre-filled or field is left for manual entry
        if (!string.IsNullOrEmpty(data.TaxpayerIdNumber) && data.TaxpayerIdNumber.Length >= 4)
        {
            // TIN left blank for security - user must enter manually
        }

        // Signature date
        SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].Date[0]",
                        data.SignatureDate.ToString("MM/dd/yyyy"));

        pdf.SaveAs(outputPath);
    }

    private void SetFieldIfExists(PdfDocument pdf, string fieldName, string value)
    {
        var field = pdf.Form.FindFormField(fieldName);
        if (field != null && !string.IsNullOrEmpty(value))
        {
            field.Value = value;
        }
    }
}

// Usage example
class Program
{
    static void Main()
    {
        var vendorData = new W9FormData
        {
            Name = "Johnson Consulting LLC",
            BusinessName = "",
            FederalTaxClassification = "LLC",
            Address = "456 Commerce Street",
            CityStateZip = "Austin, TX 78701",
            SignatureDate = DateTime.Today
        };

        var processor = new W9FormProcessor();
        processor.FillW9Form(vendorData, "fw9.pdf", "w9-johnson-consulting.pdf");
    }
}
using IronPdf;
using System;

public class W9FormData
{
    public string Name { get; set; }
    public string BusinessName { get; set; }
    public string FederalTaxClassification { get; set; }
    public string ExemptPayeeCode { get; set; }
    public string FatcaExemptionCode { get; set; }
    public string Address { get; set; }
    public string CityStateZip { get; set; }
    public string AccountNumbers { get; set; }
    public string TaxpayerIdNumber { get; set; }
    public DateTime SignatureDate { get; set; }
}

public class W9FormProcessor
{
    public void FillW9Form(W9FormData data, string templatePath, string outputPath)
    {
        PdfDocument pdf = PdfDocument.FromFile(templatePath);

        // The W-9 form has specific field names defined by the IRS
        // These correspond to the official form structure

        // Line 1: Name as shown on your income tax return
        SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].f1_1[0]", data.Name);

        // Line 2: Business name/disregarded entity name
        SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].f1_2[0]", data.BusinessName);

        // Line 3: Federal tax classification (checkbox selection)
        // The W-9 uses checkbox fields for classification
        switch (data.FederalTaxClassification)
        {
            case "Individual":
                SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].FederalClassification[0].c1_1[0]", "1");
                break;
            case "CCorporation":
                SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].FederalClassification[0].c1_1[1]", "2");
                break;
            case "SCorporation":
                SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].FederalClassification[0].c1_1[2]", "3");
                break;
            case "Partnership":
                SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].FederalClassification[0].c1_1[3]", "4");
                break;
            case "LLC":
                SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].FederalClassification[0].c1_1[5]", "6");
                break;
        }

        // Line 5: Address
        SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].Address[0].f1_7[0]", data.Address);

        // Line 6: City, state, and ZIP code
        SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].Address[0].f1_8[0]", data.CityStateZip);

        // Part I: Taxpayer Identification Number (SSN or EIN)
        // For security, typically only last 4 digits are pre-filled or field is left for manual entry
        if (!string.IsNullOrEmpty(data.TaxpayerIdNumber) && data.TaxpayerIdNumber.Length >= 4)
        {
            // TIN left blank for security - user must enter manually
        }

        // Signature date
        SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].Date[0]",
                        data.SignatureDate.ToString("MM/dd/yyyy"));

        pdf.SaveAs(outputPath);
    }

    private void SetFieldIfExists(PdfDocument pdf, string fieldName, string value)
    {
        var field = pdf.Form.FindFormField(fieldName);
        if (field != null && !string.IsNullOrEmpty(value))
        {
            field.Value = value;
        }
    }
}

// Usage example
class Program
{
    static void Main()
    {
        var vendorData = new W9FormData
        {
            Name = "Johnson Consulting LLC",
            BusinessName = "",
            FederalTaxClassification = "LLC",
            Address = "456 Commerce Street",
            CityStateZip = "Austin, TX 78701",
            SignatureDate = DateTime.Today
        };

        var processor = new W9FormProcessor();
        processor.FillW9Form(vendorData, "fw9.pdf", "w9-johnson-consulting.pdf");
    }
}
Imports IronPdf
Imports System

Public Class W9FormData
    Public Property Name As String
    Public Property BusinessName As String
    Public Property FederalTaxClassification As String
    Public Property ExemptPayeeCode As String
    Public Property FatcaExemptionCode As String
    Public Property Address As String
    Public Property CityStateZip As String
    Public Property AccountNumbers As String
    Public Property TaxpayerIdNumber As String
    Public Property SignatureDate As DateTime
End Class

Public Class W9FormProcessor
    Public Sub FillW9Form(data As W9FormData, templatePath As String, outputPath As String)
        Dim pdf As PdfDocument = PdfDocument.FromFile(templatePath)

        ' The W-9 form has specific field names defined by the IRS
        ' These correspond to the official form structure

        ' Line 1: Name as shown on your income tax return
        SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].f1_1[0]", data.Name)

        ' Line 2: Business name/disregarded entity name
        SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].f1_2[0]", data.BusinessName)

        ' Line 3: Federal tax classification (checkbox selection)
        ' The W-9 uses checkbox fields for classification
        Select Case data.FederalTaxClassification
            Case "Individual"
                SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].FederalClassification[0].c1_1[0]", "1")
            Case "CCorporation"
                SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].FederalClassification[0].c1_1[1]", "2")
            Case "SCorporation"
                SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].FederalClassification[0].c1_1[2]", "3")
            Case "Partnership"
                SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].FederalClassification[0].c1_1[3]", "4")
            Case "LLC"
                SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].FederalClassification[0].c1_1[5]", "6")
        End Select

        ' Line 5: Address
        SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].Address[0].f1_7[0]", data.Address)

        ' Line 6: City, state, and ZIP code
        SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].Address[0].f1_8[0]", data.CityStateZip)

        ' Part I: Taxpayer Identification Number (SSN or EIN)
        ' For security, typically only last 4 digits are pre-filled or field is left for manual entry
        If Not String.IsNullOrEmpty(data.TaxpayerIdNumber) AndAlso data.TaxpayerIdNumber.Length >= 4 Then
            ' TIN left blank for security - user must enter manually
        End If

        ' Signature date
        SetFieldIfExists(pdf, "topmostSubform[0].Page1[0].Date[0]", data.SignatureDate.ToString("MM/dd/yyyy"))

        pdf.SaveAs(outputPath)
    End Sub

    Private Sub SetFieldIfExists(pdf As PdfDocument, fieldName As String, value As String)
        Dim field = pdf.Form.FindFormField(fieldName)
        If field IsNot Nothing AndAlso Not String.IsNullOrEmpty(value) Then
            field.Value = value
        End If
    End Sub
End Class

' Usage example
Module Program
    Sub Main()
        Dim vendorData As New W9FormData With {
            .Name = "Johnson Consulting LLC",
            .BusinessName = "",
            .FederalTaxClassification = "LLC",
            .Address = "456 Commerce Street",
            .CityStateZip = "Austin, TX 78701",
            .SignatureDate = DateTime.Today
        }

        Dim processor As New W9FormProcessor()
        processor.FillW9Form(vendorData, "fw9.pdf", "w9-johnson-consulting.pdf")
    End Sub
End Module
$vbLabelText   $csharpLabel

政府表格通常使用复杂的分层字段命名约定。 上面显示的 W-9 字段名称遵循了美国国税局官方 PDF 中使用的 XFA 衍生命名模式。 在使用政府表格时,一定要从官方机构网站下载最新版本,并在编写自动化代码前检查其字段结构。


如何使用可变数据批量处理表单?

批量处理从数据库查询、CSV 文件或 API 响应等数据源生成多个完整的表单。 这种能力对于年度税务文件分发、员工入职培训包或客户报表生成等场景至关重要。

using IronPdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

public class VendorRecord
{
    public string VendorId { get; set; }
    public string CompanyName { get; set; }
    public string ContactName { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string TaxClassification { get; set; }
}

public class BatchFormGenerator
{
    private readonly string _templatePath;
    private readonly string _outputDirectory;

    public BatchFormGenerator(string templatePath, string outputDirectory)
    {
        _templatePath = templatePath;
        _outputDirectory = outputDirectory;

        if (!Directory.Exists(_outputDirectory))
        {
            Directory.CreateDirectory(_outputDirectory);
        }
    }

    public void GenerateVendorForms(List<VendorRecord> vendors)
    {
        var startTime = DateTime.Now;
        int successCount = 0;
        int errorCount = 0;

        foreach (var vendor in vendors)
        {
            try
            {
                GenerateSingleForm(vendor);
                successCount++;
            }
            catch (Exception ex)
            {
                errorCount++;
                // Handle error: ex.Message
            }
        }

        var elapsed = DateTime.Now - startTime;
        // Results: successCount successful, errorCount errors, elapsed time
    }

    private void GenerateSingleForm(VendorRecord vendor)
    {
        PdfDocument pdf = PdfDocument.FromFile(_templatePath);

        // Fill vendor information fields
        SetField(pdf, "vendorId", vendor.VendorId);
        SetField(pdf, "companyName", vendor.CompanyName);
        SetField(pdf, "contactName", vendor.ContactName);
        SetField(pdf, "address", vendor.Address);
        SetField(pdf, "city", vendor.City);
        SetField(pdf, "state", vendor.State);
        SetField(pdf, "zipCode", vendor.ZipCode);
        SetField(pdf, "taxClassification", vendor.TaxClassification);
        SetField(pdf, "generatedDate", DateTime.Today.ToString("MM/dd/yyyy"));

        // Create a safe filename from company name
        string safeFileName = string.Join("_", vendor.CompanyName.Split(Path.GetInvalidFileNameChars()));
        string outputPath = Path.Combine(_outputDirectory, $"vendor-form-{vendor.VendorId}-{safeFileName}.pdf");

        pdf.SaveAs(outputPath);
    }

    private void SetField(PdfDocument pdf, string fieldName, string value)
    {
        var field = pdf.Form.FindFormField(fieldName);
        if (field != null)
        {
            field.Value = value ?? string.Empty;
        }
    }
}

// Usage example with sample data
class Program
{
    static void Main()
    {
        var vendors = new List<VendorRecord>
        {
            new VendorRecord
            {
                VendorId = "V001",
                CompanyName = "Alpha Supplies Inc",
                ContactName = "Maria Garcia",
                Address = "100 Industrial Way",
                City = "Chicago",
                State = "IL",
                ZipCode = "60601",
                TaxClassification = "Corporation"
            },
            new VendorRecord
            {
                VendorId = "V002",
                CompanyName = "Beta Services LLC",
                ContactName = "James Wilson",
                Address = "200 Tech Park Drive",
                City = "Austin",
                State = "TX",
                ZipCode = "78701",
                TaxClassification = "LLC"
            },
            new VendorRecord
            {
                VendorId = "V003",
                CompanyName = "Gamma Consulting",
                ContactName = "Sarah Chen",
                Address = "300 Business Center",
                City = "Seattle",
                State = "WA",
                ZipCode = "98101",
                TaxClassification = "Partnership"
            }
        };

        var generator = new BatchFormGenerator("vendor-info-template.pdf", "output/vendor-forms");
        generator.GenerateVendorForms(vendors);
    }
}
using IronPdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

public class VendorRecord
{
    public string VendorId { get; set; }
    public string CompanyName { get; set; }
    public string ContactName { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string TaxClassification { get; set; }
}

public class BatchFormGenerator
{
    private readonly string _templatePath;
    private readonly string _outputDirectory;

    public BatchFormGenerator(string templatePath, string outputDirectory)
    {
        _templatePath = templatePath;
        _outputDirectory = outputDirectory;

        if (!Directory.Exists(_outputDirectory))
        {
            Directory.CreateDirectory(_outputDirectory);
        }
    }

    public void GenerateVendorForms(List<VendorRecord> vendors)
    {
        var startTime = DateTime.Now;
        int successCount = 0;
        int errorCount = 0;

        foreach (var vendor in vendors)
        {
            try
            {
                GenerateSingleForm(vendor);
                successCount++;
            }
            catch (Exception ex)
            {
                errorCount++;
                // Handle error: ex.Message
            }
        }

        var elapsed = DateTime.Now - startTime;
        // Results: successCount successful, errorCount errors, elapsed time
    }

    private void GenerateSingleForm(VendorRecord vendor)
    {
        PdfDocument pdf = PdfDocument.FromFile(_templatePath);

        // Fill vendor information fields
        SetField(pdf, "vendorId", vendor.VendorId);
        SetField(pdf, "companyName", vendor.CompanyName);
        SetField(pdf, "contactName", vendor.ContactName);
        SetField(pdf, "address", vendor.Address);
        SetField(pdf, "city", vendor.City);
        SetField(pdf, "state", vendor.State);
        SetField(pdf, "zipCode", vendor.ZipCode);
        SetField(pdf, "taxClassification", vendor.TaxClassification);
        SetField(pdf, "generatedDate", DateTime.Today.ToString("MM/dd/yyyy"));

        // Create a safe filename from company name
        string safeFileName = string.Join("_", vendor.CompanyName.Split(Path.GetInvalidFileNameChars()));
        string outputPath = Path.Combine(_outputDirectory, $"vendor-form-{vendor.VendorId}-{safeFileName}.pdf");

        pdf.SaveAs(outputPath);
    }

    private void SetField(PdfDocument pdf, string fieldName, string value)
    {
        var field = pdf.Form.FindFormField(fieldName);
        if (field != null)
        {
            field.Value = value ?? string.Empty;
        }
    }
}

// Usage example with sample data
class Program
{
    static void Main()
    {
        var vendors = new List<VendorRecord>
        {
            new VendorRecord
            {
                VendorId = "V001",
                CompanyName = "Alpha Supplies Inc",
                ContactName = "Maria Garcia",
                Address = "100 Industrial Way",
                City = "Chicago",
                State = "IL",
                ZipCode = "60601",
                TaxClassification = "Corporation"
            },
            new VendorRecord
            {
                VendorId = "V002",
                CompanyName = "Beta Services LLC",
                ContactName = "James Wilson",
                Address = "200 Tech Park Drive",
                City = "Austin",
                State = "TX",
                ZipCode = "78701",
                TaxClassification = "LLC"
            },
            new VendorRecord
            {
                VendorId = "V003",
                CompanyName = "Gamma Consulting",
                ContactName = "Sarah Chen",
                Address = "300 Business Center",
                City = "Seattle",
                State = "WA",
                ZipCode = "98101",
                TaxClassification = "Partnership"
            }
        };

        var generator = new BatchFormGenerator("vendor-info-template.pdf", "output/vendor-forms");
        generator.GenerateVendorForms(vendors);
    }
}
Imports IronPdf
Imports System
Imports System.Collections.Generic
Imports System.IO

Public Class VendorRecord
    Public Property VendorId As String
    Public Property CompanyName As String
    Public Property ContactName As String
    Public Property Address As String
    Public Property City As String
    Public Property State As String
    Public Property ZipCode As String
    Public Property TaxClassification As String
End Class

Public Class BatchFormGenerator
    Private ReadOnly _templatePath As String
    Private ReadOnly _outputDirectory As String

    Public Sub New(templatePath As String, outputDirectory As String)
        _templatePath = templatePath
        _outputDirectory = outputDirectory

        If Not Directory.Exists(_outputDirectory) Then
            Directory.CreateDirectory(_outputDirectory)
        End If
    End Sub

    Public Sub GenerateVendorForms(vendors As List(Of VendorRecord))
        Dim startTime = DateTime.Now
        Dim successCount As Integer = 0
        Dim errorCount As Integer = 0

        For Each vendor In vendors
            Try
                GenerateSingleForm(vendor)
                successCount += 1
            Catch ex As Exception
                errorCount += 1
                ' Handle error: ex.Message
            End Try
        Next

        Dim elapsed = DateTime.Now - startTime
        ' Results: successCount successful, errorCount errors, elapsed time
    End Sub

    Private Sub GenerateSingleForm(vendor As VendorRecord)
        Dim pdf As PdfDocument = PdfDocument.FromFile(_templatePath)

        ' Fill vendor information fields
        SetField(pdf, "vendorId", vendor.VendorId)
        SetField(pdf, "companyName", vendor.CompanyName)
        SetField(pdf, "contactName", vendor.ContactName)
        SetField(pdf, "address", vendor.Address)
        SetField(pdf, "city", vendor.City)
        SetField(pdf, "state", vendor.State)
        SetField(pdf, "zipCode", vendor.ZipCode)
        SetField(pdf, "taxClassification", vendor.TaxClassification)
        SetField(pdf, "generatedDate", DateTime.Today.ToString("MM/dd/yyyy"))

        ' Create a safe filename from company name
        Dim safeFileName As String = String.Join("_", vendor.CompanyName.Split(Path.GetInvalidFileNameChars()))
        Dim outputPath As String = Path.Combine(_outputDirectory, $"vendor-form-{vendor.VendorId}-{safeFileName}.pdf")

        pdf.SaveAs(outputPath)
    End Sub

    Private Sub SetField(pdf As PdfDocument, fieldName As String, value As String)
        Dim field = pdf.Form.FindFormField(fieldName)
        If field IsNot Nothing Then
            field.Value = If(value, String.Empty)
        End If
    End Sub
End Class

' Usage example with sample data
Module Program
    Sub Main()
        Dim vendors As New List(Of VendorRecord) From {
            New VendorRecord With {
                .VendorId = "V001",
                .CompanyName = "Alpha Supplies Inc",
                .ContactName = "Maria Garcia",
                .Address = "100 Industrial Way",
                .City = "Chicago",
                .State = "IL",
                .ZipCode = "60601",
                .TaxClassification = "Corporation"
            },
            New VendorRecord With {
                .VendorId = "V002",
                .CompanyName = "Beta Services LLC",
                .ContactName = "James Wilson",
                .Address = "200 Tech Park Drive",
                .City = "Austin",
                .State = "TX",
                .ZipCode = "78701",
                .TaxClassification = "LLC"
            },
            New VendorRecord With {
                .VendorId = "V003",
                .CompanyName = "Gamma Consulting",
                .ContactName = "Sarah Chen",
                .Address = "300 Business Center",
                .City = "Seattle",
                .State = "WA",
                .ZipCode = "98101",
                .TaxClassification = "Partnership"
            }
        }

        Dim generator As New BatchFormGenerator("vendor-info-template.pdf", "output/vendor-forms")
        generator.GenerateVendorForms(vendors)
    End Sub
End Module
$vbLabelText   $csharpLabel

对于非常大的批量,可以考虑使用受控并发来实施并行处理,以最大限度地提高吞吐量,同时又不会占用系统资源。


如何确保符合政府 PDF 标准?

政府机构通常要求文件符合特定的 PDF 标准,以便长期存档和访问。 PDF/A 是 PDF 的 ISO 标准化子集,旨在可靠地长期保存电子文档。 IronPDF 支持以 PDF/A 格式导出文档,以满足这些合规性要求。

以下代码从 HTML 创建了一份政府官方表格,填写了申请人数据,并使用 SaveAsPdfA() 方法将其保存为符合 PDF/A-3b 标准的文档。 这种格式可确保文件在未来几十年内保持可读性,并符合大多数政府提交文件的要求。

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-forms-csharp/pdfa-compliance.cs
using IronPdf;

// Create or load a form document
string formHtml = @"
<!DOCTYPE html>
<html>
<head>
    <style>
        body { font-family: Arial, sans-serif; margin: 40px; }
        h1 { color: #003366; }
        .form-section { margin: 20px 0; }
        label { display: block; margin: 10px 0 5px 0; font-weight: bold; }
        input, select { padding: 8px; width: 300px; border: 1px solid #ccc; }
    </style>
</head>
<body>
    <h1>Official Government Form</h1>
    <p>This document complies with PDF/A-3b archival standards.</p>
    <form>
        <div class='form-section'>
            <label>Applicant Name</label>
            <input type='text' name='applicantName' />
        </div>
        <div class='form-section'>
            <label>Application Date</label>
            <input type='text' name='applicationDate' />
        </div>
        <div class='form-section'>
            <label>Department</label>
            <select name='department'>
                <option value=''>Select Department</option>
                <option value='Revenue'>Department of Revenue</option>
                <option value='Labor'>Department of Labor</option>
                <option value='Commerce'>Department of Commerce</option>
            </select>
        </div>
    </form>
</body>
</html>";

ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.CreatePdfFormsFromHtml = true;

PdfDocument pdf = renderer.RenderHtmlAsPdf(formHtml);

// Fill form fields
pdf.Form.FindFormField("applicantName").Value = "Government Services Corp";
pdf.Form.FindFormField("applicationDate").Value = DateTime.Today.ToString("MM/dd/yyyy");
pdf.Form.FindFormField("department").Value = "Commerce";

// Convert to PDF/A-3b for archival compliance
pdf.SaveAsPdfA("government-form-archived.pdf", PdfAVersions.PdfA3b);
Imports IronPdf

' Create or load a form document
Dim formHtml As String = "
<!DOCTYPE html>
<html>
<head>
    <style>
        body { font-family: Arial, sans-serif; margin: 40px; }
        h1 { color: #003366; }
        .form-section { margin: 20px 0; }
        label { display: block; margin: 10px 0 5px 0; font-weight: bold; }
        input, select { padding: 8px; width: 300px; border: 1px solid #ccc; }
    </style>
</head>
<body>
    <h1>Official Government Form</h1>
    <p>This document complies with PDF/A-3b archival standards.</p>
    <form>
        <div class='form-section'>
            <label>Applicant Name</label>
            <input type='text' name='applicantName' />
        </div>
        <div class='form-section'>
            <label>Application Date</label>
            <input type='text' name='applicationDate' />
        </div>
        <div class='form-section'>
            <label>Department</label>
            <select name='department'>
                <option value=''>Select Department</option>
                <option value='Revenue'>Department of Revenue</option>
                <option value='Labor'>Department of Labor</option>
                <option value='Commerce'>Department of Commerce</option>
            </select>
        </div>
    </form>
</body>
</html>"

Dim renderer As New ChromePdfRenderer()
renderer.RenderingOptions.CreatePdfFormsFromHtml = True

Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(formHtml)

' Fill form fields
pdf.Form.FindFormField("applicantName").Value = "Government Services Corp"
pdf.Form.FindFormField("applicationDate").Value = DateTime.Today.ToString("MM/dd/yyyy")
pdf.Form.FindFormField("department").Value = "Commerce"

' Convert to PDF/A-3b for archival compliance
pdf.SaveAsPdfA("government-form-archived.pdf", PdfAVersions.PdfA3b)
$vbLabelText   $csharpLabel

输出示例

SaveAsPdfA() 方法接收输出文件名和 PdfAVersions 枚举值(如 PdfA3b )。 在保存过程中,IronPDF 会自动嵌入所有字体、标准化色彩空间、去除任何加密,并根据 PDF/A 要求构建元数据。 这些转换可确保文档在未来几十年内保持可读性和视觉上的一致性,无论什么软件试图打开它。

向政府机构提交表格时,请查看其具体要求。 有些人可能要求使用 PDF/A-1b 进行基本存档,而其他人则指定使用 PDF/A-3b,以允许嵌入附件(如 XML 数据文件)。 例如,美国国税局接受各种提交类型的特定 PDF 版本,而移民表格通常也有自己的技术规范。

有关 PDF/A 转换的完整文档,请参阅 Convert PDF to PDF/A-3b in C#PDF/A Compliance Guide 。 有关可访问性标准,请参阅 Create PDF/UA Documents


下一步

现在,您已经拥有了将耗费数小时手工数据录入的 PDF 表单工作流程自动化所需的一切。 无论您是在处理供应商入职的 W-9 表单,还是在 EHR 系统中预填病人接收表单,或者将提交的应用程序数据导入 CRM,本指南中的技术都能直接转化为您收件箱中的表单。 有关表单之外的 PDF 编辑功能的完整攻略,请参阅 PDF 编辑教程

要向前推进,首先填写和编辑您的团队已经使用的现有表单,例如客户申请或员工入职资料包,然后使用本指南中的字典方法建立一个简单的管道。在此基础上,从 HTML 创建新表单,实现完全的设计灵活性,或为需要具有法律约束力的签批的表单添加数字签名字段。 当您需要满足政府存档标准时,PDF/A 合规性指南涵盖了长期保存的内容,PDF/UA 可访问性指南确保您的表格通过第 508 条的审核。

准备好开始构建了吗? 下载 IronPDF 并免费试用。 同一个库可以处理从单个表单填写到跨 .NET 环境的 大批量批处理的所有事务。 如果您有关于表单自动化或集成的问题,请联系我们的工程支持团队


常见问题解答

如何使用 C# 创建 PDF 表单?

您可以使用 IronPDF 将 HTML 表单转换为 PDF 文档,从而用 C# 创建 PDF 表单。这样就可以轻松定制和设计交互式表单。

是否可以用 C# 以编程方式填写现有的 PDF 表单?

是的,IronPdf 允许您使用 C# 代码访问和修改表单字段,从而以编程方式用 C# 填充现有的 PDF 表单。

我可以使用 IronPDF 从 PDF 表单中提取数据吗?

IronPDF 提供从 PDF 文档中提取表单数据的功能,可轻松检索和处理输入到 PDF 表单中的信息。

PDF 表单扁平化是什么意思?

扁平化 PDF 表单涉及将表单域转换为静态内容,从而防止进一步编辑。IronPDF 可对表单进行扁平化处理,以确保数据完整性,防止表单提交后发生更改。

如何自动处理 W-9 等政府表格?

IronPdf 可以自动处理 W-9 等政府表格,使您能够以编程方式填写、提取和管理表格数据,从而简化工作流程。

使用 HTML 创建 PDF 表单有哪些优势?

使用 IronPDF 以 HTML 创建 PDF 表单,设计灵活,易于与现有网络技术集成,可创建动态和响应式表单。

IronPdf 能否处理交互式表单元素?

是的,IronPDF 支持文本字段、复选框和按钮等交互式表单元素,可以用 C# 创建和操作动态 PDF 表单。

是否可以自定义 PDF 表单的外观?

IronPDF 允许完全自定义 PDF 表单的外观,使您能够将 CSS 样式应用于表单元素,从而获得专业、一致的外观。

Curtis Chau
技术作家

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

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 17,386,124 | 版本: 2026.2 刚刚发布