Working with Form Fields

This article demonstrates how to load and read form fields in a Word document using GroupDocs.Editor for Python via .NET. We will go through the process of opening a document, retrieving the form field manager, and inspecting the form fields it contains.

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. Use it to read the form_field_collection and to check whether the document contains invalid form fields.

    with open("form-fields.docx", "rb") as stream:
        with Editor(stream, WordProcessingLoadOptions()) as editor:
            field_manager = editor.form_field_manager
            collection = field_manager.form_field_collection
    
            print("Has invalid form fields:", field_manager.has_invalid_form_fields())
            print("Form fields count:", len(collection))
    
  3. Process the form fields

    The form_field_collection can be iterated to inspect each form field. Every field exposes common properties such as name and type. The snippet below illustrates how to enumerate the collection and react to a field’s type. Treat the per-type access details as illustrative — adjust them to the exact members exposed by your build.

    # Illustrative: iterate the collection and inspect each form field
    for form_field in collection:
        print("name:", form_field.name, "type:", form_field.type)
    

Complete code example

Below is the complete runnable example. It opens the document, retrieves the form field manager, and reports whether the document has invalid form fields together with the number of form fields it contains.

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

def working_with_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

            # Read the form field collection
            collection = field_manager.form_field_collection

            print("Has invalid form fields:", field_manager.has_invalid_form_fields())
            print("Form fields count:", len(collection))

            # Iterate the collection and inspect each form field
            for form_field in collection:
                print("name:", form_field.name, "type:", form_field.type)

if __name__ == "__main__":
    working_with_form_fields()

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

Has invalid form fields: True
Form fields count: 31
name: Text1 type: 0
name: Check1 type: 5
name: Check2 type: 5
name: Check3 type: 5
name: Check4 type: 5
name: Check5 type: 5
name: Check6 type: 5
name: Text7 type: 0
[TRUNCATED]

Download full output

Conclusion

This guide demonstrates how to work with form fields in Word documents using GroupDocs.Editor for Python via .NET. By following these steps, you can load a document, retrieve the form field manager, and inspect the form fields it contains. This functionality is essential for applications that require manipulation and extraction of form field data from documents.