Fill Form Fields in PDF File in C#

We have already demonstrated how to create form fields in PDF file. This article mainly shows you how developers fill form field in PDF in C# only with 3 simple steps by using a standalone .NET PDF component Spire.PDF.

Make sure Spire.PDF for .NET has been installed correctly and then add Spire.Pdf.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Pdf\Bin\NET4.0\ Spire.Pdf.dll". Here comes to the details of how developers Fill Form Fields by using Spire.PDF:

Step 1: Open the form that needs to fill the data

[C#]
//Create a pdf document
PdfDocument doc = new PdfDocument();
//Load from file
doc.LoadFromFile(@"..\..\FormField.pdf");

Step 2: Update the data to the form

[C#]
//Get form
PdfFormWidget formWidget = doc.Form as PdfFormWidget;
for (int i = 0; i < formWidget.FieldsWidget.List.Count; i++)
            {
                PdfField field = formWidget.FieldsWidget.List[i] as PdfField;


//Fill the data for textBoxField
if (field is PdfTextBoxFieldWidget)
   {
    PdfTextBoxFieldWidget textBoxField = field as PdfTextBoxFieldWidget;
    switch (textBoxField.Name)
     {

     case "email":
        textBoxField.Text = "support@e-iceblue.com";
    break;
     case "password":
        textBoxField.Password = true;
        textBoxField.Text = "e-iceblue";

//Fill the data for listBoxField
if (field is PdfListBoxWidgetFieldWidget)
  {
     PdfListBoxWidgetFieldWidget listBoxField = field as PdfListBoxWidgetFieldWidget;
     switch (listBoxField.Name)
       {

        case "email_format":
         int[] index = { 1 };
         listBoxField.SelectedIndex = index;
         break;


//Fill the data for comBoxField
if (field is PdfComboBoxWidgetFieldWidget)
  {
   PdfComboBoxWidgetFieldWidget comBoxField = field as PdfComboBoxWidgetFieldWidget;
   switch (comBoxField.Name)
    {

     case "title":
       int[] items = { 0 };
       comBoxField.SelectedIndex = items;
       break;


//Fill the data for checkBoxField
if (field is PdfCheckBoxWidgetFieldWidget)
  {
   PdfCheckBoxWidgetFieldWidget checkBoxField = field as PdfCheckBoxWidgetFieldWidget;
   switch (checkBoxField.Name)
    {

    case "agreement_of_terms":
      checkBoxField.Checked = true;
      break;

Step 3: Save and launch file

[C#]
//Save the document to file in PDF format
doc.SaveToFile(@"../../FormFieldData.pdf.pdf");
//Launch the file
System.Diagnostics.Process.Start(@"../../FormFieldData.pdf.pdf");

Effective Screenshot:

The original PDF with form fields:

before fill form fields

After fill the form fields:

after fill form fields

View the full code as below:

[C#]
static void Filldata()
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"..\..\FormField.pdf");
            PdfFormWidget formWidget = doc.Form as PdfFormWidget;
            for (int i = 0; i < formWidget.FieldsWidget.List.Count; i++)
            {
                PdfField field = formWidget.FieldsWidget.List[i] as PdfField;
                if (field is PdfTextBoxFieldWidget)
                {
                    PdfTextBoxFieldWidget textBoxField = field as PdfTextBoxFieldWidget;
                    switch (textBoxField.Name)
                    {
                        case "email":
                            textBoxField.Text = "support@e-iceblue.com";
                            break;
                        case "username":
                            textBoxField.Text = "E-iceblue";
                            break;
                        case "password":
                            textBoxField.Password = true;
                            textBoxField.Text = "e-iceblue";
                            break;
                        case "password2":
                            textBoxField.Password = true;
                            textBoxField.Text = "e-iceblue";
                            break;
                        case "company_name ":
                            textBoxField.Text = "E-iceblue";
                            break;
                        case "first_name":
                            textBoxField.Text = "James";
                            break;
                        case "last_name":
                            textBoxField.Text = "Chen";
                            break;
                        case "middle_name":
                            textBoxField.Text = "J";
                            break;
                        case "address1":
                            textBoxField.Text = "Chengdu";
                            break;
                        case "address2":
                            textBoxField.Text = "Beijing";
                            break;
                        case "city":
                            textBoxField.Text = "Shanghai";
                            break;
                        case "postal_code":
                            textBoxField.Text = "11111";
                            break;
                        case "state":
                            textBoxField.Text = "Shanghai";
                            break;
                        case "phone":
                            textBoxField.Text = "1234567901";
                            break;
                        case "mobile_phone":
                            textBoxField.Text = "123456789";
                            break;
                        case "fax":
                            textBoxField.Text = "12121212";
                            break;
                    }
                }
                if (field is PdfListBoxWidgetFieldWidget)
                {
                    PdfListBoxWidgetFieldWidget listBoxField = field as PdfListBoxWidgetFieldWidget;
                    switch (listBoxField.Name)
                    {
                        case "email_format":
                            int[] index = { 1 };
                            listBoxField.SelectedIndex = index;
                            break;
                    }
                }

                if (field is PdfComboBoxWidgetFieldWidget)
                {
                    PdfComboBoxWidgetFieldWidget comBoxField = field as PdfComboBoxWidgetFieldWidget;
                    switch (comBoxField.Name)
                    {
                        case "title":
                            int[] items = { 0 };
                            comBoxField.SelectedIndex = items;
                            break;
                    }
                }

                if (field is PdfRadioButtonListFieldWidget)
                {
                    PdfRadioButtonListFieldWidget radioBtnField = field as PdfRadioButtonListFieldWidget;
                    switch (radioBtnField.Name)
                    {
                        case "country":
                            radioBtnField.SelectedIndex = 1;
                            break;
                    }
                }

                if (field is PdfCheckBoxWidgetFieldWidget)
                {
                    PdfCheckBoxWidgetFieldWidget checkBoxField = field as PdfCheckBoxWidgetFieldWidget;
                    switch (checkBoxField.Name)
                    {
                        case "agreement_of_terms":
                            checkBoxField.Checked = true;
                            break;
                    }
                }
                if (field is PdfButtonWidgetFieldWidget)
                {
                    PdfButtonWidgetFieldWidget btnField = field as PdfButtonWidgetFieldWidget;
                    switch (btnField.Name)
                    {
                        case "submit":
                            btnField.Text = "Submit";
                            break;
                    }
                }
            }
            doc.SaveToFile(@"../../FormFieldData.pdf.pdf");
            System.Diagnostics.Process.Start(@"../../FormFieldData.pdf.pdf");