PDF Forms in C#: Create, Fill, and Process Interactive Forms
PDF forms remain the backbone of document workflows across government, healthcare, legal, and financial sectors. With over 90% of government agencies releasing official forms in PDF format and 290 billion new PDFs created annually, the ability to programmatically create PDF form .NET applications and fill PDF form C# code has become an essential skill for developers. Whether you need to automate tax form processing, streamline patient intake workflows, or build document management systems, understanding how to work with PDF AcroForms C# opens the door to powerful automation opportunities.
This guide walks you through the complete lifecycle of PDF form handling using C# and IronPDF. You will learn how to read existing forms and populate them with data, create PDF form .NET solutions from scratch using familiar HTML syntax, extract submitted data for processing, and build automation pipelines capable of handling thousands of government forms. The code examples provided are complete and ready to use in your own projects.
Quickstart: Fill a PDF Form with IronPDF
Get started quickly with IronPDF to fill PDF forms programmatically. This simple example shows how to load an existing PDF form, fill in a text field, and save the result. With minimal code, you can automate form filling for any PDF with interactive fields.
Get started making PDFs with NuGet now:
Install IronPDF with NuGet Package Manager
Copy and run this code snippet.
var pdf = IronPdf.PdfDocument.FromFile("form.pdf"); pdf.Form.FindFormField("name").Value = "John Smith"; pdf.SaveAs("filled-form.pdf");Deploy to test on your live environment
Table of Contents
- Understanding PDF Forms
- Reading and Filling Existing Forms
- Creating Forms from Scratch
- Form Processing Workflows
- Government Form Automation
Why are Interactive PDF Forms Still Essential in 2025?
Despite the proliferation of web forms and cloud-based document solutions, PDF forms continue to dominate critical business processes. Government agencies, healthcare providers, financial institutions, and legal firms all rely heavily on PDF forms for their official documentation. In healthcare alone, 88% of patient records are stored or shared as PDFs. Tax authorities worldwide process billions of form submissions annually, with the vast majority arriving as electronic PDF documents.
Why does this format persist when seemingly more modern alternatives exist? The answer lies in a combination of legal requirements, universal compatibility, and document integrity. PDF forms preserve exact formatting regardless of the device or operating system used to view them. A form filled on a Windows desktop appears identical when opened on a Mac laptop or printed at a government office. This consistency matters enormously when dealing with legal documents, regulatory filings, and official records where precise formatting can affect validity.
Web forms certainly have their place, but they cannot fully replace PDF forms for several reasons. Many regulatory bodies specifically require PDF submissions. Legal proceedings often demand documents in a format that can be reliably archived and verified years later. Offline access remains important for field workers, remote locations, and situations where internet connectivity cannot be guaranteed. Additionally, PDF forms can be digitally signed, encrypted, and tracked in ways that provide audit trails essential for compliance.
The PDF software market reflects this ongoing demand, valued at $4.8 billion in 2024 with projected growth of 8 to 11 percent annually through 2030. For .NET developers, this represents both a technical challenge and a significant opportunity. Mastering PDF form automation means being able to streamline workflows that touch millions of documents across every industry.
What are PDF AcroForms and How Do They Work?
AcroForms represent the standard interactive form technology built into the PDF specification. Developed by Adobe and now maintained as an ISO standard, AcroForms allow PDF documents to contain fillable fields that users can complete using any compliant PDF reader. When you open a tax form in Adobe Acrobat or Preview and type into the fields, you are interacting with AcroForm elements embedded in that document.
Each form field in an AcroForm has several key properties. The field name serves as a unique identifier that programs use to locate and manipulate the field. The field type determines what kind of input the field accepts, whether text, checkboxes, radio buttons, dropdown selections, or signatures. The value property holds the current content of the field, which can be read or written programmatically. Additional properties control appearance, validation rules, and default values.
AcroForms support several distinct field types that cover most data collection needs. Text fields accept free-form text input and can be configured for single lines or multiple lines. Checkbox fields represent binary choices and can be grouped to allow multiple selections. Radio button fields work together in groups where selecting one option automatically deselects the others. Combo boxes and list boxes present predefined options for selection. Signature fields provide designated areas for digital signatures. Understanding these field types and how they behave is essential for effective form automation.
The structure of AcroForms makes them well suited to programmatic manipulation. Because each field has a name and a value, you can iterate through all fields in a document, read their current values, and set new values using straightforward code. This predictable structure enables the automation workflows covered throughout this guide.
How do you Open a PDF and List All Form Fields?
Before filling a PDF form programmatically, you need to understand its structure. Opening a document and enumerating its fields reveals the field names you will use to set values, along with the field types and any existing content. This discovery step proves especially valuable when working with third-party forms where you do not have documentation about the internal field structure.
Input PDF
For this example, we'll use an existing PDF form called application-form.pdf. This could be any PDF containing interactive form fields—a government form, an application template from your organization, or a third-party document you need to process. The code below loads this file and iterates through all its interactive form fields.
:path=/static-assets/pdf/content-code-examples/tutorials/pdf-forms-csharp/list-form-fields.csusing 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
NextThe PdfDocument.FromFile() method loads the PDF into memory, and the Form collection provides access to every interactive field in the document. Each field exposes properties like Name (the unique identifier used for programmatic access), Type (text field, checkbox, radio button, dropdown, or signature), Value (the current content), and ReadOnly (whether the field can be modified). Running this enumeration against an unfamiliar form gives you the complete field inventory needed to write code that fills it correctly.
For forms with many fields, you might want to filter by type or search for specific field names. The FindFormField method locates a field by its exact name, throwing an exception if no match exists. Use try-catch blocks when you are not certain if a field exists.
:path=/static-assets/pdf/content-code-examples/tutorials/pdf-forms-csharp/find-form-field.csusing 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")For more details on reading form field values, see the Extract PDF Form Fields in C# guide.
How do you Fill Text Fields, Checkboxes, and Dropdowns?
Once you know the field names in a PDF form, filling them requires only a few lines of code. Each field type accepts values in a specific format. Text fields take string values directly. Checkboxes use "Yes" for checked and "No" for unchecked states. Radio buttons accept the value of the selected option. Dropdown fields accept any value from their predefined list of choices.
Input PDF
We'll work with a customer registration form (customer-registration.pdf) that contains several field types: text fields for name, email, phone, and address; a checkbox for newsletter opt-in; and a dropdown for account type selection. This example loads the form and fills each field with sample customer data.
:path=/static-assets/pdf/content-code-examples/tutorials/pdf-forms-csharp/fill-text-checkbox-dropdown.csusing 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")Sample Output
The FindFormField() method locates each field by its exact name, and setting the Value property populates the field content. Notice the use of \r\n for line breaks in the address field—multi-line text areas interpret these escape sequences correctly, allowing you to format addresses, comments, and other content that spans multiple lines. For checkboxes, the values "Yes" and "No" toggle the checked state, while dropdowns accept any value that matches one of their predefined options.
Working with radio buttons requires understanding that all buttons in a group share the same field name. Setting the value selects the matching option and deselects all others in that group.
:path=/static-assets/pdf/content-code-examples/tutorials/pdf-forms-csharp/radio-button-group.csusing 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")Dropdown fields work similarly. You can examine the available choices through the Choices property before setting a value, ensuring your code only attempts to select valid options.
:path=/static-assets/pdf/content-code-examples/tutorials/pdf-forms-csharp/dropdown-choices.csusing 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")For a comprehensive guide to filling all form field types, see Fill & Edit PDF Forms in C# and Programmatically Fill PDF Forms.
How do you Fill Forms from Data Objects and Dictionaries?
Real-world applications rarely hard-code form values. Instead, data comes from databases, API responses, user input, or configuration files. Organizing this data in dictionaries or custom objects makes your form-filling code more maintainable and reusable.
Input PDF
Consider a loan application form (loan-application.pdf) containing fields for applicant information: name, date of birth, SSN, address fields, employment status, annual income, and a terms agreement checkbox. The following code fills the form using data stored in a Dictionary<string, string>, where each dictionary key matches a form field name.
:path=/static-assets/pdf/content-code-examples/tutorials/pdf-forms-csharp/fill-from-dictionary.csusing 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")Sample Output
The dictionary-based approach cleanly separates data from logic. The foreach loop iterates through each key-value pair, uses FindFormField() to locate the corresponding PDF field, and assigns the value if the field exists. The null check (if (field != null)) prevents exceptions when the dictionary contains keys that do not match any field in the document—this is particularly useful when working with data sources that may include extra fields not present in the form.
For more complex scenarios, you might define a class that represents the form data, then use reflection or a mapping function to transfer property values to form fields.
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 ModuleSample Output
How do you Validate Form Data Before Filling?
Before populating a PDF form, validating your data helps catch errors early and ensures the resulting document meets requirements. While PDF forms can include their own validation rules, relying solely on those rules means errors surface only at the end of processing. Implementing validation in your C# code gives you more control and better error messages.
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 ClassThis validation layer checks for missing required fields, validates format patterns for common data types, confirms that specified dropdown values exist in the form's choices, and verifies that all field names in your data actually exist in the PDF. Catching these issues before attempting to fill the form prevents partial fills and makes debugging much easier.
How do you Create Text Input Fields in a PDF Form?
Creating PDF forms from scratch gives you complete control over the form's structure and appearance. IronPDF's HTML-to-PDF rendering engine interprets standard HTML form elements and converts them into PDF AcroForm fields. This approach lets you design forms using familiar web technologies, including CSS for styling. For the complete guide on creating forms, see Create PDF Forms in C#.
The following code creates a customer feedback form with text input fields and a textarea, using HTML and CSS to define the form structure and styling. The output PDF contains fillable form fields that users can complete in any PDF reader.
:path=/static-assets/pdf/content-code-examples/tutorials/pdf-forms-csharp/create-text-input-form.csusing 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")Sample Output
The key setting is CreatePdfFormsFromHtml = true on the RenderingOptions, which instructs the ChromePdfRenderer to convert HTML <input> and <textarea> elements into their PDF AcroForm equivalents. Without this setting, the inputs would render as static visual elements without interactivity. The name attribute on each input element becomes the field name in the resulting PDF—these are the identifiers you'll use when filling the form programmatically. Choose descriptive, consistent names that make your form-filling code readable and maintainable. For more on HTML-to-PDF conversion, see the Convert HTML to PDF in C# tutorial.
How do you Add Checkboxes and Radio Buttons to PDF Forms?
Checkboxes and radio buttons capture selection-based input. In HTML, checkboxes use type='checkbox' while radio buttons use type='radio'. Radio buttons sharing the same name attribute automatically form a mutually exclusive group.
This snippet creates an event registration form with multiple checkboxes for dietary preferences and radio buttons for ticket type selection. The HTML structure defines the form layout while CSS provides styling.
:path=/static-assets/pdf/content-code-examples/tutorials/pdf-forms-csharp/create-checkbox-radio-form.csusing 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")Sample Output
Notice that the three radio buttons share name='ticketType', creating a group where only one option can be selected. Each checkbox has a unique name because they represent independent choices that can be selected in any combination.
How do you Create Dropdown Lists in PDF Forms?
Dropdown lists conserve space while offering multiple predefined options. The HTML <select> element with <option> children creates a PDF combobox field. Users click the field to reveal the list of choices and select one.
Here we create a job application form with multiple dropdown menus for department selection, experience level, start date preference, and work location. Each <select> element contains predefined <option> values.
:path=/static-assets/pdf/content-code-examples/tutorials/pdf-forms-csharp/create-dropdown-form.csusing 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")Sample Output
The first <option> in each select element (with an empty value) serves as a placeholder prompt like "Select Department". When the PDF is rendered, these become combobox fields that users can click to reveal the full list of choices. When filling this form programmatically, set the field value to match one of the option values exactly, such as "Engineering" or "Remote".
How do you Add Signature Fields to PDF Forms?
Signature fields designate areas where users can apply digital signatures. While HTML does not have a native signature input type, IronPDF allows you to add signature fields programmatically to any PDF document.
The code below first creates a Non-Disclosure Agreement document from HTML, then programmatically adds a SignatureFormField at a specific position on the page. The signature field is positioned using PDF coordinates (x, y, width, height in points).
:path=/static-assets/pdf/content-code-examples/tutorials/pdf-forms-csharp/add-signature-field.csusing 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")Sample Output
The SignatureFormField constructor takes six parameters: the field name ("authorizedSignature"), page index (0 for the first page), and the position/size in points (x=72, y=200, width=250, height=60). PDF coordinates originate at the bottom-left corner of the page, with 72 points per inch. The signature field appears as an interactive area in PDF readers that support digital signatures—users can click this field to apply a certificate-based signature.
For more on digital signatures including certificate-based signing, see the Digital Signature Guide for PDFs in C# and C# PDF Digital Signatures Examples.
How do you Style and Position Form Elements?
CSS provides extensive control over form appearance when creating PDFs from HTML. You can adjust colors, fonts, borders, spacing, and layout to match your organization's branding or meet specific design requirements.
This snippet creates a visually polished contact request form using advanced CSS features including Google Fonts, gradient backgrounds, CSS Grid for two-column layouts, and custom form field styling with rounded corners and focus states.
:path=/static-assets/pdf/content-code-examples/tutorials/pdf-forms-csharp/styled-form-css.csusing 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")Sample Output
The HTML uses the Inter font from Google Fonts (@import url(...)), applies a gradient background to the body, and styles the form container with rounded corners and a drop shadow. The RenderingOptions sets all margins to zero and uses A4 paper size to ensure the gradient fills the entire page. Despite the sophisticated styling, all form fields remain fully interactive in the resulting PDF—users can click and type in each field.
For more on CSS rendering options and responsive layouts, see Convert HTML to PDF with Responsive CSS and Use Rendering Options in PDF.
How do you Extract Data from Filled PDF Forms?
After users complete a PDF form, your application needs to retrieve the entered values for processing, storage, or transfer to other systems. Extracting form data reverses the filling process, reading values from fields rather than writing to them.
Input PDF
Imagine you've received a completed application form (completed-application.pdf) that a user has already filled out. This is a common scenario where you receive submitted forms via email, file upload, or document management system and need to extract the data for database storage or further processing. The code below loads the filled form and extracts all field values into a dictionary.
:path=/static-assets/pdf/content-code-examples/tutorials/pdf-forms-csharp/extract-form-data.csusing 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)The extraction process iterates through the Form collection, reading each field's Name and Value properties. The null-coalescing operator (?? string.Empty) ensures that empty or unset fields return an empty string rather than null, preventing null reference issues in downstream processing. Once extracted, the dictionary provides easy access to any field value using its name as the key.
For more structured extraction, you can map form fields directly to a strongly-typed object.
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 ClassFor more examples of reading form data, see C# PDF Forms Examples and Extract PDF Form Fields Guide.
How do you Flatten a PDF Form to Lock Field Values?
Flattening a PDF form converts interactive fields into static content. The visual appearance remains identical, but users can no longer edit the values. This technique serves several purposes: creating permanent records of completed forms, preparing documents for archival, and preventing accidental or intentional modifications after submission.
Input PDF
Here we'll use a contract template (contract-template.pdf) containing fields for client name, contract date, contract value, and payment terms. This mirrors a typical workflow where you fill a template with specific deal information and then lock the fields to create a finalized, tamper-resistant document. The following code loads the template, fills in the contract details, and sets all fields to read-only.
:path=/static-assets/pdf/content-code-examples/tutorials/pdf-forms-csharp/flatten-form-readonly.csusing 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")Sample Output
The code first populates fields like clientName, contractDate, contractValue, and paymentTerms with the contract details. The foreach loop then iterates through every field in the form and sets field.ReadOnly = true, which locks each field against further editing. After saving, users can still view the form values in any PDF reader, but clicking the fields will not allow modification. This technique creates permanent records suitable for archival or legal documentation.
For additional PDF security options including password protection, see the Sign and Secure PDFs Tutorial and PDF Password Protection Guide.
How do you Pre-Fill PDF Forms for End Users?
Pre-filling forms with known information improves user experience by reducing data entry. When you already have customer information in your system, you can populate form fields before sending the document, letting users review and complete only the remaining fields.
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 ClassSample Output
This pattern works well for renewal forms, application updates, annual reviews, and any scenario where partial information already exists in your system. The user receives a document with their details already populated, needing only to provide new or changing information.
How do you Build a Form Data Processing Pipeline?
Processing form submissions at scale requires a structured approach that handles extraction, validation, transformation, and storage. A simple pipeline pattern keeps this process organized and maintainable.
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 ClassThis pipeline structure can be extended to include database storage, email notifications, integration with external APIs, or any other processing steps your workflow requires. The key is maintaining separation between extraction, validation, and downstream processing.
For high-performance batch processing, see the Async PDF Generation Guide which covers multithreading and parallel processing patterns.
How do you Automate IRS Tax Forms like the W-9?
Government forms represent some of the highest-volume PDF processing scenarios. The W-9 (Request for Taxpayer Identification Number) alone is filled millions of times annually by businesses onboarding vendors and contractors. Every company that pays independent contractors needs W-9s on file, and automating this collection process can save significant administrative time.
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 ModuleGovernment forms often use complex hierarchical field naming conventions. The W-9 field names shown above follow the XFA-derived naming pattern used in official IRS PDFs. When working with government forms, always download the latest version from the official agency website and examine its field structure before writing automation code.
How do you Process Forms in Batch with Variable Data?
Batch processing generates multiple completed forms from a data source such as a database query, CSV file, or API response. This capability proves essential for scenarios like annual tax document distribution, employee onboarding packets, or customer statement generation.
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 ModuleFor very large batches, consider implementing parallel processing with controlled concurrency to maximize throughput without overwhelming system resources.
How do you Ensure Compliance with Government PDF Standards?
Government agencies often require documents to meet specific PDF standards for long-term archival and accessibility. PDF/A is an ISO-standardized subset of PDF designed for reliable long-term preservation of electronic documents. IronPDF supports exporting documents in PDF/A format to meet these compliance requirements.
The following code creates an official government form from HTML, fills it with applicant data, and saves it as a PDF/A-3b compliant document using the SaveAsPdfA() method. This format ensures documents remain readable decades into the future and meets most government submission requirements.
:path=/static-assets/pdf/content-code-examples/tutorials/pdf-forms-csharp/pdfa-compliance.csusing 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)Sample Output
The SaveAsPdfA() method takes the output filename and a PdfAVersions enum value (such as PdfA3b). During the save process, IronPDF automatically embeds all fonts, standardizes color spaces, strips any encryption, and structures the metadata according to PDF/A requirements. These transformations ensure the document remains readable and visually identical decades into the future, regardless of what software attempts to open it.
When submitting forms to government agencies, check their specific requirements. Some may require PDF/A-1b for basic archival, while others specify PDF/A-3b to allow embedded attachments like XML data files. The IRS, for instance, accepts specific PDF versions for various submission types, and immigration forms often have their own technical specifications.
For complete PDF/A conversion documentation, see Convert PDF to PDF/A-3b in C# and the PDF/A Compliance Guide. For accessibility standards, see Create PDF/UA Documents.
Next Steps
You now have everything you need to automate those PDF form workflows that eat up hours of manual data entry. Whether you're processing W-9 forms for vendor onboarding, pre-filling patient intake forms from your EHR system, or pulling submitted application data into your CRM, the techniques in this guide translate directly to the forms sitting in your inbox right now.
To move forward, you should first automate your first form workflow by grabbing a PDF form your team actually uses, such as a customer application or employee onboarding packet and building a simple template based pipeline using the dictionary approach from this guide. From there, you can add form validation patterns to catch missing fields and format errors before generating documents; your reviewers will thank you. Finally, scale to batch processing if you handle forms in volume, adapting the examples here to manage your monthly vendor forms, quarterly compliance filings, or annual tax documents.
With PDF AcroForms C# manipulation through IronPDF, you can fill PDF form C# programmatically using the same field names you see in any PDF reader. No special tooling is needed to figure out the form structure. Furthermore, the ability to create PDF forms from HTML means you get complete design flexibility while keeping full AcroForm compatibility.
Ready to stop copying data by hand? Download a free trial of IronPDF and start automating your PDF forms today. You get a development license, full API access to all the form features covered in this guide, and access to technical support when you need help integrating form automation into your .NET applications.
Related Resources
- Fill & Edit PDF Forms in C#: API reference for form field manipulation.
- Create PDF Forms in C#: A complete guide to building forms from HTML.
- Digital Signature Guide for PDFs: Adding signature fields and certificate-based signing.
- PDF/A Compliance Guide: Meeting government archival standards.
Frequently Asked Questions
How can I create PDF forms using C#?
You can create PDF forms in C# by using IronPDF to convert HTML forms into PDF documents. This allows for easy customization and design of interactive forms.
Is it possible to programmatically fill existing PDF forms in C#?
Yes, IronPDF allows you to programmatically fill existing PDF forms in C# by accessing and modifying form fields using C# code.
Can I extract data from PDF forms using IronPDF?
IronPDF provides functionality to extract form data from PDF documents, making it easy to retrieve and process information entered into PDF forms.
What does it mean to flatten PDF forms?
Flattening PDF forms involves converting form fields into static content, which prevents further editing. IronPDF can flatten forms to ensure data integrity and prevent changes after form submission.
How can I automate processing of government forms like W-9?
IronPDF can automate the processing of government forms like W-9 by enabling you to fill, extract, and manage form data programmatically, streamlining your workflow.
What are the advantages of using HTML for creating PDF forms?
Using HTML to create PDF forms with IronPDF offers flexibility in design and ease of integration with existing web technologies, allowing for dynamic and responsive form creation.
Can IronPDF handle interactive form elements?
Yes, IronPDF supports interactive form elements such as text fields, checkboxes, and buttons, enabling the creation and manipulation of dynamic PDF forms in C#.
Is it possible to customize the appearance of PDF forms?
IronPDF allows full customization of PDF forms' appearance, enabling you to apply CSS styles to form elements for a professional and consistent look.






