Form fields are data fields that let you collect data from the filled forms. There are many types of form fields available in PDF, such as text fields, list boxes, radio buttons, combo boxes and check boxes. In this article, I will introduce how to read values of these PDF form fields programmatically in Java.
Add Dependencies
In order to read form fields, I used Free Spire.PDF for Java library. There are two ways to include Free Spire.PDF for Java in your Java project:
For maven projects:
Add the following dependency to your project’s pom.xml file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>http: //repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId> e-iceblue </groupId> <artifactId>spire.pdf.free</artifactId> <version> 4.4 . 1 </version> </dependency> </dependencies> |
For non-maven projects:
Download Free Spire.PDF for Java pack from here: Download- Free Spire.PDF for Java, extract the zip file, then add Spire.Pdf.jar in the lib folder into your project as a dependency.
Read Form Field values
You can either read all of the form fields or read a particular form field as shown in the following examples.
The input PDF:
Example 1. Read values of all form fields
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | import com.spire.pdf.PdfDocument; import com.spire.pdf.fields.PdfField; import com.spire.pdf.widget.*; import java.io.FileWriter; import java.io.IOException; public class ReadFormFields { public static void main(String []args){ //Load PDF document PdfDocument pdf = new PdfDocument(); pdf.loadFromFile( "FormFields.pdf" ); //Get form fields PdfFormWidget formWidget = (PdfFormWidget)pdf.getForm(); StringBuilder sb = new StringBuilder(); //Loop through the form field widget collection and extract the value of each field for ( int i = 0 ; i < formWidget.getFieldsWidget().getCount(); i++) { PdfField field = (PdfField)formWidget.getFieldsWidget().getList().get(i); if (field instanceof PdfTextBoxFieldWidget) { PdfTextBoxFieldWidget textBoxField = (PdfTextBoxFieldWidget)field ; //Get text of textbox String text = textBoxField.getText(); sb.append( "The text in textbox is: " + text + "\r\n" ); } if (field instanceof PdfListBoxWidgetFieldWidget) { PdfListBoxWidgetFieldWidget listBoxField = (PdfListBoxWidgetFieldWidget)field; sb.append( "Listbox items are: \r\n" ); //Get values of listbox PdfListWidgetItemCollection items = listBoxField.getValues(); for (PdfListWidgetItem item : (Iterable<PdfListWidgetItem>) items) { sb.append(item.getValue() + "\r\n" ); } //Get selected value String selectedValue = listBoxField.getSelectedValue(); sb.append( "The selected value in the listbox is: " + selectedValue + "\r\n" ); } if (field instanceof PdfComboBoxWidgetFieldWidget) { PdfComboBoxWidgetFieldWidget comBoxField = (PdfComboBoxWidgetFieldWidget)field ; sb.append( "comBoxField items are: \r\n" ); //Get values of comboBox PdfListWidgetItemCollection items = comBoxField.getValues(); for (PdfListWidgetItem item : (Iterable<PdfListWidgetItem>) items) { sb.append(item.getValue() + "\r\n" ); } //Get selected value String selectedValue = comBoxField.getSelectedValue(); sb.append( "The selected value in the comBoxField is: " + selectedValue + "\r\n" ); } if (field instanceof PdfRadioButtonListFieldWidget) { PdfRadioButtonListFieldWidget radioBtnField = (PdfRadioButtonListFieldWidget)field; //Get value of radio button String value = radioBtnField.getValue(); sb.append( "The text in radioButtonField is: " + value + "\r\n" ); } if (field instanceof PdfCheckBoxWidgetFieldWidget) { PdfCheckBoxWidgetFieldWidget checkBoxField = (PdfCheckBoxWidgetFieldWidget)field; //Get the checked state of the checkbox boolean state = checkBoxField.getChecked(); sb.append( "Is the checkBox checked? " + state + "\r\n" ); } } try { //Write text into a .txt file FileWriter writer = new FileWriter( "GetAllValues.txt" ); writer.write(sb.toString()); writer.flush(); } catch (IOException e) { e.printStackTrace(); } pdf.close(); } } |
Output:
Example 2. Read values of a particular form field
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import com.spire.pdf.PdfDocument; import com.spire.pdf.widget.PdfFormWidget; import com.spire.pdf.widget.PdfTextBoxFieldWidget; import java.io.FileWriter; import java.io.IOException; public class ReadParticularFormField { public static void main(String[] args){ //Load PDF document PdfDocument pdf = new PdfDocument(); pdf.loadFromFile( "FormFields.pdf" ); //Get form fields PdfFormWidget formWidget = (PdfFormWidget)pdf.getForm(); //Get the textbox by index or by name PdfTextBoxFieldWidget textbox = ( PdfTextBoxFieldWidget)formWidget.getFieldsWidget().get( 0 ); //PdfTextBoxFieldWidget textbox = ( PdfTextBoxFieldWidget)formWidget.getFieldsWidget().get("Text1"); //Get the text of the textbox String text = textbox.getText(); try { //Write text into a .txt file FileWriter writer = new FileWriter( "GetSpecificFieldValue.txt" ); writer.write(text); writer.flush(); } catch (IOException e) { e.printStackTrace(); } pdf.close(); } } |
Output:
Comments
Post a Comment