This article demonstrates how to load, edit, and read form fields in a Word document using GroupDocs.Editor for Java. We will go through the process of opening a document, retrieving form fields, and processing them based on their types.
Step-by-Step Guide
Get the Path to the Input File
Create a InputStream from the Path
Create a InputStream from the input file path.
InputStreamfs=newFileInputStream(inputFilePath);{// Further code will be placed here
}
Create Load Options
Initialize the WordProcessingLoadOptions for loading the document. If your document is password-protected, specify the password. In this example, the document is unprotected, so the password will be ignored.
Below is the complete example code demonstrating the entire process:
/**
* This example demonstrates loading a FormFieldCollection and reading form fields.
*/publicclassLegacyFormFieldCollection{/**
* Runs the example to demonstrate loading, editing, and reading form fields from a document.
*/publicstaticvoidrun()throwsException{// 1. Get a path to the input file (or stream with file content).
// In this case, it is a sample Docx with form fields.
StringinputFilePath=Constants.SampleLegacyFormFields_docx;// 2. Create a stream from this path
InputStreamfs=newFileInputStream(inputFilePath);// 3. Create load options for this document
WordProcessingLoadOptionsloadOptions=newWordProcessingLoadOptions();// 3.1. In case the input document is password-protected, we can specify a password for its opening...
loadOptions.setPassword("some_password_to_open_a_document");// 3.2. ...but, because the document is unprotected, this password will be ignored
// 4. Load the document with options to the Editor instance
Editoreditor=newEditor(fs,loadOptions);// 4.1. Read the FormFieldManager instance
FormFieldManagerfieldManager=editor.getFormFieldManager();// 4.2. Read the FormFieldCollection in the document
FormFieldCollectioncollection=fieldManager.getFormFieldCollection();for(IFormFieldformField:collection){switch(formField.getType()){caseFormFieldType.Text:TextFormFieldtextFormField=collection.getFormField(formField.getName(),TextFormField.class);System.out.println("TextFormField detected, name: "+formField.getName()+", value: "+textFormField.getValue());break;caseFormFieldType.CheckBox:CheckBoxFormcheckBoxFormField=collection.getFormField(formField.getName(),CheckBoxForm.class);System.out.println("CheckBoxForm detected, name: "+formField.getName()+", value: "+checkBoxFormField.getValue());break;caseFormFieldType.Date:DateFormFielddateFormField=collection.getFormField(formField.getName(),DateFormField.class);System.out.println("DateFormField detected, name: "+formField.getName()+", value: "+dateFormField.getValue());break;caseFormFieldType.Number:NumberFormFieldnumberFormField=collection.getFormField(formField.getName(),NumberFormField.class);System.out.println("NumberFormField detected, name: "+formField.getName()+", value: "+numberFormField.getValue());break;caseFormFieldType.DropDown:DropDownFormFielddropDownFormField=collection.getFormField(formField.getName(),DropDownFormField.class);System.out.println("DropDownFormField detected, name: "+formField.getName()+", value selected: "+dropDownFormField.getValue().get(dropDownFormField.getSelectedIndex()));break;}}System.out.println("ReadFormFieldCollection routine has successfully finished");}}
Conclusion
This guide demonstrates how to work with form fields in Word documents using GroupDocs.Editor for Java. By following these steps, you can load a document, retrieve form fields, and process them based on their types. This functionality is essential for applications that require manipulation and extraction of form field data from documents.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.