This article demonstrates how to load, edit, and read form fields in a Word document using GroupDocs.Editor for Node.js via 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
Specify the path to your input Word document (.docx) that contains form fields.
constinputFilePath='path/to/your/document.docx';
Create a Readable Stream from the Path
Create a readable stream from the input file path.
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.
Obtain the FormFieldManager instance from the Editor. Use it to read the FormFieldCollection and process each form field based on its type.
// Get the FormFieldManager instance
constfieldManager=editor.getFormFieldManager();// Get the FormFieldCollection in the document
constcollection=fieldManager.getFormFieldCollection();// Iterate over each form field in the collection
collection.forEach((formField)=>{switch(formField.getType()){casegroupdocsEditor.FormFieldType.Text:consttextFormField=collection.getFormField(formField.getName(),groupdocsEditor.TextFormField);console.log(`TextFormField detected, name: ${formField.getName()}, value: ${textFormField.getValue()}`);break;casegroupdocsEditor.FormFieldType.CheckBox:constcheckBoxFormField=collection.getFormField(formField.getName(),groupdocsEditor.CheckBoxForm);console.log(`CheckBoxForm detected, name: ${formField.getName()}, value: ${checkBoxFormField.getValue()}`);break;casegroupdocsEditor.FormFieldType.Date:constdateFormField=collection.getFormField(formField.getName(),groupdocsEditor.DateFormField);console.log(`DateFormField detected, name: ${formField.getName()}, value: ${dateFormField.getValue()}`);break;casegroupdocsEditor.FormFieldType.Number:constnumberFormField=collection.getFormField(formField.getName(),groupdocsEditor.NumberFormField);console.log(`NumberFormField detected, name: ${formField.getName()}, value: ${numberFormField.getValue()}`);break;casegroupdocsEditor.FormFieldType.DropDown:constdropDownFormField=collection.getFormField(formField.getName(),groupdocsEditor.DropDownFormField);constselectedValue=dropDownFormField.getValue()[dropDownFormField.getSelectedIndex()];console.log(`DropDownFormField detected, name: ${formField.getName()}, selected value: ${selectedValue}`);break;default:console.log(`Unknown form field type detected, name: ${formField.getName()}`);}});
In this code:
We use the getType() method to determine the type of each form field.
Retrieve the specific form field instance using getFormField() with the appropriate class.
Access the properties of each form field and print them to the console.
Complete Example Code
Below is the complete example code demonstrating the entire process:
/**
* This example demonstrates how to load, edit, and read form fields in a Word document using GroupDocs.Editor for Node.js via Java.
*/// Import the necessary modules
constfs=require('fs');constgroupdocsEditor=require('groupdocs-editor');(async()=>{try{// 1. Get the path to the input file (or stream with file content).
// In this case, it is a sample DOCX with form fields.
constinputFilePath='path/to/your/document.docx';// 2. Create a readable stream from this path
constinputStream=fs.createReadStream(inputFilePath);// 3. Create load options for this document
constloadOptions=newgroupdocsEditor.WordProcessingLoadOptions();// 3.1. If the input document is password-protected, specify the 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 into the Editor instance
consteditor=newgroupdocsEditor.Editor(inputStream,loadOptions);// 5. Retrieve and process the form fields
// 5.1. Get the FormFieldManager instance
constfieldManager=editor.getFormFieldManager();// 5.2. Get the FormFieldCollection in the document
constcollection=fieldManager.getFormFieldCollection();// 5.3. Iterate over each form field in the collection
collection.forEach((formField)=>{switch(formField.getType()){casegroupdocsEditor.FormFieldType.Text:consttextFormField=collection.getFormField(formField.getName(),groupdocsEditor.TextFormField);console.log(`TextFormField detected, name: ${formField.getName()}, value: ${textFormField.getValue()}`);break;casegroupdocsEditor.FormFieldType.CheckBox:constcheckBoxFormField=collection.getFormField(formField.getName(),groupdocsEditor.CheckBoxForm);console.log(`CheckBoxForm detected, name: ${formField.getName()}, value: ${checkBoxFormField.getValue()}`);break;casegroupdocsEditor.FormFieldType.Date:constdateFormField=collection.getFormField(formField.getName(),groupdocsEditor.DateFormField);console.log(`DateFormField detected, name: ${formField.getName()}, value: ${dateFormField.getValue()}`);break;casegroupdocsEditor.FormFieldType.Number:constnumberFormField=collection.getFormField(formField.getName(),groupdocsEditor.NumberFormField);console.log(`NumberFormField detected, name: ${formField.getName()}, value: ${numberFormField.getValue()}`);break;casegroupdocsEditor.FormFieldType.DropDown:constdropDownFormField=collection.getFormField(formField.getName(),groupdocsEditor.DropDownFormField);constselectedValue=dropDownFormField.getValue()[dropDownFormField.getSelectedIndex()];console.log(`DropDownFormField detected, name: ${formField.getName()}, selected value: ${selectedValue}`);break;default:console.log(`Unknown form field type detected, name: ${formField.getName()}`);}});console.log('Working with form fields routine has successfully finished.');}catch(error){console.error('An error occurred:',error);}})();
Conclusion
This guide demonstrates how to work with form fields in Word documents using GroupDocs.Editor for Node.js via 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.
Note: Make sure to replace 'path/to/your/document.docx' with the actual file path in your application.