Create a PDF form with JavaScript from scratch

  • Reference KB000004
  • Type Code sample
  • Product -
  • Category PDF forms
  • Created 10/24/2009

In this article we will create a PDF form from scratch entirely in code. The form has three text fields: A, B and SUM and a single button: ADD. When the user opens the created PDF in a PDF reader and fills fields A and B and hits the ADD button, JavaScript will be invoked that fills field SUM with the sum of A and B.

The following code creates the form:


// create a new document with 0 pages
Document document = new Document();
 
// add a new empty page to the document
Page page = new Page(PageSize.A6, true);
document.Pages.Add(page);
 
// add 3 text fields and a button 
// helper methods are discussed later
addTextField(document, page, "A", 20);
addTextField(document, page, "B", 80);
addButton(document, page, "ADD", 140);
addTextField(document, page, "SUM", 200);
 
// add JavaScript to the push button
// helper method is discussed later
PushButtonField addField = 
  document.Fields["ADD"] as PushButtonField;
addJavaScript( addField );
 
// save the new form
using (FileStream fileOut = new FileStream(
  @"..\..\newform.pdf", FileMode.Create, FileAccess.Write))
{
  document.Write(fileOut);
}
 1 // create a new document with 0 pages
 2 Document document = new Document();
 3  
 4 // add a new empty page to the document
 5 Page page = new Page(PageSize.A6, true);
 6 document.Pages.Add(page);
 7  
 8 // add 3 text fields and a button 
 9 // helper methods are discussed later
10 addTextField(document, page, "A", 20);
11 addTextField(document, page, "B", 80);
12 addButton(document, page, "ADD", 140);
13 addTextField(document, page, "SUM", 200);
14  
15 // add JavaScript to the push button
16 // helper method is discussed later
17 PushButtonField addField = 
18   document.Fields["ADD"] as PushButtonField;
19 addJavaScript( addField );
20  
21 // save the new form
22 using (FileStream fileOut = new FileStream(
23   @"..\..\newform.pdf", FileMode.Create, FileAccess.Write))
24 {
25   document.Write(fileOut);
26 }

The code above uses three helper methods. Let's discuss them in turn.

The helper method addTextField adds a new text field. As you can see from the code, it first adds a new TextField instance to the Document.Fields collection, next it adds a new Widget instance to the Page.Widgets collection. By adding the Widget to the Field.Widgets collection, the Field and the Widget are connected. All other code has to do with positioning and styling the field appearance. Here is the code:


static void addTextField(
  Document document, Page page, string name, int left)
{
  // create a new text field and add it the document
  TextField field = new TextField(name);
  document.Fields.Add(field);

  // create a widget for the field and add it to the page
  Widget widget = new Widget();
  field.Widgets.Add(widget);
  page.Widgets.Add(widget);
 
  // position the widget
  widget.Left = left;
  widget.Bottom = 200;
  widget.Width = 50;
  widget.Height = 20;
 
  // style the widget
  widget.Font = Font.Helvetica;
  widget.FontSize = 0; // autosize
  widget.BorderWidth = 2;
  widget.BorderStyle = 
    TallComponents.PDF.Annotations.BorderStyle.Solid;
  widget.BorderColor = RgbColor.Black;
}
 1 static void addTextField(
 2   Document document, Page page, string name, int left)
 3 {
 4   // create a new text field and add it the document
 5   TextField field = new TextField(name);
 6   document.Fields.Add(field);
 7 
 8   // create a widget for the field and add it to the page
 9   Widget widget = new Widget();
10   field.Widgets.Add(widget);
11   page.Widgets.Add(widget);
12  
13   // position the widget
14   widget.Left = left;
15   widget.Bottom = 200;
16   widget.Width = 50;
17   widget.Height = 20;
18  
19   // style the widget
20   widget.Font = Font.Helvetica;
21   widget.FontSize = 0; // autosize
22   widget.BorderWidth = 2;
23   widget.BorderStyle = 
24     TallComponents.PDF.Annotations.BorderStyle.Solid;
25   widget.BorderColor = RgbColor.Black;
26 }

The helper method addButton is not much different from addTextField. It first adds a new PushButtonField instance to the Document.Fields collection, next it adds a new PushButtonWidget instance to the Page.Widgets collection. Next, the widget is connected to the field by adding the widget to the Field.Widgets collection. All other code has to do with positioning and styling the field appearance. Here is the code:


static void addButton(
  Document document, Page page, string name, int left)
{
  // create a push button field and add it the document
  PushButtonField field = new PushButtonField(name);
  document.Fields.Add(field);

  // create a new widget for the field and add it to the page
  PushButtonWidget widget = new PushButtonWidget();
  field.Widgets.Add(widget);
  page.Widgets.Add(widget);
 
  // position the widget
  widget.Left = left;
  widget.Bottom = 200;
  widget.Width = 50;
  widget.Height = 20;
 
  // style the widget
  widget.Label = "ADD"; // button text
  widget.Font = Font.Helvetica;
  widget.FontSize = 0; // autosize
  widget.BorderStyle = 
    TallComponents.PDF.Annotations.BorderStyle.Beveled;
  widget.BorderColor = RgbColor.Black;
}
 1 static void addButton(
 2   Document document, Page page, string name, int left)
 3 {
 4   // create a push button field and add it the document
 5   PushButtonField field = new PushButtonField(name);
 6   document.Fields.Add(field);
 7 
 8   // create a new widget for the field and add it to the page
 9   PushButtonWidget widget = new PushButtonWidget();
10   field.Widgets.Add(widget);
11   page.Widgets.Add(widget);
12  
13   // position the widget
14   widget.Left = left;
15   widget.Bottom = 200;
16   widget.Width = 50;
17   widget.Height = 20;
18  
19   // style the widget
20   widget.Label = "ADD"; // button text
21   widget.Font = Font.Helvetica;
22   widget.FontSize = 0; // autosize
23   widget.BorderStyle = 
24     TallComponents.PDF.Annotations.BorderStyle.Beveled;
25   widget.BorderColor = RgbColor.Black;
26 }

Add a JavaScript Action

The implementation of helper method addJavaScript is straightforward. It first retrieves the widget from the field that is passed as an argument. Next it creates a JavaScript action. The JavaScriptAction is assigned to the widget.MouseUpActions collection. This tells the PDF reader to execute this action when the mouse is released after pressing the button. Finally, the JavaScript action is assigned a snippet of JavaScript which is quite straightforward. Here is the code:


static void addJavaScript( Field field )
{
  // retrieve the widget
  Widget widget = field.Widgets[0];

  // create a new JavaScript action and associate it
  // with the mouse-up event
  JavaScriptAction action = new JavaScriptAction();
  widget.MouseUpActions.Add(action);
 
  // set the JavaScript
  action.JavaScript.Text = 
    "this.getField('SUM').value = " +
    "this.getField('A').value + " +
    "this.getField('B').value;";
}
 1 static void addJavaScript( Field field )
 2 {
 3   // retrieve the widget
 4   Widget widget = field.Widgets[0];
 5 
 6   // create a new JavaScript action and associate it
 7   // with the mouse-up event
 8   JavaScriptAction action = new JavaScriptAction();
 9   widget.MouseUpActions.Add(action);
10  
11   // set the JavaScript
12   action.JavaScript.Text = 
13     "this.getField('SUM').value = " +
14     "this.getField('A').value + " +
15     "this.getField('B').value;";
16 }