Skip to content

Creating and Filling Out Forms Programmatically on Android

Youna edited this page Mar 1, 2023 · 1 revision

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.

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");`
`}`

} }