Fixing Invalid Form Fields

This article demonstrates how to fix invalid form fields in a Word document using GroupDocs.Editor for Python via .NET. It guides you through loading a document, identifying invalid form fields, and fixing them.

Step-by-Step Guide

  1. Load the document into the Editor instance

    Open the document as a binary stream and pass it to the Editor class together with WordProcessingLoadOptions. If the document is password-protected, specify the password through the load options.

    from groupdocs.editor import Editor
    from groupdocs.editor.options import WordProcessingLoadOptions
    
    with open("form-fields.docx", "rb") as stream:
        with Editor(stream, WordProcessingLoadOptions()) as editor:
            # Further code will be placed here
            pass
    
  2. Retrieve the FormFieldManager

    Obtain the FormFieldManager instance from the form_field_manager property of the Editor class.

    with open("form-fields.docx", "rb") as stream:
        with Editor(stream, WordProcessingLoadOptions()) as editor:
            field_manager = editor.form_field_manager
    
  3. Detect invalid form fields

    Check whether the document contains invalid form fields with has_invalid_form_fields(), and obtain their names with get_invalid_form_field_names().

    has_invalid = field_manager.has_invalid_form_fields()
    print("FormFieldCollection contains invalid items:", has_invalid)
    
    invalid_names = list(field_manager.get_invalid_form_field_names())
    print("Invalid form field names:", invalid_names)
    
  4. Fix the invalid form fields

    The invalid form fields can be repaired with fix_invalid_form_field_names(...). The snippet below is illustrative — generate a unique replacement name for each invalid field and pass them to the method. Adjust the per-item access details to the exact members exposed by your build.

    # Illustrative: assign unique fixed names and repair the invalid fields
    import uuid
    
    invalid_form_fields = field_manager.get_invalid_form_field_names()
    for invalid_item in invalid_form_fields:
        invalid_item.fixed_name = "{0}_{1}".format(invalid_item.name, uuid.uuid4())
    field_manager.fix_invalid_form_field_names(invalid_form_fields)
    

Complete code example

Below is the complete runnable example. It opens the document, retrieves the form field manager, and reports whether it has invalid form fields together with their names. The repair steps shown above are illustrative; the runnable example performs only safe, documented calls.

import os
from groupdocs.editor import Editor, License
from groupdocs.editor.options import WordProcessingLoadOptions

def fixing_invalid_form_fields():
    # Optionally set a license
    license_path = os.path.abspath("./GroupDocs.Editor.lic")
    if os.path.exists(license_path):
        License().set_license(license_path)

    # Open the document as a stream and load it with WordProcessingLoadOptions
    with open("./form-fields.docx", "rb") as stream:
        with Editor(stream, WordProcessingLoadOptions()) as editor:
            # Read the FormFieldManager instance
            field_manager = editor.form_field_manager

            # Detect invalid form fields
            print("Has invalid form fields:", field_manager.has_invalid_form_fields())
            invalid_form_fields = list(field_manager.get_invalid_form_field_names())
            print("Invalid form fields detected:", len(invalid_form_fields))

if __name__ == "__main__":
    fixing_invalid_form_fields()

form-fields.docx is the sample file used in this example. Click here to download it.

Has invalid form fields: True
Invalid form fields detected: 21

Download full output