Creating and Filling Out Forms Programmatically on Android

Tutorials | Java · How to · Android Tue. 28 Jun. 2022

Do you have lots of PDF forms to deal with? PDF forms play a significant role in our daily life, and there are a variety of things you can accomplish with PDF forms, such as creating surveys, collecting information, and calculating financial statements. 

 

ComPDFKit PDF SDK fully offers the ability to read, create, fill, and edit PDF forms and provides utility methods to make working with forms simple and efficient. And in this blog, we’ll delve into creating and filling out forms programmatically.

 

 

What Are Forms?

 

The concept of forms in PDFs is similar to forms in the physical world. You are able to enter unique information into a preformatted page in PDF forms. As it’s an electronic format, the previously entered information in PDF forms can be edited again, which is convenient for users.

 

 

Supported Form Fields

 

Under the hood, PDF form fields are a type of PDF annotation called widget annotations. And ComPDFKit PDF SDK supports all form types specified by the PDF specification like text boxes, checkboxes, radio buttons, drop-down lists, pushbuttons, and signatures.

 

CPDFWidget is the base class for all form fields, and CPDFWidget is a subclass for CPDFAnnotation. A CPDFWidget object by itself is not useful, only subclasses (CPDFCheckboxWidgetCPDFRadiobuttonWidget, etc.) are interesting. In parsing a PDF, however, any unknown or unsupported form fields will be filtered out.

 

We have to differentiate between field types and annotation objects:

 

Type Annotation Object
Check Boxes CPDFCheckboxWidget
Radio Buttons CPDFRadiobuttonWidget
Push Buttons CPDFPushbuttonWidget
List Boxes CPDFListboxWidget
Combo Boxes CPDFComboboxWidget
Text CPDFTextWidget
Signatures CPDFSignatureWidget
 
 

Creating Forms

 

Creating form fields works the same as adding any other annotation, as programmatically creating annotations.

 

int pageNumber = 0;
RectF pageSize = readerView.getPageNoZoomSize(pageNumber);
RectF insertRect = new RectF(0,0,100,100);
//coordinate conversion
insertRect = document.pageAtIndex(pageNumber).convertRectToPage(false,pageSize.width(),pageSize.height(),insertRect);
CPDFCheckboxWidget checkboxWidget = (CPDFCheckboxWidget) document.pageAtIndex(page).addFormWidget(CPDFWidget.WidgetType.Widget_CheckBox);
checkboxWidget.setRect(insertRect);
checkboxWidget.setChecked(true);
checkboxWidget.updateFormAp();
readerView.reloadPages();

 

 

Filling Out Forms

 

ComPDFKit fully supports the AcroForm (An interactive form, is a collection of fields for gathering information interactively from the user) standard, and forms can be viewed and filled inside the CPDFView.

 

To fill in a text form element, tap it and then type text using either the onscreen keyboard or an attached hardware keyboard. Then tap either the Done button above the keyboard or any blank area on the page to deselect the form element, which will commit the changes.

 

 

To set the value of a choice form element (a list or combo box), tap the element, and then select an item from the list, or type in a custom item.

 

 

 

To enable or disable a checkbox form element, tap it to toggle its state. And you can set the selection of a radio button form element by tapping the desired item.

 

 

While a form element is selected (focused), the left and right arrows above the keyboard may be used to move the focus sequentially between all the form elements on the page.

 

The following example demonstrates how form fields can be queried and filled with code:

 

int pageNumber = 0;
CPDFPage page = document.pageAtIndex(pageNumber);
List<CPDFAnnotation> annotations = page.getAnnotations();
for (CPDFAnnotation annotation:annotations) {
  if (annotation.getType() == CPDFAnnotation.Type.WIDGET) {
    if (((CPDFWidget) annotation).getWidgetType() == CPDFWidget.WidgetType.Widget_CheckBox) {
      ((CPDFCheckboxWidget) annotation).setChecked(true);
    }

    if (((CPDFWidget) annotation).getWidgetType() == CPDFWidget.WidgetType.Widget_TextField) {
      ((CPDFTextWidget) annotation).setText("Hello world");
    }
  }
}

 

 

Conclusion

 

Although we got some information about PDF forms and saw how to use our solution to create and fill out forms in Java, we’ve only just begun to scratch the surface. Contact us and request a free trial to learn a bit more about the full capabilities of our PDF library.

Ready to Get Started?

Download our all-in-one ComPDFKit for free and run it to your project within minutes!