C#/VB.NET: Create, Fill, or Remove Fillable Forms in PDF

The ability to create fillable PDF forms that end users can fill out (without having to print) can be a real benefit to making your business efficient. With these documents, users can download them, fill them out, save them, and send them back to you. No more printing and scanning. In this article, you will learn how to create, fill, or remove fillable forms in PDF in C# and VB.NET by using Spire.PDF for .NET.

Spire.PDF for .NET offers a series of useful classes under the Spire.Pdf.Fields namespace, allowing programmers to create and edit various types of form fields including text box, check box, combo box, list box, and radio button. The table below lists some of the core classes involved in this tutorial.

Class Description
PdfForm Represents interactive form of the PDF document.
PdfField Represents field of the PDF document's interactive form.
PdfTextBoxField Represents text box field in the PDF form.
PdfCheckBoxField Represents check box field in the PDF form.
PdfComboBoxField Represents combo box field in the PDF Form.
PdfListBoxField Represents list box field of the PDF form.
PdfListFieldItem Represents an item of a list field.
PdfRadioButtonListField Represents radio button field in the PDF form.
PdfRadioButtonListItem Represents an item of a radio button list.
PdfButtonField Represents button field in the PDF form.
PdfSignatureField Represents signature field in the PDF form.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Create Fillable Form Fields in a PDF Document

To create a PDF form, initialize an instance of the corresponding field class. Set its size and position in the document through the Bounds property, and then add it to PDF using PdfFormFieldCollection.Add() method. The following are the main steps to create various types of form fields in a PDF document using Spire.PDF for .NET.

  • Create a PdfDocument object.
  • Add a page using PdfDocuemnt.Pages.Add() method.
  • Create a PdfTextBoxField object, set the properties of the field including Bounds, Font and Text, and then add it to the document using PdfFormFieldCollection.Add() method.
  • Repeat the step 3 to add check box, combo box, list box, radio button, signature field and button to the document.
  • Save the document to a PDF file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Fields;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace CreateFillableFormsInPdf
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Add a page
            PdfPageBase page = doc.Pages.Add();

            //Initialize x and y coordinates
            float baseX = 100;
            float baseY = 30;

            //Create two brush objects
            PdfSolidBrush brush1 = new PdfSolidBrush(new PdfRGBColor(Color.Blue));
            PdfSolidBrush brush2 = new PdfSolidBrush(new PdfRGBColor(Color.Black));

            //Create a font 
            PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 12f, PdfFontStyle.Regular);

            //Add a textbox 
            page.Canvas.DrawString("TextBox:", font, brush1, new PointF(10, baseY));
            RectangleF tbxBounds = new RectangleF(baseX, baseY, 150, 15);
            PdfTextBoxField textBox = new PdfTextBoxField(page, "textbox");
            textBox.Bounds = tbxBounds;
            textBox.Text = "Hello World";
            textBox.Font = font;
            doc.Form.Fields.Add(textBox);
            baseY += 25;

            //add two checkboxes 
            page.Canvas.DrawString("CheckBox:", font, brush1, new PointF(10, baseY));
            RectangleF checkboxBound1 = new RectangleF(baseX, baseY, 15, 15);
            PdfCheckBoxField checkBoxField1 = new PdfCheckBoxField(page, "checkbox1");
            checkBoxField1.Bounds = checkboxBound1;
            checkBoxField1.Checked = false;
            page.Canvas.DrawString("Option 1", font, brush2, new PointF(baseX + 20, baseY));

            RectangleF checkboxBound2 = new RectangleF(baseX + 70, baseY, 15, 15);
            PdfCheckBoxField checkBoxField2 = new PdfCheckBoxField(page, "checkbox2");
            checkBoxField2.Bounds = checkboxBound2;
            checkBoxField2.Checked = false;
            page.Canvas.DrawString("Option 2", font, brush2, new PointF(baseX + 90, baseY));
            doc.Form.Fields.Add(checkBoxField1);
            doc.Form.Fields.Add(checkBoxField2);
            baseY += 25;

            //Add a listbox
            page.Canvas.DrawString("ListBox:", font, brush1, new PointF(10, baseY));
            RectangleF listboxBound = new RectangleF(baseX, baseY, 150, 50);
            PdfListBoxField listBoxField = new PdfListBoxField(page, "listbox");
            listBoxField.Items.Add(new PdfListFieldItem("Item 1", "item1"));
            listBoxField.Items.Add(new PdfListFieldItem("Item 2", "item2"));
            listBoxField.Items.Add(new PdfListFieldItem("Item 3", "item3")); ;
            listBoxField.Bounds = listboxBound;
            listBoxField.Font = font;
            listBoxField.SelectedIndex = 0;
            doc.Form.Fields.Add(listBoxField);
            baseY += 60;

            //Add two radio buttons
            page.Canvas.DrawString("RadioButton:", font, brush1, new PointF(10, baseY));
            PdfRadioButtonListField radioButtonListField = new PdfRadioButtonListField(page, "radio");
            PdfRadioButtonListItem radioItem1 = new PdfRadioButtonListItem("option1");
            RectangleF radioBound1 = new RectangleF(baseX, baseY, 15, 15);
            radioItem1.Bounds = radioBound1;
            page.Canvas.DrawString("Option 1", font, brush2, new PointF(baseX + 20, baseY));

            PdfRadioButtonListItem radioItem2 = new PdfRadioButtonListItem("option2");
            RectangleF radioBound2 = new RectangleF(baseX + 70, baseY, 15, 15);
            radioItem2.Bounds = radioBound2;
            page.Canvas.DrawString("Option 2", font, brush2, new PointF(baseX + 90, baseY));
            radioButtonListField.Items.Add(radioItem1);
            radioButtonListField.Items.Add(radioItem2);
            radioButtonListField.SelectedIndex = 0;
            doc.Form.Fields.Add(radioButtonListField);
            baseY += 25;

            //Add a combobox
            page.Canvas.DrawString("ComboBox:", font, brush1, new PointF(10, baseY));
            RectangleF cmbBounds = new RectangleF(baseX, baseY, 150, 15);
            PdfComboBoxField comboBoxField = new PdfComboBoxField(page, "combobox");
            comboBoxField.Bounds = cmbBounds;
            comboBoxField.Items.Add(new PdfListFieldItem("Item 1", "item1"));
            comboBoxField.Items.Add(new PdfListFieldItem("Item 2", "itme2"));
            comboBoxField.Items.Add(new PdfListFieldItem("Item 3", "item3"));
            comboBoxField.Items.Add(new PdfListFieldItem("Item 4", "item4"));
            comboBoxField.SelectedIndex = 0;
            comboBoxField.Font = font;
            doc.Form.Fields.Add(comboBoxField);
            baseY += 25;

            //Add a signature field
            page.Canvas.DrawString("Signature Field:", font, brush1, new PointF(10, baseY));
            PdfSignatureField sgnField = new PdfSignatureField(page, "sgnField");
            RectangleF sgnBounds = new RectangleF(baseX, baseY, 150, 80);
            sgnField.Bounds = sgnBounds;
            doc.Form.Fields.Add(sgnField);
            baseY += 90;

            //Add a button
            page.Canvas.DrawString("Button:", font, brush1, new PointF(10, baseY));
            RectangleF btnBounds = new RectangleF(baseX, baseY, 50, 15);
            PdfButtonField buttonField = new PdfButtonField(page, "button");
            buttonField.Bounds = btnBounds;
            buttonField.Text = "Submit";
            buttonField.Font = font;
            PdfSubmitAction submitAction = new PdfSubmitAction("https://www.e-iceblue.com/getformvalues.php");
            submitAction.DataFormat = SubmitDataFormat.Html;
            buttonField.Actions.MouseDown = submitAction;
            doc.Form.Fields.Add(buttonField);

            //Save to file
            doc.SaveToFile("FillableForms.pdf", FileFormat.PDF);
        }
    }
}

C#/VB.NET: Create, Fill, or Remove Fillable Forms in PDF

Fill Form Fields in an Existing PDF Document

In order to fill out a form, we must first get all the form fields from the PDF document, determine the type of a certain field, and then input a value or select a value from a predefined list. The following are the steps to fill form fields in an existing PDF document using Spire.PDF for .NET.

  • Create a PdfDocument object.
  • Load a sample PDF document using PdfDocument.LoadFromFile() method.
  • Get the form from the document through PdfDocument.Form property.
  • Get the form widget collection through PdfFormWidget.FieldsWidget property.
  • Loop through the form widget collection to get a specific PdfField.
  • Determine if the PdfField is a certain field type such as text box. If yes, set the text of the text box through PdfTextBoxFieldWidget.Text property.
  • Repeat the sixth step to fill radio button, check box, combo box, and list box with values.
  • Save the document to a PDF file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Fields;
using Spire.Pdf.Widget;

namespace FillFormFields
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Load a template containing forms
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\FormsTemplate.pdf");

            //Get the form from the document
            PdfFormWidget form = (PdfFormWidget)doc.Form;

            //Get the form widget collection
            PdfFormFieldWidgetCollection formWidgetCollection = form.FieldsWidget;

            //Loop through the widgets
            for (int i = 0; i < formWidgetCollection.Count; i++)
            {
                //Get a specific field
                PdfField field = formWidgetCollection[i];

                //Determine if the field is a text box
                if (field is PdfTextBoxFieldWidget)
                {
                    if (field.Name == "name")
                    {
                        //Set the text of text box
                        PdfTextBoxFieldWidget textBoxField = (PdfTextBoxFieldWidget)field;
                        textBoxField.Text = "Kaila Smith";
                    }
                }

                //Determine if the field is a radio button
                if (field is PdfRadioButtonListFieldWidget)
                {
                    if (field.Name == "gender")
                    {
                        //Set the selected index of radio button
                        PdfRadioButtonListFieldWidget radioButtonListField = (PdfRadioButtonListFieldWidget)field;
                        radioButtonListField.SelectedIndex = 1;
                    }
                }

                //Determine if the field is a combo box
                if (field is PdfComboBoxWidgetFieldWidget)
                {
                    if (field.Name == "country")
                    {
                        //Set the selected index of combo box
                        PdfComboBoxWidgetFieldWidget comboBoxField = (PdfComboBoxWidgetFieldWidget)field;
                        int[] index = { 0 };
                        comboBoxField.SelectedIndex = index;
                    }
                }

                //Determine if the field is a check box
                if (field is PdfCheckBoxWidgetFieldWidget)
                {
                    //Set the "Checked" status of check box
                    PdfCheckBoxWidgetFieldWidget checkBoxField = (PdfCheckBoxWidgetFieldWidget)field;
                    switch (checkBoxField.Name)
                    {
                        case "travel":
                            checkBoxField.Checked = true;
                            break;
                        case "movie":
                            checkBoxField.Checked = true;
                            break;
                    }
                }

                //Determine if the field is a list box
                if (field is PdfListBoxWidgetFieldWidget)
                {
                    if (field.Name == "degree")
                    {
                        //Set the selected index of list box
                        PdfListBoxWidgetFieldWidget listBox = (PdfListBoxWidgetFieldWidget)field;
                        int[] index = { 1 };
                        listBox.SelectedIndex = index;
                    }
                }
            }

            //Save to file
            doc.SaveToFile("FillFormFields.pdf", FileFormat.PDF);
        }
    }
}

C#/VB.NET: Create, Fill, or Remove Fillable Forms in PDF

Remove a Particular Field or All Fields from an Existing PDF Document

A form field in a PDF document can be accessed by its index or name and removed by PdfFieldCollection.Remove() method. The following are the steps to remove a particular field or all fields from an existing PDF document using Sprie.PDF for .NET.

  • Create a PdfDocument object.
  • Load a PDF document using PdfDocument.LoadFromFile() method.
  • Get the form widget collection from the document.
  • Loop through the widget collection to get a specific PdfField. Remove the field one by one using PdfFieldCollection.Remove() method.
  • To remove a certain form field, get it from the document through PdfFormWidget.FieldsWidget["fieldName"] property and then call the PdfFieldCollectiont.Remove() method.
  • Save the document to a PDF file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Fields;
using Spire.Pdf.Widget;

namespace RemoveFormFields
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Load a PDF file
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\FormsTemplate.pdf");

            //Get the form fields from the document
            PdfFormWidget formWidget = doc.Form as PdfFormWidget;

            //Loop through the widgets
            for (int i = formWidget.FieldsWidget.List.Count - 1; i >= 0; i--)
            {
                //Get a specific field
                PdfField field = formWidget.FieldsWidget.List[i] as PdfField;

                //Remove the field
                formWidget.FieldsWidget.Remove(field);
            }

            //Get a specific field by its name
            //PdfField field = formWidget.FieldsWidget["name"];
            //Remove the field
            //formWidget.FieldsWidget.Remove(field);

            //Save to file
            doc.SaveToFile("DeleteAllFields.pdf");
        }
    }
}

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.