Articles in this section
Category / Section

How to add form fields in an existing PDF file using C# and VB.NET

5 mins read

Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can add form fields in an existing PDF file using C# and VB.NET.

Steps to add a form fields in an existing PDF document programmatically:

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

C#

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

 

VB.NET

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

 

  1. Use the following code snippet to add a form fields in existing PDF document.

C#

//Load the existing PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Create the form if the form does not exist in the loaded document
if (loadedDocument.Form == null)
    loadedDocument.CreateForm();
//Load the page
PdfLoadedPage loadedPage = loadedDocument.Pages[0] as PdfLoadedPage;
 
//Set the standard font
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 16);
//Draw the string      
loadedPage.Graphics.DrawString("Job Application", font, PdfBrushes.Black, new PointF(250, 30));
font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
 
loadedPage.Graphics.DrawString("Name", font, PdfBrushes.Black, new PointF(30, 50));
//Create a text box field for name
PdfTextBoxField textBoxField1 = new PdfTextBoxField(loadedPage, "Name");
textBoxField1.Bounds = new RectangleF(30, 70, 200, 20);
textBoxField1.ToolTip = "Name";
loadedDocument.Form.Fields.Add(textBoxField1);
 
loadedPage.Graphics.DrawString("Email address", font, PdfBrushes.Black, new PointF(30, 110));
//Create a text box field for email address
PdfTextBoxField textBoxField3 = new PdfTextBoxField(loadedPage, "Email address");
textBoxField3.Bounds = new RectangleF(30, 130, 200, 20);
textBoxField3.ToolTip = "Email address";
loadedDocument.Form.Fields.Add(textBoxField3);
 
loadedPage.Graphics.DrawString("Gender", font, PdfBrushes.Black, new PointF(30, 180));
//Create radio button for gender
PdfRadioButtonListField employeesRadioList = new PdfRadioButtonListField(loadedPage, "Gender");
loadedDocument.Form.Fields.Add(employeesRadioList);
loadedPage.Graphics.DrawString("Male", font, PdfBrushes.Black, new PointF(60, 200));
PdfRadioButtonListItem radioButtonItem1 = new PdfRadioButtonListItem("Male");
radioButtonItem1.Bounds = new RectangleF(30, 200, 20, 20);
loadedPage.Graphics.DrawString("Female", font, PdfBrushes.Black, new PointF(160, 200));
PdfRadioButtonListItem radioButtonItem2 = new PdfRadioButtonListItem("Female");
radioButtonItem2.Bounds = new RectangleF(130, 200, 20, 20);
employeesRadioList.Items.Add(radioButtonItem1);
employeesRadioList.Items.Add(radioButtonItem2);
 
loadedPage.Graphics.DrawString("Which position you are applying for?", font, PdfBrushes.Black, new PointF(30, 240));
//Create combo box for position
PdfComboBoxField comboBox = new PdfComboBoxField(loadedPage, "JobTitle");
comboBox.Bounds = new RectangleF(30, 260, 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"));
loadedDocument.Form.Fields.Add(comboBox);
 
//Save the document
loadedDocument.Save("Form.pdf");
//Close the document
loadedDocument.Close(true);
//This will open the PDF file so, the result will be seen in default PDF Viewer 
Process.Start("Form.pdf");
 

 

VB.NET

'Load the existing PDF document
Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument("Input.pdf")
'Create the form if the form does not exist in the loaded document
If loadedDocument.Form Is Nothing Then loadedDocument.CreateForm()
'Load the page
Dim loadedPage As PdfLoadedPage = TryCast(loadedDocument.Pages(0), PdfLoadedPage)
 
'Set the standard font
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 16)
'Draw the string     
loadedPage.Graphics.DrawString("Job Application", font, PdfBrushes.Black, New PointF(250, 30))
font = New PdfStandardFont(PdfFontFamily.Helvetica, 12)
 
loadedPage.Graphics.DrawString("Name", font, PdfBrushes.Black, New PointF(30, 50))
'Create a text box field for name
Dim textBoxField1 As PdfTextBoxField = New PdfTextBoxField(loadedPage, "Name")
textBoxField1.Bounds = New RectangleF(30, 70, 200, 20)
textBoxField1.ToolTip = "Name"
loadedDocument.Form.Fields.Add(textBoxField1)
loadedPage.Graphics.DrawString("Email address", font, PdfBrushes.Black, New PointF(30, 110))
'Create a text box field for email address
Dim textBoxField3 As PdfTextBoxField = New PdfTextBoxField(loadedPage, "Email address")
textBoxField3.Bounds = New RectangleF(30, 130, 200, 20)
textBoxField3.ToolTip = "Email address"
loadedDocument.Form.Fields.Add(textBoxField3)
 
loadedPage.Graphics.DrawString("Gender", font, PdfBrushes.Black, New PointF(30, 180))
'Create radio button for gender
Dim employeesRadioList As PdfRadioButtonListField = New PdfRadioButtonListField(loadedPage, "Gender")
loadedDocument.Form.Fields.Add(employeesRadioList)
loadedPage.Graphics.DrawString("Male", font, PdfBrushes.Black, New PointF(60, 200))
Dim radioButtonItem1 As PdfRadioButtonListItem = New PdfRadioButtonListItem("Male")
radioButtonItem1.Bounds = New RectangleF(30, 200, 20, 20)
loadedPage.Graphics.DrawString("Female", font, PdfBrushes.Black, New PointF(160, 200))
Dim radioButtonItem2 As PdfRadioButtonListItem = New PdfRadioButtonListItem("Female")
radioButtonItem2.Bounds = New RectangleF(130, 200, 20, 20)
employeesRadioList.Items.Add(radioButtonItem1)
employeesRadioList.Items.Add(radioButtonItem2)
 
loadedPage.Graphics.DrawString("Which position you are applying for?", font, PdfBrushes.Black, New PointF(30, 240))
'Create combo box for position
Dim comboBox As PdfComboBoxField = New PdfComboBoxField(loadedPage, "JobTitle")
comboBox.Bounds = New RectangleF(30, 260, 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"))
loadedDocument.Form.Fields.Add(comboBox)
 
'Save the document
loadedDocument.Save("Form.pdf")
'Close the document
loadedDocument.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 PDFFormSample.zip.

By executing the program, you will get the PDF document as follows. Output document screenshot

Take a moment to peruse the documentation, where you can find the options like creating a form in new PDF document, 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 to PDF, etc., with code examples.

Refer here to learn more about Syncfusion 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.

 

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