Articles in this section
Category / Section

How to create PDF forms in C#, VB.NET?

5 mins read

An interactive form, sometimes referred to as an AcroForm, is a collection of fields for gathering information interactively from the user. A PDF document may contain any number of fields appearing on any combination of pages, which make a single and global interactive form spanning the entire document.

Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can create interactive PDF forms in C# and VB.NET.

Steps to create a PDF forms programmatically:

  1. Create a new C# console application project. Create new console application
  2. Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org. Install nuget packages
  3. Include the following namespace in the Program.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Interactive;
using System.Drawing;

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports Syncfusion.Pdf.Interactive
Imports System.Drawing

 

  1. Use the following code snippet to create a PDF forms.

C#

//Create a new PDF document
PdfDocument document = new PdfDocument();
//Add a new page to the PDF document
PdfPage page = document.Pages.Add();
//Set the standard font
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 16);   
//Draw the string      
page.Graphics.DrawString("Job Application", font, PdfBrushes.Black, new PointF(250, 0));
font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
page.Graphics.DrawString("Name", font, PdfBrushes.Black, new PointF(10, 20));
 
//Create a text box field for name
PdfTextBoxField textBoxField1 = new PdfTextBoxField(page, "Name");
textBoxField1.Bounds = new RectangleF(10, 40, 200, 20);
textBoxField1.ToolTip = "Name";
document.Form.Fields.Add(textBoxField1);
 
page.Graphics.DrawString("Email address", font, PdfBrushes.Black, new PointF(10, 80));
//Create a text box field for email address
PdfTextBoxField textBoxField3 = new PdfTextBoxField(page, "Email address");
textBoxField3.Bounds = new RectangleF(10, 100, 200, 20);
textBoxField3.ToolTip = "Email address";
document.Form.Fields.Add(textBoxField3);
 
page.Graphics.DrawString("Phone", font, PdfBrushes.Black, new PointF(10, 140));
//Create a text box field for phone
PdfTextBoxField textBoxField4 = new PdfTextBoxField(page, "Phone");
textBoxField4.Bounds = new RectangleF(10, 160, 200, 20);
textBoxField4.ToolTip = "Phone";
document.Form.Fields.Add(textBoxField4);
 
page.Graphics.DrawString("Gender", font, PdfBrushes.Black, new PointF(10, 200));
//Create radio button for gender
PdfRadioButtonListField employeesRadioList = new PdfRadioButtonListField(page, "Gender");
document.Form.Fields.Add(employeesRadioList);
page.Graphics.DrawString("Male", font, PdfBrushes.Black, new PointF(40, 220));
PdfRadioButtonListItem radioButtonItem1 = new PdfRadioButtonListItem("Male");
radioButtonItem1.Bounds = new RectangleF(10, 220, 20, 20);
page.Graphics.DrawString("Female", font, PdfBrushes.Black, new PointF(140, 220));
PdfRadioButtonListItem radioButtonItem2 = new PdfRadioButtonListItem("Female");
radioButtonItem2.Bounds = new RectangleF(110, 220, 20, 20);
employeesRadioList.Items.Add(radioButtonItem1);
employeesRadioList.Items.Add(radioButtonItem2);
 
page.Graphics.DrawString("Which position you are applying for?", font, PdfBrushes.Black, new PointF(10, 260));
//Create combo box for position
PdfComboBoxField comboBox = new PdfComboBoxField(page, "JobTitle");
comboBox.Bounds = new RectangleF(10, 280, 100, 20);
comboBox.BorderColor = new PdfColor(Color.Gray);
comboBox.ToolTip = "Job Title";
comboBox.Items.Add(new PdfListFieldItem("Development", "Development"));
comboBox.Items.Add(new PdfListFieldItem("Support", "Support"));
comboBox.Items.Add(new PdfListFieldItem("Documentation", "Documentation"));
document.Form.Fields.Add(comboBox);
 
page.Graphics.DrawString("Languages Known", font, PdfBrushes.Black, new PointF(10, 320));
//Create list box field for languages
PdfListBoxField listBoxField = new PdfListBoxField(page, "Languages");
listBoxField.Bounds = new RectangleF(10, 340, 100, 50);
listBoxField.Items.Add(new PdfListFieldItem("English", "English"));
listBoxField.Items.Add(new PdfListFieldItem("French", "French"));
listBoxField.Items.Add(new PdfListFieldItem("German", "German"));
document.Form.Fields.Add(listBoxField);
 
page.Graphics.DrawString("Highest Qualification", font, PdfBrushes.Black, new PointF(10, 410));
//Add checked box field for associate degree
PdfCheckBoxField checkBoxField1 = new PdfCheckBoxField(page, "Associate degree");
page.Graphics.DrawString("Associate degree", font, PdfBrushes.Black, new PointF(30, 430));
checkBoxField1.ToolTip = "Associate degree";
checkBoxField1.Bounds = new RectangleF(10, 430, 10, 10);
document.Form.Fields.Add(checkBoxField1);
 
//Add checked box field for bachelor degree
PdfCheckBoxField checkBoxField2 = new PdfCheckBoxField(page, "Bachelor degree");
page.Graphics.DrawString("Bachelor degree", font, PdfBrushes.Black, new PointF(30, 450));
checkBoxField2.ToolTip = "Bachelor degree";
checkBoxField2.Bounds = new RectangleF(10, 450, 10, 10);
document.Form.Fields.Add(checkBoxField2);
 
//Add checked box field for Post Graduate
PdfCheckBoxField checkBoxField3 = new PdfCheckBoxField(page, "Post Graduate");
page.Graphics.DrawString("Post Graduate", font, PdfBrushes.Black, new PointF(30, 470));
checkBoxField3.ToolTip = "Post Graduate";
checkBoxField3.Bounds = new RectangleF(10, 470, 10, 10);
document.Form.Fields.Add(checkBoxField3);
 
//Save the document
document.Save("Form.pdf");
//Close the document
document.Close(true);
//This will open the PDF file so, the result will be seen in default PDF Viewer 
Process.Start("Form.pdf");      

 

VB.NET

'Create a new PDF document
Dim document As PdfDocument = New PdfDocument()
'Add a new page to the PDF document
Dim page As PdfPage = document.Pages.Add()
'Set the standard font
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 16)
'Draw the string  
page.Graphics.DrawString("Job Application", font, PdfBrushes.Black, New PointF(250, 0))
 
font = New PdfStandardFont(PdfFontFamily.Helvetica, 12)
page.Graphics.DrawString("Name", font, PdfBrushes.Black, New PointF(10, 20))
'Create a text box field for name
Dim textBoxField1 As PdfTextBoxField = New PdfTextBoxField(page, "Name")
textBoxField1.Bounds = New RectangleF(10, 40, 200, 20)
textBoxField1.ToolTip = "Name"
document.Form.Fields.Add(textBoxField1)
 
page.Graphics.DrawString("Email address", font, PdfBrushes.Black, New PointF(10, 80))
'Create a text box field for email address
Dim textBoxField3 As PdfTextBoxField = New PdfTextBoxField(page, "Email address")
textBoxField3.Bounds = New RectangleF(10, 100, 200, 20)
textBoxField3.ToolTip = "Email address"
document.Form.Fields.Add(textBoxField3)
 
page.Graphics.DrawString("Phone", font, PdfBrushes.Black, New PointF(10, 140))
'Create a text box field for phone
Dim textBoxField4 As PdfTextBoxField = New PdfTextBoxField(page, "Phone")
textBoxField4.Bounds = New RectangleF(10, 160, 200, 20)
textBoxField4.ToolTip = "Phone"
document.Form.Fields.Add(textBoxField4)
 
page.Graphics.DrawString("Gender", font, PdfBrushes.Black, New PointF(10, 200))
'Create radio button for gender
Dim employeesRadioList As PdfRadioButtonListField = New PdfRadioButtonListField(page, "Gender")
document.Form.Fields.Add(employeesRadioList)
page.Graphics.DrawString("Male", font, PdfBrushes.Black, New PointF(40, 220))
Dim radioButtonItem1 As PdfRadioButtonListItem = New PdfRadioButtonListItem("Male")
radioButtonItem1.Bounds = New RectangleF(10, 220, 20, 20)
page.Graphics.DrawString("Female", font, PdfBrushes.Black, New PointF(140, 220))
Dim radioButtonItem2 As PdfRadioButtonListItem = New PdfRadioButtonListItem("Female")
radioButtonItem2.Bounds = New RectangleF(110, 220, 20, 20)
employeesRadioList.Items.Add(radioButtonItem1)
employeesRadioList.Items.Add(radioButtonItem2)
 
page.Graphics.DrawString("Which position you are applying for?", font, PdfBrushes.Black, New PointF(10, 260))
'Create combo box for position
Dim comboBox As PdfComboBoxField = New PdfComboBoxField(page, "JobTitle")
comboBox.Bounds = New RectangleF(10, 280, 100, 20)
comboBox.BorderColor = New PdfColor(Color.Gray)
comboBox.ToolTip = "Job Title"
comboBox.Items.Add(New PdfListFieldItem("Development", "Development"))
comboBox.Items.Add(New PdfListFieldItem("Support", "Support"))
comboBox.Items.Add(New PdfListFieldItem("Documentation", "Documentation"))
document.Form.Fields.Add(comboBox)
 
page.Graphics.DrawString("Languages Known", font, PdfBrushes.Black, New PointF(10, 320))
'Create list box field for languages
Dim listBoxField As PdfListBoxField = New PdfListBoxField(page, "Languages")
listBoxField.Bounds = New RectangleF(10, 340, 100, 50)
listBoxField.Items.Add(New PdfListFieldItem("English", "English"))
listBoxField.Items.Add(New PdfListFieldItem("French", "French"))
listBoxField.Items.Add(New PdfListFieldItem("German", "German"))
document.Form.Fields.Add(listBoxField)
 
page.Graphics.DrawString("Highest Qualification", font, PdfBrushes.Black, New PointF(10, 410))
'Add checked box field for associate degree
Dim checkBoxField1 As PdfCheckBoxField = New PdfCheckBoxField(page, "Associate degree")
page.Graphics.DrawString("Associate degree", font, PdfBrushes.Black, New PointF(30, 430))
checkBoxField1.ToolTip = "Associate degree"
checkBoxField1.Bounds = New RectangleF(10, 430, 10, 10)
document.Form.Fields.Add(checkBoxField1)
 
'Add checked box field for bachelor degree
Dim checkBoxField2 As PdfCheckBoxField = New PdfCheckBoxField(page, "Bachelor degree")
page.Graphics.DrawString("Bachelor degree", font, PdfBrushes.Black, New PointF(30, 450))
checkBoxField2.ToolTip = "Bachelor degree"
checkBoxField2.Bounds = New RectangleF(10, 450, 10, 10)
document.Form.Fields.Add(checkBoxField2)
 
'Add checked box field for Post Graduate
Dim checkBoxField3 As PdfCheckBoxField = New PdfCheckBoxField(page, "Post Graduate")
page.Graphics.DrawString("Post Graduate", font, PdfBrushes.Black, New PointF(30, 470))
checkBoxField3.ToolTip = "Post Graduate"
checkBoxField3.Bounds = New RectangleF(10, 470, 10, 10)
document.Form.Fields.Add(checkBoxField3)
 
'Save the document
document.Save("Form.pdf")
'Close the document 
document.Close(True)
'This will open the PDF file so, the result will be seen in default PDF Viewer
Process.Start("Form.pdf")

 

Download the work sample from CreatePDFFormSample.Zip

By executing the program, you will get the PDF document as follows. Screenshot of output PDF file

Take a moment to peruse the documentation, where you find other options like modifying existing form fields, filling form fields, removing editing capability, removing form fields, importing FDF file to PDF, export PDF file to FDF, adding actions to form fields and features like XFA Form, adding annotation and JavaScript actions to PDF, etc., with code examples.

Refer here to explore the rich set of Syncfusion Essential PDF features.

An online sample link to generate PDF Forms

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

Conclusion

I hope you enjoyed learning about how to create pdf forms in C# and VB.NET. 

You can refer to our .NET PDF Framework’s feature tour page to know about its other groundbreaking feature representations and documentation to know about how to quickly getting started for configuration specifications. You can also explore our PDF example to understand how to create and manipulate data in .NET PDF. 

For current customers, you can check out our Document processing libraries from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our JavaScript Context Menu and other JavaScript controls.

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied