This article demonstrates how to fix invalid form fields in a Word document using GroupDocs.Editor for .NET. The example guides you through loading a document, identifying and fixing invalid form fields, and saving the updated document.
Step-by-Step Guide
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.
Use the Editor class to load the document with the specified load options.
using(Editoreditor=newEditor(fs,loadOptions)){// Further code will be placed here}
Retrieve and Fix Invalid Form Fields
Obtain the FormFieldManager instance from the Editor and read the FormFieldCollection. Attempt to auto-fix invalid form fields, then check if any invalid form fields remain. If so, generate unique names for the invalid fields and fix them.
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.Console.WriteLine("FixInvalidFormFieldCollectionAndSave routine has successfully finished");
Complete Example Code
Below is the complete example code demonstrating the entire process:
/// <summary>/// This example demonstrates how to fix invalid form fields in a collection and save the document using GroupDocs.Editor for .NET./// </summary>internalstaticclassFixInvalidFormFieldCollectionAndSave{/// <summary>/// Runs the example to demonstrate loading, fixing invalid form fields, and saving 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. If the input document is password-protected, specify the 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 into 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;// 4.3. Try to auto-fix invalid form fieldsfieldManager.FixInvalidFormFieldNames(newInvalidFormField[0]);collection=fieldManager.FormFieldCollection;boolhasInvalidFormFields=fieldManager.HasInvalidFormFields();System.Console.WriteLine("FormFieldCollection contains invalid items: {0}",hasInvalidFormFields);// 4.4. Try to fix invalid form fields// 4.4.1. Get all invalid form field namesvarinvalidFormFields=fieldManager.GetInvalidFormFieldNames();// 4.4.2. Create unique names for invalid fieldsforeach(varinvalidItemininvalidFormFields){invalidItem.FixedName=string.Format("{0}_{1}",invalidItem.Name,Guid.NewGuid());}// 4.4.3. Fix invalid fieldsfieldManager.FixInvalidFormFieldNames(invalidFormFields);collection=fieldManager.FormFieldCollection;// 5. Create document save optionsWordProcessingFormatsdocFormat=WordProcessingFormats.Docx;WordProcessingSaveOptionssaveOptions=newWordProcessingSaveOptions(docFormat);// 5.1. If the document is large and causes OutOfMemoryException, set the memory optimization optionsaveOptions.OptimizeMemoryUsage=true;// 5.2. Protect the document from writing (allow only form fields) with a passwordsaveOptions.Protection=newWordProcessingProtection(WordProcessingProtectionType.AllowOnlyFormFields,"write_password");// 6. Save the document// 6.1. Prepare a stream for savingusing(MemoryStreamoutputStream=newMemoryStream()){// 6.2. Save the documenteditor.Save(outputStream,saveOptions);}}}System.Console.WriteLine("FixInvalidFormFieldCollectionAndSave 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.