Skip to content
Guides

Create Form Fields

Create Text Fields

Text fields allow users to input text in a designated area, commonly used for collecting user information or filling out forms.

The steps to create a text field are as follows:

  1. Obtain the page object from CPDFDocument where the text field needs to be created.
  2. Create a text field on the page object.
  3. Set the position and other properties of the text field.

This example shows how to create a text field:

java
CPDFDocument document = new CPDFDocument();
document.open(pdfPath);
int pageNumber = 0;
CPDFPage page = document.pageAtIndex(pageNumber);
RectF pageSize = document.getPageSize(pageNumber);
RectF singleLineTextRect = new RectF(28, 32, 237, 75);
//Insert a single-line TextField.
CPDFTextWidget textField = (CPDFTextWidget) page.addFormWidget(CPDFWidget.WidgetType.Widget_TextField,document);
textField.setRect(singleLineTextRect);
textField.setFieldName("TextField1");
textField.setText("Basic Text Field");
textField.setFontColor(Color.BLACK.getRGB());
textField.setFontSize(15);
textField.setFillColor(Color.RED.getRGB());
textField.setBorderColor(Color.YELLOW.getRGB());
textField.setBorderWidth(5F);
textField.updateAp();

Create Buttons

Buttons allow users to perform actions on PDF pages, such as page navigation and hyperlink jumps.

The steps to create a button are as follows:

  1. Obtain the page object from CPDFDocument.
  2. Create a button on the page object.
  3. Set the button's position and appearance properties.
  4. Create and set the text properties of the button.
  5. Create and set the actions of the button.
  6. Update the appearance of the button.

This example shows how to create a button:

java
CPDFDocument document = CPDFDocument();
document.open(pdfPath);
RectF pageSize = document.getPageSize(0);
CPDFPage cpdfPage = document.pageAtIndex(0);

// Create a button for navigating to the second page.
RectF pushButton1Rect = new RectF(267F, 203F, 401F, 235F);
CPDFPushbuttonWidget pushButtonWidget1 = (CPDFPushbuttonWidget) cpdfPage.addFormWidget(CPDFWidget.WidgetType.Widget_PushButton,document);
pushButtonWidget1.setRect(pushButton1Rect);
pushButtonWidget1.setFieldName("PushButton1");
pushButtonWidget1.setFontColor(Color.BLACK.getRGB());
pushButtonWidget1.setFontSize(15);
pushButtonWidget1.setButtonTitle("PushButton");
CPDFGoToAction goToAction = new CPDFGoToAction();
CPDFDestination destination = new CPDFDestination(1, 0, 842, 0F);
goToAction.setDestination(document, destination);
pushButtonWidget1.setButtonAction(goToAction);
pushButtonWidget1.updateAp();

// Create a button for navigating to a web page.
RectF pushButton2Rect = new RectF(433F, 203F, 567F, 235F);
CPDFPushbuttonWidget pushButtonWidget2 = (CPDFPushbuttonWidget) cpdfPage.addFormWidget(CPDFWidget.WidgetType.Widget_PushButton,document);
pushButtonWidget2.setRect(pushButton2Rect);
pushButtonWidget2.setFieldName("PushButton2");
pushButtonWidget2.setFontColor(Color.BLACK.getRGB());
pushButtonWidget2.setFontSize(15);
pushButtonWidget2.setButtonTitle("PushButton");
CPDFUriAction uriAction = new CPDFUriAction();
uriAction.setUri("https://www.compdf.com/");
pushButtonWidget2.setButtonAction(uriAction);
pushButtonWidget2.updateAp();

Create List Boxes

List boxes enable users to choose one or multiple items from a predefined list, providing a convenient data selection feature.

The steps to create a list box are as follows:

  1. Obtain the page object from CPDFDocument where the list box needs to be created.
  2. Create a list box on the page object.
  3. Set the position of the list box.
  4. Add list items to the list box.
  5. Set the properties of the list box.

This example shows how to create a list box:

java
CPDFDocument document = new CPDFDocument();
document.open(pdfPath);
CPDFPage cpdfPage = document.pageAtIndex(0);
RectF pageSize = document.getPageSize(0);

RectF listBoxRect = new RectF(267F, 32F, 567F, 138F);
CPDFListboxWidget listBoxWidget = (CPDFListboxWidget) cpdfPage.addFormWidget(CPDFWidget.WidgetType.Widget_ListBox,document);
listBoxWidget.setRect(listBoxRect);
listBoxWidget.setFieldName("ListBox1");
listBoxWidget.setFillColor(Color.WHITE.getRGB());
listBoxWidget.setBorderColor(Color.BLACK.getRGB());
listBoxWidget.setBorderWidth(2);
CPDFWidgetItem[] listBoxItems = new CPDFWidgetItem[]{
  new CPDFWidgetItem("List Box No.1", "List Box No.1"),
  new CPDFWidgetItem("List Box No.2", "List Box No.2"),
  new CPDFWidgetItem("List Box No.3", "List Box No.3"),
};
listBoxWidget.setOptionItems(listBoxItems);
listBoxWidget.setSelectedIndexes(new int[]{1});
listBoxWidget.updateAp();

Create Signature Fields

Signature fields allow users to insert digital or electronic signatures into a document, verifying the authenticity and integrity of the document.

The steps to create a signature field are as follows:

  1. Obtain the page object from CPDFDocument where the signature field needs to be added.
  2. Create a signature field on the page object.
  3. Set the position of the signature field.
  4. Set the properties of the signature field.

This example shows how to create a signature field:

java
CPDFDocument document = new CPDFDocument();
document.open(pdfPath);
CPDFPage cpdfPage = document.pageAtIndex(0);
RectF pageSize = document.getPageSize(0);

RectF signatureRect = new RectF(28, 206, 237, 301);
CPDFSignatureWidget signatureWidget = (CPDFSignatureWidget) cpdfPage.addFormWidget(CPDFWidget.WidgetType.Widget_SignatureFields,document);
signatureWidget.setFieldName("Signature1");
signatureWidget.setFillColor(Color.WHITE.getRGB());
signatureWidget.setBorderColor(Color.BLACK.getRGB());
signatureWidget.setBorderWidth(2F);
signatureWidget.setRect(signatureRect);
signatureWidget.updateAp();

Create Check Boxes

Check boxes allow users to indicate the state of an option by checking or unchecking it.

The steps to create a check box are as follows:

  1. Obtain the page object from CPDFDocument where the checkbox needs to be added.
  2. Create a checkbox on the page object.
  3. Set the position of the checkbox.
  4. Set the properties of the checkbox.

This example shows how to create a checkbox:

java
CPDFDocument document = new CPDFDocument();
document.open(pdfPath);
CPDFPage cpdfPage = document.pageAtIndex(0);
RectF pageSize = document.getPageSize(0);

RectF checkBoxRect = new RectF(267, 251, 299, 283);
CPDFCheckboxWidget checkboxWidget = (CPDFCheckboxWidget) cpdfPage.addFormWidget(CPDFWidget.WidgetType.Widget_CheckBox,document);
checkboxWidget.setRect(checkBox1);
checkboxWidget.setFieldName("CheckBox1");
checkboxWidget.setFillColor(Color.parseColor("#CCE5E5FF").getRGB());
checkboxWidget.setBorderColor(Color.BLACK.getRGB());
checkboxWidget.setBorderWidth(2F);
checkboxWidget.setChecked(false);
checkboxWidget.updateAp();

Create Radio Buttons

Radio buttons allow users to select a unique option from a predefined group of options.

The steps to create a radio button are as follows:

  1. Obtain the page object from CPDFDocument where the radio button needs to be added.
  2. Create a radio button on the page object.
  3. Set the position of the radio button.
  4. Set the properties of the radio button.

This example shows how to create a radio button:

java
CPDFDocument document = new CPDFDocument();
document.open(pdfPath);
CPDFPage cpdfPage = document.pageAtIndex(0);
RectF pageSize = document.getPageSize(0);

RectF radioButtonRect = new RectF(385F, 251F, 424F, 290F);
CPDFRadiobuttonWidget radiobuttonWidget = (CPDFRadiobuttonWidget) cpdfPage.addFormWidget(CPDFWidget.WidgetType.Widget_RadioButton,document);
radiobuttonWidget.setRect(radioButtonRect);
radiobuttonWidget.setFieldName("RadioButton");
radiobuttonWidget.setCheckStyle(CPDFWidget.CheckStyle.CK_Circle);
radiobuttonWidget.setFillColor(Color.decode("#CCE5E5FF").getRGB());
radiobuttonWidget.setBorderColor(Color.BLACK.getRGB());
radiobuttonWidget.setBorderWidth(2F);
radiobuttonWidget.setChecked(false);
radiobuttonWidget.setFieldName("RadioButton1");
radiobuttonWidget.updateAp();

Create Combo Boxes

A combo box is an area where a selected item from the dropdown will be displayed.

The steps to create a combo box are as follows:

  1. Obtain the page object from CPDFDocument where the combo box needs to be added.
  2. Create a combo box on the page object.
  3. Add list items to the combo box.
  4. Set the properties of the combo box.

This example shows how to create a combo box:

java
CPDFDocument document = new CPDFDocument();
document.open(pdfPath);
CPDFPage cpdfPage = document.pageAtIndex(0);
RectF pageSize = document.getPageSize(0);

RectF comboBoxRect = new RectF(267F, 143F, 567F, 189F);
CPDFComboboxWidget comboBoxWidget = (CPDFComboboxWidget) cpdfPage.addFormWidget(CPDFWidget.WidgetType.Widget_ComboBox,document);
comboBoxWidget.setRect(comboBoxRect);
comboBoxWidget.setFieldName("ComboBox1");
CPDFWidgetItem[] comboBoxItems = new CPDFWidgetItem[]{
  new CPDFWidgetItem("Combo Box No.1", "Combo Box No.1"),
  new CPDFWidgetItem("Combo Box No.2", "Combo Box No.2"),
  new CPDFWidgetItem("Combo Box No.3", "Combo Box No.3"),
};
comboBoxWidget.setOptionItems(comboBoxItems, new int[]{1});
comboBoxWidget.updateAp();