Some test text!

Search
Hamburger Icon

Nodejs / Guides / Create form fields

Create new form fields and widget annotations in Node.js

To create new form fields and widget annotations within a new document.

async function main() {
  // Create a blank page
  const doc = await PDFNet.PDFDoc.create();
  const blankPage = await doc.pageCreate();

  // Create a new text field and text widget/annotation (aka AcroForms).
  const textField = await doc.fieldCreate(
    "employee.name",
    PDFNet.Field.Type.e_text
  );
  const text = await PDFNet.TextWidget.createWithField(
    doc,
    await PDFNet.Rect.init(110, 660, 380, 690),
    textField
  );
  await text.setFont(
    await PDFNet.Font.Create(doc, PDFNet.Font.StandardType1Font.e_times_bold)
  );
  await text.refreshAppearance();
  await blankPage.annotPushBack(text);

  // Create a new signature field and signature annotation (aka AcroForms)
  const sigField = await doc.createDigitalSignatureField("employee.signature");
  const signature = await PDFNet.SignatureWidget.createWithField(
    doc,
    await PDFNet.Rect.init(0, 100, 200, 150),
    sigField
  );
  await signature.refreshAppearance();
  await blankPage.annotPushBack(signature);

  // Create a new checkbox field and checkbox widget/annotation (aka AcroForms)
  const checkboxField = await doc.fieldCreate(
    "employee.checkbox",
    PDFNet.Field.Type.e_check
  );
  const checkbox = await PDFNet.CheckBoxWidget.createWithField(
    doc,
    await PDFNet.Rect.init(190, 490, 250, 540),
    checkboxField
  );
  await checkbox.setBackgroundColor(await PDFNet.ColorPt.init(1, 1, 1), 3);
  await checkbox.setBorderColor(await PDFNet.ColorPt.init(0, 0, 0), 3);
  await checkbox.setChecked(true); // Check the widget (by default it is unchecked).
  await checkbox.refreshAppearance();
  await blankPage.annotPushBack(checkbox);

  // Create a radio button group and add three radio button widget/annotation (aka AcroForms) in it.
  const radioGroup = await PDFNet.RadioButtonGroup.Create(
    doc,
    "employee.radiogroup"
  );
  const radiobutton1 = await radioGroup.add(
    await PDFNet.Rect.init(140, 410, 190, 460)
  );
  await radiobutton1.setBackgroundColor(await PDFNet.ColorPt.init(1, 1, 0), 3);
  await radiobutton1.refreshAppearance();
  const radiobutton2 = await radioGroup.add(
    await PDFNet.Rect.init(310, 410, 360, 460)
  );
  await radiobutton2.setBackgroundColor(await PDFNet.ColorPt.init(0, 1, 0), 3);
  await radiobutton2.refreshAppearance();
  const radiobutton3 = await radioGroup.add(
    await PDFNet.Rect.init(480, 410, 530, 460)
  );
  await radiobutton3.enableButton(); // Enable the third radio button. By default the first one is selected
  await radiobutton3.setBackgroundColor(await PDFNet.ColorPt.init(0, 1, 1), 3);
  await radiobutton3.refreshAppearance();
  await radioGroup.addGroupButtonsToPage(blankPage);

  // Add the page as the last page in the document.
  await doc.pagePushBack(blankPage);
}
PDFNet.runWithCleanup(main);

PDF interactive forms (AcroForms)
Full code sample which illustrates some basic PDFNet capabilities related to interactive forms (also known as AcroForms).

About creating form fields

Regardless of which field type you create, you must provide a Field name:

const my_field = await doc.fieldCreate(
  "address",
  PDFNet.Field.Type.e_text,
  null
);

Under most circumstances, field names must be unique. If you have a field you name as "address" and you create a second field you likewise call "address", you cannot supply different data in the two fields.

Field names can use alphanumeric characters to identify a field. All field names are case-sensitive. For example, you can use names such as empFirstName, empSecondName, empNumber, and so on for a group of fileds that are related to the same concept (in our sample employee entity).

Another technique for naming fields is to use a parent and child name. For example, you could name the above fields as follows: employee.name.first, employee.name.second, employee.number.

This naming convention is not only useful for organizing purposes but is well-suited for automatic operations on Fields. In the Apryse SDK, Field.GetName() returns a string representing the fully qualified name of the field (e.g. "employee.name.first"). To get the child name ("first") use the Field.GetPartialName() method.

For more information about adding Fields, see the FDF code sample .

Understand field types

PDF offers six different field types. Each type of form field is used for a different purpose, and they have different properties, appearances, options, and actions that can be associated with the fields. In this section, we will explain how to create all the seven field types and some attributes specific to each one.

Common field types are text-box, checkbox, radio-button, combo-box, and push-button. To find out the type of the Field use Field.GetType() method:

const type = await field.getType();
switch (type) {
  case PDFNet.Field.Type.e_button:
    console.log("Button");
    break;
  case PDFNet.Field.Type.e_check:
    console.log("Check");
    break;
  case PDFNet.Field.Type.e_radio:
    console.log("Radio");
    break;
  case PDFNet.Field.Type.e_text:
    console.log("Text");
    break;
  case PDFNet.Field.Type.e_choice:
    console.log("Choice");
    break;
  case PDFNet.Field.Type.e_signature:
    console.log("Signature");
    break;
}

Get the answers you need: Chat with us