PDFOne .NET
Powerful all-in-one PDF library for .NET
Compatibility
VS 2005/2008/2010/2012/2013

How to Create and Fill PDF Forms in .NET

Learn how to create, edit, fill, and save PDF form fields using PDFOne .NET.
By Raju Sinha

PDF or AcroForms can be downloaded from a remote computer and filled offline. The filled form can later be submitted online or printed on paper. This convenience has lead to a great demand for desktop and web applications that generate PDF forms.

Creating PDF forms using PDFOne .NET is very easy. In this article, you will see how to create form fields, edit their properties, fill them with values, and save them to a document.

A PDF form typically has push buttons, text boxes, list boxes, radio buttons and check boxes.

Let us start with the simplest and perhaps most important of them all - buttons. A push button is represented by the class PDFFormPushButton. (Push buttons and other types of form fields are derived from the parent class PDFFormField).

Example 1: Creating A Form With A JavaScript Action Button

In the code snippet below, a new push button form field is created. Immediately after this, a "field name" is assigned to it. (This will be the name that the form field will be known internally in the PDF document.) Next, the position on the page where the form field will appear on a page is specified. A caption for the button is also specified.

// Create a new blank PDF document
PDFDocument doc = new PDFDocument();
doc.MeasurementUnit = PDFMeasurementUnit.Points;

// Create a push button form field
PDFFormPushButton fpb1 = new PDFFormPushButton();
fpb1.FieldName = "fPushButton1";
fpb1.Rectangle = new RectangleF(100, 100, 100, 30);
fpb1.NormalCaption = "Push this button";
fpb1.ActionType =
  PDFFormFieldActionType.Javascript_Action;
fpb1.JavaScript = "app.alert('You pushed me!')";

// Add push button to the document
doc.AddFormField(fpb1);

doc.OpenAfterCreate = true;
// Save document to file
doc.Save("forms_doc1.pdf");
doc.Close();

A "JavaScript action" is assigned to the button. (Form fields can have several types of "actions" associated with them. These actions can be executed by a viewer application, such as AdobeĀ® Reader, when the user interacts with the document.)

In the above code snippet, the button is set to execute a JavaScript script when selected by the user. You will typically use JavaScript actions for validating entered values or updating calculated fields. Other type of actions that can be assigned to a push button would be to submit the form contents to a website URL or to reset the entered values in the form fields.

Example 2: Creating A Form With A Submit Action Button

We will now see an example of how to create a form that will submit information to a website. First, a text form field is created using PDFFormTextField class. Then, a list box is created using a PDFFormListBox object. Options in the list box are added using PDFFormListBox.AddItem(String str) method. Finally, a submit button is added by creating a PDFFormPushButton object, specifying a submit action for the button, and setting the website URL where the form contents need to be submitted.

// Create a blank PDF document
PDFDocument doc = new PDFDocument();

// Create a blank page
PDFPage page1 = new PDFPage();

page1.MeasurementUnit = PDFMeasurementUnit.Points;

// Create a text form field
PDFFormTextField ftf1 = new PDFFormTextField();
ftf1.FieldName = "ftf_name";
ftf1.NameAsUnicode = false;
ftf1.Rectangle = new RectangleF(100, 25, 100, 20);
ftf1.FieldValue = "Enter your name here";
ftf1.BorderWidth = 1;

// Create a list box form field
PDFFormListBox flb1 = new PDFFormListBox();
flb1.FieldName = "flb_country";
flb1.NameAsUnicode = false;
flb1.Rectangle = new RectangleF(100, 55, 100, 45);
flb1.BorderWidth = 1;
// Add options to the list box
flb1.AddItem("India");
flb1.AddItem("United States");
flb1.AddItem("United Kingdom");
// Specify option that is selected by default
flb1.DefaultSelectedIndex = 1;

PDFFormPushButton fpb1 = new PDFFormPushButton();
fpb1.FieldName = "flb_submit";
fpb1.NameAsUnicode = false;
fpb1.Rectangle = new RectangleF(100, 125, 100, 20);
fpb1.BorderWidth = 1;            
fpb1.NormalCaption = "Submit";
// Specify action for the button
fpb1.ActionType = PDFFormFieldActionType.Submit_Action;
// Specify website URL where form contents
// should be submitted
fpb1.SubmitUrl =
  "http://www.gnostice.com/newsletters/demos/" 
  +  "200802/pdfone_net/forms_submit_test.asp";

// Add form field to the page
page1.AddFormField(ftf1);
page1.AddFormField(flb1);
page1.AddFormField(fpb1);
    
// Write text identifying the form fields
page1.WriteText("Name: ", 10, 30);
page1.WriteText("Country: ", 10, 55);

// Add the page to document
doc.AddPage(page1);

doc.OpenAfterCreate = true;

// Write to file
doc.Save("forms_doc2.pdf");

doc.Close();

Example 3: Modify A List Box Form Field

To modify a form field, you need to first load the forms document using PDFDocument.Load(string filePath) method. After loading the document, you can retrieve all form field of a specified type using PDFDocument.GetAllFormFields(int pageNo, PDFFormFieldType type) method in array list. You can then cast individual list members and then gain access to their methods and properties. After you have made changes to the form fields, call the PDFDocument.Save(string FileName) to save the forms document with the modified form fields.

In the code snippet below, the forms document generated in Example 2 is loaded and an extra option is added to the list box form field.

// Load existing forms document
PDFDocument doc = new PDFDocument();
doc.ApplyFormAppearances = true;
doc.Load("forms_doc2.pdf");

// Retrieve all list box form fields
System.Collections.ArrayList formList =
    doc.GetAllFormFields(1, PDFFormFieldType.ListBox);

// Cast the first list member
PDFFormListBox flb1 = (PDFFormListBox) formList[0];

// Add a new option
flb1.AddItem("Other");
// Increase the height of the list box
flb1.Rectangle = new RectangleF(100, 55, 100, 50);

// Save the modified forms document
doc.Save("forms_doc3.pdf");
doc.Close();

Once you can gain access to individual form fields, filling the form fields with your own values is easy. For a text field, you will need to set the PDFFormTextField.FieldValue property. For a list box, you need to use PDFFormListBox.DefaultSelectedIndex and/or SelectedItemIndex property.

---o0O0o---

Our .NET Developer Tools
Gnostice Document Studio .NET

Multi-format document-processing component suite for .NET developers.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
Gnostice Document Studio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
Gnostice Document Studio Java

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2024 Gnostice Information Technologies Private Limited. All rights reserved.