Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
This tutorial will demonstrate how to interact with forms in PDF files programmatically.
There are multiple .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 .NET Core library as it is easy to understand and free for development. Apart from filling PDF forms, IronPDF also allows to create new PDFs from HTML String, HTML files, and URLs.
Let's take a look at how to fill PDF forms programmatically using C#. First of all, a Console Application will be created for demonstration, but you can use any as per your requirement.
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.
a newly created Console Application in Visual Studio
As discussed before, the IronPDF library will be used in this 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.
The first step to filling out a PDF form is reading the PDF document. Obviously, how could we fill out the form without reading it first? The following PDF document will be used for the demonstration. You can download it from here, or you may use your document.
The sample PDF file to fill out form
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")
Pass the complete path of the PDF document inside the FromFile
method. This will read the PDF file from your local system.
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
To get the form fields to set their value, IronPDF makes this very easy by accessing the form fields using two methods: either by field name or via the index. Let's discuss both one by one.
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")
The GetFieldByName
method takes the field name as the argument. This is fault-tolerant and will attempt to match case mistakes and partial field names.
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)
Next, let's combine all the code to fill out 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")
The sample code above will fill the form fields by index values. You can also do the same using the field names mentioned earlier. Let's run the program to see the output.
The filled form in the sample PDF file
You can see that the library can fill the PDF form with the simplest code, without any need for complex logic. This is the reason IronPDF is recommended.
Let's suppose you do not yet have any PDF documents with forms — don't worry, IronPDF provides full support to generate PDF forms. Follow the steps below:
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>
<!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")
Run the program to see the resulting PDF forms document.
The PDF form that generated from an HTML file
It is important to automatically and programmatically fill out PDF forms. In this article, the easiest approach is suggested for filling PDF forms in C# using IronPDF. Additionally, you also learned how to generate new PDF forms from scratch.
Additionally, IronPDF also offers developers methods to extract text and content from a PDF, render charts in PDFs, insert barcodes, enhance security with passwords and watermark programmatically.
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. Please visit the licensing page for more details.
9 .NET API products for your office documents