C# tutorial: PDF form fields


Add form fields to a PDF document

In the previous page, you learnt to add a print button to an existing PDF document to make the end user easy to print the PDF document from any page. The button is a form field and is created by using the PushButtonField class.

Adobe supports two different ways to view and fill out PDF forms. One is based on the AcroForms technology. A PDF document can contain one form that consists of different types of fields. The type of a field is defined by the /FT value in the field dictionary. AcroForms support four types of fields:

- /Btn—Button fields
- /Tx—Text fields
- /Ch—Choice fields
- /Sig—Signature field

Radio Button Field

Typically, a radio button field is represented by a group of buttons in which you can select only one button at a time. A radio button group can be created by using the CreateRadioButton method of the PdfFormField class. A radio button is created by using the RadioCheckField class. Then you will get an instance of PdfFormField from the RadioCheckField object and add it to the group.

There are many different type of a radio button field. You can specify the type of radio button field by using its code as shown in the table below.

Code Description
TYPE_CHECK A square with a check mark (when selected)
TYPE_CIRCLE A circle with a bullet (when selected)
TYPE_CROSS A square with an X (when selected)
TYPE_DIAMOND A square with a diamond (when selected)
TYPE_SQUARE A square with a filled sqare (when selected)
TYPE_STAR A square with a five-pointed star (when selected)

 

//create a document object
var doc = new Document(PageSize.A4);
//get the current directory
string path = Environment.CurrentDirectory;
//create PdfWriter object
PdfWriter writer=PdfWriter.GetInstance(doc, new FileStream(path + "/pdfdoc.pdf", FileMode.Create));
//define array sex to store Female and Male
String[] sex= { "Female","Male"};
//open the document for writing
doc.Open();
//Get direct content
PdfContentByte canvas = writer.DirectContent;
//create a Font object
Font font = new Font(Font.FontFamily.HELVETICA, 18);
//create RadioButton group object
PdfFormField radiogroup = PdfFormField.CreateRadioButton(writer, true);
radiogroup.FieldName="gsex";
//variables
Rectangle rect;
RadioCheckField radio;
for(int i=0;i<sex.Length;i++)
{
rect = new Rectangle( 40, 806 - i * 40, 60, 788 - i * 40);
//create RadioCheckField object
radio = new RadioCheckField(writer, rect, "sex" + i, sex[i]);
//set properties
radio.BorderColor=GrayColor.GRAYBLACK;
radio.BackgroundColor=GrayColor.GRAYWHITE;
radio.CheckType=RadioCheckField.TYPE_CIRCLE;
radiogroup.AddKid(radio.RadioField);
//show labels
ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT,new Phrase(sex[i], font), 70, 790 - i * 40, 0);

}
//add RadioButton group to the writer
writer.AddAnnotation(radiogroup);
//close document
doc.Close();
//view the result pdf file
System.Diagnostics.Process.Start(path + "/pdfdoc.pdf");

pdf radio button form field

Check Boxes

You can use the RadioCheckField class to create check boxes. Normally, you can select more than one check box. Unlike radio buttons, Check boxes are not placed in a group.

//create a document object
var doc = new Document(PageSize.A4);
//get the current directory
string path = Environment.CurrentDirectory;
//create PdfWriter object
PdfWriter writer=PdfWriter.GetInstance(doc, new FileStream(path + "/pdfdoc.pdf", FileMode.Create));
//define array sex to store languages
String[] languages= { "English","French","Khmer","Chinese","Japanese"};
//open the document for writing
doc.Open();
//Get direct content
PdfContentByte canvas = writer.DirectContent;
//create a Font object
Font font = new Font(Font.FontFamily.HELVETICA, 18);

//variables
Rectangle rect;
RadioCheckField radio;

for (int i = 0; i < languages.Length; i++)
{
rect = new Rectangle( 40, 806 - i * 40, 60, 788 - i * 40);
radio = new RadioCheckField(writer, rect,languages[i],"Yes");
radio.CheckType = RadioCheckField.TYPE_CROSS;
radio.Checked =false;
writer.AddAnnotation(radio.CheckField);
//show labels
ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(languages[i], font), 70, 790 - i * 40, 0);

}
//close document
doc.Close();
//view the result pdf file
System.Diagnostics.Process.Start(path + "/pdfdoc.pdf");

pdf check box form fields

Text Field

Text fields allow you to enter data in a PDF document. You can create a text field by using the TextField class. In the example code below, two text fields are added to the PDF document. One text field is for entering your name and another text field allows you to enter your e-mail address.

var doc = new Document(PageSize.A4);
//get the current directory
string path = Environment.CurrentDirectory;
//create PdfWriter object
PdfWriter writer=PdfWriter.GetInstance(doc, new FileStream(path + "/pdfdoc.pdf", FileMode.Create));
//open the document for writing
doc.Open();
TextField textname = new TextField(writer,new Rectangle(36,800,136,780),"txtname");
textname.Text = "Enter your name...";
textname.TextColor = new BaseColor(255, 0, 0);
textname.BackgroundColor = BaseColor.LIGHT_GRAY;
TextField textmail = new TextField(writer,new Rectangle(36,770,136,750),"txtmail");
textmail.Text = "Enter your e-mail...";
textmail.TextColor = new BaseColor(255, 0, 0);
textmail.BackgroundColor = BaseColor.LIGHT_GRAY;
writer.AddAnnotation(textname.GetTextField());
writer.AddAnnotation(textmail.GetTextField());
doc.Close();
//view the result pdf file
System.Diagnostics.Process.Start(path + "/pdfdoc.pdf");

pdf text field


Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.