This article demonstrates how to edit form fields in a Word document using GroupDocs.Editor for Java. The example provided guides you through loading a document, modifying form fields, and saving the updated document.
Step-by-Step Guide
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.
Use the Editor class to load the document with the specified load options.
Editoreditor=newEditor(fs,loadOptions);{// Further code will be placed here
}
Retrieve and Update Form Fields
Obtain the FormFieldManager instance from the Editor and read the FormFieldCollection. Update a specific text form field by modifying its properties and then call the UpdateFormFiled method to apply the changes.
Initialize the WordProcessingSaveOptions with the desired format. Optimize memory usage if necessary, and protect the document from writing with a password.
Print a confirmation message to indicate the successful completion of the operation.
System.out.println("EditFormFieldCollection routine has successfully finished");
Complete Example Code
Below is the complete example code demonstrating the entire process:
/**
* This example demonstrates how to edit form fields in a Word document using GroupDocs.Editor for Java.
*/publicclassEditFormFieldCollection{/**
* Runs the example to demonstrate loading, editing, and saving form fields in 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. 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
Editoreditor=newEditor(fs,loadOptions);// 4.1. Read the FormFieldManager instance
FormFieldManagerfieldManager=editor.getFormFieldManager();// 4.2. Read the FormFieldCollection in the document
FormFieldCollectioncollection=fieldManager.getFormFieldCollection();// 4.3. Update a specific text form field
TextFormFieldtextField=collection.getFormField("Text1",TextFormField.class);textField.setLocaleId(1029);textField.setValue("new Value");fieldManager.updateFormFields(collection);// 5. Create document save options
WordProcessingFormatsdocFormat=WordProcessingFormats.Docx;WordProcessingSaveOptionssaveOptions=newWordProcessingSaveOptions(docFormat);// 5.1. If the document is large and causes OutOfMemoryException, set the memory optimization option
saveOptions.setOptimizeMemoryUsage(true);// 5.2. Protect the document from writing (allow only form fields) with a password
saveOptions.setProtection(newWordProcessingProtection(WordProcessingProtectionType.AllowOnlyFormFields,"write_password"));// 6. Save the document
// 6.1. Prepare a stream for saving
ByteArrayOutputStreamoutputStream=newByteArrayOutputStream();{// 6.2. Save the document
editor.save(outputStream,saveOptions);}System.out.println("EditFormFieldCollection routine has successfully finished");}}
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.