On this page
Guides
Fill Form Fields
ComPDFKit supports programmatically filling form fields in a PDF document.
Since form fields are a special type of annotation, inheriting from the annotation class, the interface for annotations applies to form fields.
The steps to fill form fields using code are as follows:
- Obtain the page object from CPDFDocument where you want to fill in the form.
- Retrieve all annotations from the page object.
- Iterate through all annotations to find the form to be filled.
- Modify the form field content as needed.
This example shows how to fill form fields:
java
CPDFDocument document = new CPDFDocument();
document.open(pdfPath);
for (int i = 0; i < document.getPageCount(); i++) {
CPDFPage page = document.pageAtIndex(i);
for (CPDFAnnotation annotation : page.getAnnotations()) {
switch (annotation.getType()) {
case WIDGET:
CPDFWidget cpdfWidget = (CPDFWidget) annotation;
switch (cpdfWidget.getWidgetType()) {
case Widget_TextField:
CPDFTextWidget textWidget = (CPDFTextWidget) cpdfWidget;
textWidget.setText("test");
textWidget.updateAp();
break;
case Widget_RadioButton:
CPDFRadiobuttonWidget radiobuttonWidget = (CPDFRadiobuttonWidget) cpdfWidget;
radiobuttonWidget.setChecked(true);
radiobuttonWidget.updateAp();
break;
case Widget_ListBox:
CPDFListboxWidget listBoxWidget = (CPDFListboxWidget) cpdfWidget;
listBoxWidget.setSelectedIndexes(new int[]{0});
listBoxWidget.updateAp();
break;
default:
break;
}
break;
default:
break;
}
}
}