Programmatically Fill PDF Forms in C# (Coding Tutorial)

PDF documents are widely used across the world. The main reason for this is that this format is compatible with every kind of OS system. Whether we have Android or iPhone, Mac, Windows, or Linux, PDFs can work on all of them without the need for any third-party app.

This is also the reason why PDF forms are also used for filling out questionnaires, registration forms, surveys, feedback, government forms, tax forms, and many more. In fact, we couldn't possibly count all the instances where PDF forms are used. Therefore, it is often considered necessary to be able to fill PDF forms programmatically.

It helps enormously to be able to get data from a database and fill out all necessary forms in a single go. This saves time and reduces the margin for error.

There are multiple Dot Net libraries out there on the market that allow us to fill PDF forms programmatically in C#. Some of them are difficult to understand, and some of them need to be paid for.

IronPDF is the best Dot Net core library as it is easy to understand and free for development. Apart from filling PDF forms, IronPDF also allows us to create new PDFs from HTML string, HTML files, and URLs.

Let's take a look at how we can fill PDF forms programmatically using C#. First of all, we need to create a new project. I will be using a console application for the purposes of demonstration, but you can use any as per your requirement.

Create a Visual Studio Project

Open Microsoft Visual Studio. Click on Create New Project > Select console application from templates > Press Next > Name your Project. Press Next > Select Target Framework. Click the Create Button. The project will be created as shown below.

Install the IronPDF Library

As discussed before, we are going to use the IronPDF library in our tutorial. The main reason for using this .Net library is that it is free for development and provides all features in a single library.

Go to the Package Manager Console. Type the following command:

Install-Package IronPDF

This command will install the IronPDF library for us. Next, let's begin the coding.

Read PDF Documents

The first step to filling a PDF form is actually reading the PDF document. Obviously, how could we fill the form without reading it first? I am using the following PDF document for the demonstration. You can download it from here, or you may use your own document.

The code to read this file is:

PdfDocument doc =  PdfDocument.FromFile(@"D:\myPdfForm.pdf");
PdfDocument doc =  PdfDocument.FromFile(@"D:\myPdfForm.pdf");
Dim doc As PdfDocument = PdfDocument.FromFile("D:\myPdfForm.pdf")
VB   C#

Pass the complete path of the PDF document inside the FromFile() method. This will read the PDF file from your local system.

Get PDF Forms

Write the following line of code to get the form from the loaded PDF document.

var form = doc.Form;
var form = doc.Form;
Dim form = doc.Form
VB   C#

Get Form Fields

We need to get the form fields in order to set their value. IronPDF makes this very easy for us. We can access the form fields using two methods: either by field name or via the index. Let's discuss both one by one.

Get form Field by Name

The following code will get the field by name:

var  field = form.GetFieldByName("First Name");
var  field = form.GetFieldByName("First Name");
Dim field = form.GetFieldByName("First Name")
VB   C#

GetFieldByName() takes the field name as the argument. This is fault-tolerant, and will attempt to match case mistakes and partial field names.

Get Form Field by Index

We can also get PDF form fields by using the index. The index starts from zero. The following sample code is used to get form fields by index.

var field = form.Fields[0];
var field = form.Fields[0];
Dim field = form.Fields(0)
VB   C#

Fill PDF Forms

Next, let's combine all the code to fill the PDF form.

PdfDocument doc =  PdfDocument.FromFile(@"D:\myPdfForm.pdf");
var form = doc.Form;
form.Fields[0].Value = "John";
form.Fields[1].Value = "Smith";
form.Fields[2].Value = "+19159969739";
form.Fields[3].Value = "John@email.com";
form.Fields[4].Value = "Chicago";
doc.SaveAs(@"D:\myPdfForm.pdf");
PdfDocument doc =  PdfDocument.FromFile(@"D:\myPdfForm.pdf");
var form = doc.Form;
form.Fields[0].Value = "John";
form.Fields[1].Value = "Smith";
form.Fields[2].Value = "+19159969739";
form.Fields[3].Value = "John@email.com";
form.Fields[4].Value = "Chicago";
doc.SaveAs(@"D:\myPdfForm.pdf");
Dim doc As PdfDocument = PdfDocument.FromFile("D:\myPdfForm.pdf")
Dim form = doc.Form
form.Fields(0).Value = "John"
form.Fields(1).Value = "Smith"
form.Fields(2).Value = "+19159969739"
form.Fields(3).Value = "John@email.com"
form.Fields(4).Value = "Chicago"
doc.SaveAs("D:\myPdfForm.pdf")
VB   C#

We have set the field values by using the index. We can also set the field value using the field name as already discussed.

The sample code above will fill the form fields. Let's run the program to see the output.

Filled PDF Form

Our form has been filled as shown below:

You can see that we have filled the PDF form with the simplest code, and we have not used any complex logic. This is the reason I like IronPDF.

Let's suppose we don't have any existing PDF documents with forms — don't worry, we can generate our new PDF forms using IronPDF. Follow the steps below:

Generate a new PDF form document

Create A New HTML File

Create a new HTML file and paste the following code:

<!DOCTYPE html>
<html>
<body>

<h2>PDF Forms</h2>

<form action="/action_page.php">
  <label for="fname">First name:</label><br>
  <input type="text" id="fname" name="fname" ><br>
  <label for="lname">Last name:</label><br>
  <input type="text" id="lname" name="lname" ><br>
  <label for="contact">Contact #:</label><br>
  <input type="text" id="contact" name="contact" ><br>
  <label for="email">Email:</label><br>
  <input type="text" id="email" name="email"><br>
  <label for="city">City:</label><br>
  <input type="text" id="city" name="city"><br>

</form> 
</body>
</html>

Save this example HTML File. You can customize this HTML as per your form requirement.

Next, write the following code in your C# Program.

var renderer = new ChromePdfRenderer();
var file = renderer.RenderHtmlFileAsPdf(@"D:\myForm.html");
file.SaveAs(@"D:\myForm.pdf");
var renderer = new ChromePdfRenderer();
var file = renderer.RenderHtmlFileAsPdf(@"D:\myForm.html");
file.SaveAs(@"D:\myForm.pdf");
Dim renderer = New ChromePdfRenderer()
Dim file = renderer.RenderHtmlFileAsPdf("D:\myForm.html")
file.SaveAs("D:\myForm.pdf")
VB   C#

Run the program to see the resulting PDF forms document.

Summary

In this article, we have understood the importance of filling out PDF forms programmatically. We have learned to fill PDF forms in C# with the simplest code by using IronPDF. We have also generated new PDF forms from scratch.

There are other many useful libraries such as IronBarcode for working with barcodes, IronXL for working with Excel documents, and IronOCR for working with OCR. You can get all five libraries for the price of just two by purchasing the complete Iron Suite. For more details, please click here.

I hope you have liked the article, and please feel free to ask any questions in the comments section.