Foxit Quick PDF Library

Create PDF form fields programmatically

Creating PDF forms programmatically is easy with Foxit Quick PDF Library. In the sample code below we show you how to create text, checkbox and radio button form fields. A list of all form field functions available in our PDF SDK can be found in our form field function reference. PDF form fields need appearance streams in order to be displayed properly. Read more about appearance stream generation.

Text Form Fields

The sample code below demonstrates how to add text form fields to PDF files.

/* Create text form fields */

// Tell the library that all co-ordinates should
// begin from the top left corner of the page.

DPL.SetOrigin(1);

// Call the SetNeedAppearances function. 
// Required to display form fields in
// PDF viewer.

DPL.SetNeedAppearances(1);

// Add the first new form field

var iDf1 = DPL.NewFormField("First Name", 1);
DPL.SetFormFieldValue(iDf1, "Jane");
DPL.SetFormFieldBounds(iDf1, 20, 20, 100, 20);
DPL.SetFormFieldAlignment(iDf1, 2);

// Add the second new form field

var iDf2 = DPL.NewFormField("Second Name", 1);
DPL.SetFormFieldValue(iDf2, "Doe");
DPL.SetFormFieldBounds(iDf2, 20, 50, 100, 20);

fID = DPL.AddTrueTypeFont("Myriad Pro Cond", 1);
ffID = DPL.AddFormFont(fID);
DPL.SetFormFieldFont(iDf2, ffID);

// Add the third new form field

var iDf3 = DPL.NewFormField("Age", 1);
DPL.SetFormFieldValue(iDf3, "31");
DPL.SetFormFieldBounds(iDf3, 20, 80, 100, 20);

// Add the fourth new form field

var iDf3 = DPL.NewFormField("Nationality", 1);
DPL.SetFormFieldValue(iDf3, "Australian");
DPL.SetFormFieldBounds(iDf3, 20, 110, 100, 20);

// Save the file

DQPL.SaveToFile("text_form_fields.pdf");

Pushbutton Form Fields

The sample code below demonstrates how to add pushbutton form fields to PDF files.

// Tell the library that all co-ordinates should
// begin from the top left corner of the page.

DPL.SetOrigin(1);

// Add add the pushbutton form field and style it

DPL.SetNeedAppearances(1);
int iDf8 = DPL.NewFormField("PushMyButton", 2);
DPL.SetFormFieldBounds(iDf8, 20, 250, 100, 20);
DPL.SetFormFieldTextSize(iDf8, 12);
DPL.SetFormFieldBorderColor(iDf8, 1, 0.3, 0.1);
DPL.SetFormFieldBorderStyle(iDf8, 1, 0, 0, 0);
DPL.SetFormFieldCaption(iDf8, "Push Me!");
DPL.FormFieldWebLinkAction(iDf8, "U", "http://www.foxit.com/");

Checkbox Form Fields

The sample code below demonstrates how to add checkbox form fields to PDF files.

DPL.SetOrigin(1);

int csCross = 0;
int csCheck = 1;

DPL.SetOrigin(1);
DPL.SetTextSize(16);

DPL.SetNeedAppearances(1);
DPL.DrawText(50, 70, "Check Boxes");

int c1 = DPL.NewFormField("Check1", 3);
DPL.SetFormFieldBounds(c1, 50, 80, 20, 20);
DPL.SetFormFieldCheckStyle(c1, csCross, 1);
DPL.SetFormFieldValue(c1, "Off");

int c2 = DPL.NewFormField("Check2", 3);
DPL.SetFormFieldBounds(c2, 90, 80, 20, 20);
DPL.SetFormFieldCheckStyle(c2, csCross, 1);
DPL.SetFormFieldValue(c2, "Yes");

DPL.SaveToFile("checkbox_form_fields.pdf");

Radiobutton Form Fields

The sample code below demonstrates how to add radiobutton form fields to PDF files.

DPL.SetOrigin(1);

int csCross = 0;
int csCheck = 1;

DPL.SetOrigin(1);
DPL.SetTextSize(16);

DPL.SetNeedAppearances(1);

DPL.DrawText(50, 160, "RadioButtons");
int r0 = DPL.NewFormField("RadioParent", 4);

int r1 = DPL.AddFormFieldSub(r0, "Radio1");
DPL.SetFormFieldBounds(r1, 50, 180, 20, 20);
DPL.SetFormFieldCheckStyle(r1, csCheck, 1);

int r2 = DPL.AddFormFieldSub(r0, "Radio2");
DPL.SetFormFieldBounds(r2, 90, 180, 20, 20);
DPL.SetFormFieldCheckStyle(r2, csCheck, 1);

DPL.SetFormFieldValue(r0, "Radio1");

DPL.SaveToFile("radiobutton_form_fields.pdf");

Choice Form Fields

The sample code below demonstrates how to add choice form fields to PDF files. A choice form field can be either a drop-down combo box or a list box.

There are a variety of different ways that the choice form fields can be displayed. The function that determines how the choice form field is displayed is called SetFormFieldChoiceType. The code below includes lines of commented out code which demonstrate how to use the various different options.

// Required to generate form field appearance stream in Acrobat
DPL.SetNeedAppearances(1);

// Create the parent form field for the combobox or list box
int ParentFormFieldIndex1 = DPL.NewFormField("MyNewChoiceFormField", 5);

// There are four different types of choice form fields
// that can be specified using SetFormFieldChoiceType.
// Uncomment the code for the type that you want to use.

// 1. Set the form field to be a scrollable list box
//DPL.SetFormFieldChoiceType(ParentFormFieldIndex1, 1);

// 2. Set the form field to be a drop-down combo box
DPL.SetFormFieldChoiceType(ParentFormFieldIndex1, 2);

// 3. Set the form field to be a multiselect scrollable list box
// DPL.SetFormFieldChoiceType(ParentFormFieldIndex1, 3);

// 4. Set the form field to be a drop-down combo box with edit box
//DPL.SetFormFieldChoiceType(ParentFormFieldIndex1, 4); 

// Set location and dimensions of form field.
// If choice field type 2 or 4 we need a form 
// field that is smaller than if we are using
// choice field type 1 or 3 (drop-down vs list box)

// For choice field type 2 and 4
DPL.SetFormFieldBounds(ParentFormFieldIndex1, 20, 600, 100, 20);

// Uncomment for choice field type 1 and 3
// DPL.SetFormFieldBounds(ParentFormFieldIndex1, 20, 600, 100, 75);

// Follow function calls are not required
// but deal with the border style and color of
// the form field
DPL.SetFormFieldBorderStyle(ParentFormFieldIndex1, 1, 0, 0, 0);
DPL.SetFormFieldBackgroundColor(ParentFormFieldIndex1, 0.9, 0.9, 0.9);
DPL.SetFormFieldBorderColor(ParentFormFieldIndex1, 0.5, 0.5, 0.5);          

// 1st sub field. Add the export value and display name
int SubIndex1 = DPL.AddFormFieldChoiceSub(ParentFormFieldIndex1, "1", "One");

// 2nd sub field. Add the export value and display name
int SubIndex2 = DPL.AddFormFieldChoiceSub(ParentFormFieldIndex1, "2", "Two");

// 3rd sub field. Add the export value and display name
int SubIndex3 = DPL.AddFormFieldChoiceSub(ParentFormFieldIndex1, "3", "Three");

// 4th sub field. Add the export value and display name
int SubIndex4 = DPL.AddFormFieldChoiceSub(ParentFormFieldIndex1, "4", "Four");

// Save the file
DPL.SaveToFile("MyNewChoiceFormField.pdf");

Signature Form Fields

The sample code below demonstrates how to add signature form fields to PDF files.

// Tell the library that all co-ordinates should
// begin from the top left corner of the page.
DPL.SetOrigin(1);

// Call the SetNeedAppearances function. 
// Required to display form fields in
// PDF viewer.
DPL.SetNeedAppearances(1);

// Add new signature form field
int iDf1 = DPL.NewFormField("First Signature Field", 6);

// Set the location and size of the form field
DPL.SetFormFieldBounds(iDf1, 20, 20, 100, 20);

// Save the PDF to disk with the new field
DPL.SaveToFile("new_signature_field.pdf");

This article refers to a deprecated product. If you are looking for support for Foxit PDF SDK, please click here.

Updated on May 16, 2022

Was this article helpful?
Thanks for your feedback. If you have a comment on how to improve the article, you can write it here: