Home > Articles > .Net Framework Articles > Create Fillable PDF Forms Programmatically

Create Fillable PDF Forms Programmatically

by Tyler.D   on Jan 24, 2018   Category: .Net Framework  | Level: Intermediate  |  Views: 138    |  Points: 100   
Like this article? Bookmark and Share:
This article will show you how to create fillable PDF forms by using Free API with C#.

Introduction

The ability to create 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. This article describes a quick and simple approach to programmatically creating fillable PDF forms through the use of Free Spire.PDF DLL.

Background

Free Spire.PDF is a managed C# class library written to support the creation and manipulation of PDF documents. It offers a number of useful classes in Spire.Pdf.Fields namespace, allowing programmers to create various types of forms and manage form fields embedded in a PDF document. Here are some of them involved in this article.

ClassDescription
PdfTextBoxFieldRepresents text box field in the PDF form.
PdfCheckBoxFieldRepresents check box field in the PDF form.
PdfComboBoxFieldRepresents combo box field in the PDF Form.
PdfListBoxFieldRepresents list box field of the PDF form.
PdfListFieldItemRepresents an item of the list fields.
PdfRadioButtonListFieldRepresents radio button field in the PDF form.
PdfRadioButtonListItemRepresents an item of a radio button list.
PdfSignatureFieldRepresents signature field in the PDF Form.
PdfButtonFieldRepresents button field in the PDF form.

To create a PDF form, initialize an instance of the corresponding class with two parameters being passed. The first argument implies the PDF page where the field to be placed, the second one indicates the field name. For instance:

PdfTextBoxField textbox = new PdfTextBoxField(page, "TextBox");

To position and resize the form, set the Bounds property of the field object. You can also format a form through properties like ForeColor, BackColor, BorderWidth, BorderStye, etc. Except the properties that all forms have in common, there are exclusive properties for a particular type of form, such as Multiline property of a textbox, Checked property of a checkbox and Item property of a list box. Try to set them based on your own situations.

After these settings are done, add the specified form field to PDF page using this line of code.

pdf.Form.Fields.Add(textbox);



Using the code


//create a Pdf document

PdfDocument pdf = new PdfDocument();

PdfPageBase page = pdf.Pages.Add();

PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 12f);

PdfBrush brush = PdfBrushes.Purple;

float x1 = 0;

float x2 = 100;

float y = 0;

float tempY = 0;

//add a textbox

string text = "TextBox:";

page.Canvas.DrawString(text, font, brush, x1, y);

tempY = font.MeasureString(text).Height + 15;     

PdfTextBoxField textbox = new PdfTextBoxField(page, "TextBox");

textbox.Text = "Hello World";

textbox.Bounds = new RectangleF(x2, y, 100, 15);

textbox.BorderWidth = 0.75f;

textbox.BorderStyle = PdfBorderStyle.Solid;

pdf.Form.Fields.Add(textbox);

//add a password textbox where the input shows up as stars

text = "Password:";

y += tempY;

page.Canvas.DrawString(text, font, brush, x1, y);

tempY = font.MeasureString(text).Height + 15;

PdfTextBoxField passwordBox = new PdfTextBoxField(page, "Password");

passwordBox.Password = true;

passwordBox.Text = "123456";

passwordBox.Bounds = new RectangleF(x2, y, 100, 15);

passwordBox.BorderWidth = 0.75f;

passwordBox.BorderStyle = PdfBorderStyle.Solid;

pdf.Form.Fields.Add(passwordBox);

//add two checkboxes

text = "CheckBox:";

y += tempY;

page.Canvas.DrawString(text, font, brush, x1, y);

tempY = font.MeasureString(text).Height + 15;

PdfCheckBoxField checkbox1 = new PdfCheckBoxField(page, "CheckBox1");

checkbox1.Bounds = new RectangleF(x2, y, 15, 15);

checkbox1.BorderWidth = 0.75f;

checkbox1.BorderStyle = PdfBorderStyle.Solid;

checkbox1.Style = PdfCheckBoxStyle.Check;

pdf.Form.Fields.Add(checkbox1);

page.Canvas.DrawString("Option 1", font, brush, x2 + 20, y);

PdfCheckBoxField checkbox2 = new PdfCheckBoxField(page, "CheckBox2");

checkbox2.Bounds = new RectangleF(x2 + 90, y, 15, 15);

checkbox2.BorderWidth = 0.75f;

checkbox2.BorderStyle = PdfBorderStyle.Solid;

checkbox2.Style = PdfCheckBoxStyle.Check;

pdf.Form.Fields.Add(checkbox2);

page.Canvas.DrawString("Option 2", font, brush, x2 + 110, y);

//add a listbox

text = "ListBox:";

y += tempY;

page.Canvas.DrawString(text, font, brush, x1, y);

PdfListBoxField listbox = new PdfListBoxField(page, "ListBox");      

listbox.Bounds = new RectangleF(x2, y, 60, 50);

listbox.BorderWidth = 0.75f;

for (int i = 0; i < 3; i++)

{

    string tempText = string.Format("Text {0}", i);

    string tempValue = string.Format("Value {0}", i);

    listbox.Items.Add(new PdfListFieldItem(tempText, tempValue));

}

pdf.Form.Fields.Add(listbox);

//add a radiobutton

text = "RadioButton:";

y += 50 + 15;

page.Canvas.DrawString(text, font, brush, x1, y);

tempY = font.MeasureString(text).Height + 15;

PdfRadioButtonListField radiobutton = new PdfRadioButtonListField(page, "RadioButton");   

PdfRadioButtonListItem item1 = new PdfRadioButtonListItem("Item 1");

item1.BorderWidth = 0.75f;

item1.Bounds = new RectangleF(x2, y, 15, 15);

radiobutton.Items.Add(item1);

page.Canvas.DrawString("Item 1", font, brush, x2 + 20, y);

PdfRadioButtonListItem item2 = new PdfRadioButtonListItem("Item 2");

item2.BorderWidth = 0.75f;

item2.Bounds = new RectangleF(x2 + 80, y, 15, 15);

radiobutton.Items.Add(item2);

page.Canvas.DrawString("Item 2", font, brush, x2 + 100, y);

radiobutton.SelectedIndex = 1;

pdf.Form.Fields.Add(radiobutton);

//add a combobox

text = "ComboBox:";

y += tempY;

page.Canvas.DrawString(text, font, brush, x1, y);

tempY = font.MeasureString(text).Height + 15;

PdfComboBoxField combobox = new PdfComboBoxField(page, "ComboBox");

combobox.Bounds = new RectangleF(x2, y, 60, 15);

combobox.BorderWidth = 0.75f;

for (int i = 0; i < 3; i++)

{

    string tempText = string.Format("Text {0}", i);

    string tempValue = string.Format("Value {0}", i);

    combobox.Items.Add(new PdfListFieldItem(tempText, tempValue));

}

pdf.Form.Fields.Add(combobox);

//add a signature field

text = "Signature:";

y += tempY;

page.Canvas.DrawString(text, font, brush, x1, y);

PdfSignatureField signaturefield = new PdfSignatureField(page, "Signature");

signaturefield.Bounds = new RectangleF(x2, y, 150, 80);

signaturefield.BorderWidth = 0.75f;

signaturefield.HighlightMode = PdfHighlightMode.Outline;

pdf.Form.Fields.Add(signaturefield);

//add a button

text = "Button:";

y += 80 + 15;

page.Canvas.DrawString(text, font, brush, x1, y);

tempY = font.MeasureString(text).Height + 15;

PdfButtonField button = new PdfButtonField(page, "Submit");

button.Text = "Submit";

button.Bounds = new RectangleF(x2, y, 60, 16);

button.BorderWidth = 0.75f;

PdfSubmitAction submitAction = new PdfSubmitAction("http://www.google.com");

button.Actions.MouseUp = submitAction;

pdf.Form.Fields.Add(button);

//save to file

pdf.SaveToFile("Forms.pdf");

pdf.Close();


Figure 1 shows the fillable PDF forms generated programmatically.

                                       Figure 1. PDF Containing fillable forms


Conclusion

The code to create forms is quite simply and logically clear, right? Anyway, if you get any questions feel free to comment below. 




Like this article? Bookmark and Share:

Most viewed Articles

User Comments


No response found, be the first to review this article.

Submit feedback about this article

Please sign in to post feedback

Latest Posts