This article demonstrates how to load, edit, and read form fields in a Word document using GroupDocs.Editor for .NET. 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 Stream from the Path
Create a FileStream from the input file path.
using(FileStreamfs=File.OpenRead(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:
/// <summary>/// This example demonstrates loading a FormFieldCollection and reading form fields./// </summary>internalstaticclassLegacyFormFieldCollection{/// <summary>/// Runs the example to demonstrate loading, editing, and reading form fields from a document./// </summary>internalstaticvoidRun(){// 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 pathusing(FileStreamfs=File.OpenRead(inputFilePath)){// 3. Create load options for this documentWordProcessingLoadOptionsloadOptions=newWordProcessingLoadOptions();// 3.1. In case the input document is password-protected, we can specify a password for its opening...loadOptions.Password="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 instanceusing(Editoreditor=newEditor(fs,loadOptions)){// 4.1. Read the FormFieldManager instanceFormFieldManagerfieldManager=editor.FormFieldManager;// 4.2. Read the FormFieldCollection in the documentFormFieldCollectioncollection=fieldManager.FormFieldCollection;foreach(varformFieldincollection){switch(formField.Type){caseFormFieldType.Text:TextFormFieldtextFormField=collection.GetFormField<TextFormField>(formField.Name);System.Console.WriteLine("TextFormField detected, name: {0}, value: {1}",formField.Name,textFormField.Value);break;caseFormFieldType.CheckBox:CheckBoxFormcheckBoxFormField=collection.GetFormField<CheckBoxForm>(formField.Name);System.Console.WriteLine("CheckBoxForm detected, name: {0}, value: {1}",formField.Name,checkBoxFormField.Value);break;caseFormFieldType.Date:DateFormFielddateFormField=collection.GetFormField<DateFormField>(formField.Name);System.Console.WriteLine("DateFormField detected, name: {0}, value: {1}",formField.Name,dateFormField.Value);break;caseFormFieldType.Number:NumberFormFieldnumberFormField=collection.GetFormField<NumberFormField>(formField.Name);System.Console.WriteLine("NumberFormField detected, name: {0}, value: {1}",formField.Name,numberFormField.Value);break;caseFormFieldType.DropDown:DropDownFormFielddropDownFormField=collection.GetFormField<DropDownFormField>(formField.Name);System.Console.WriteLine("DropDownFormField detected, name: {0}, value selected: {1}",formField.Name,dropDownFormField.Value[dropDownFormField.SelectedIndex]);break;}}}}System.Console.WriteLine("ReadFormFieldCollection routine has successfully finished");}}
Conclusion
This guide demonstrates how to work with form fields in Word documents using GroupDocs.Editor for .NET. 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.