FormField class

FormField class

Represents a single form field. To learn more, visit the Working with Form Fields documentation article.

Remarks

Microsoft Word provides the following form fields: checkbox, text input and dropdown (combobox).

FormField is an inline-node and can only be a child of Paragraph.

FormField is represented in a document by a special character and positioned as a character within a line of text.

A complete form field in a Word document is a complex structure represented by several nodes: field start, field code such as FORMTEXT, form field data, field separator, field result, field end and a bookmark. To programmatically create form fields in a Word document use DocumentBuilder.insert_check_box(), DocumentBuilder.insert_text_input() and DocumentBuilder.insert_combo_box() which make sure all of the form field nodes are created in a correct order and in a suitable state.

Inheritance: FormFieldSpecialCharInlineNode

Properties

NameDescription
calculate_on_exitTrue if references to the specified form field are automatically updated whenever the field is exited.
check_box_sizeGets or sets the size of the checkbox in points. Has effect only when FormField.is_check_box_exact_size is True.
checkedGets or sets the checked status of the check box form field. Default value for this property is False.
custom_node_idSpecifies custom node identifier.
(Inherited from Node)
defaultGets or sets the default value of the check box form field. Default value for this property is False.
documentGets the document to which this node belongs.
(Inherited from Node)
drop_down_itemsProvides access to the items of a dropdown form field.
drop_down_selected_indexGets or sets the index specifying the currently selected item in a dropdown form field.
enabledTrue if a form field is enabled.
entry_macroReturns or sets an entry macro name for the form field.
exit_macroReturns or sets an exit macro name for the form field.
fontProvides access to the font formatting of this object.
(Inherited from Inline)
help_textReturns or sets the text that’s displayed in a message box when the form field has the focus and the user presses F1.
is_check_box_exact_sizeGets or sets the boolean value that indicates whether the size of the textbox is automatic or specified explicitly.
is_compositeReturns True if this node can contain other nodes.
(Inherited from Node)
is_delete_revisionReturns true if this object was deleted in Microsoft Word while change tracking was enabled.
(Inherited from Inline)
is_format_revisionReturns true if formatting of the object was changed in Microsoft Word while change tracking was enabled.
(Inherited from Inline)
is_insert_revisionReturns true if this object was inserted in Microsoft Word while change tracking was enabled.
(Inherited from Inline)
is_move_from_revisionReturns True if this object was moved (deleted) in Microsoft Word while change tracking was enabled.
(Inherited from Inline)
is_move_to_revisionReturns True if this object was moved (inserted) in Microsoft Word while change tracking was enabled.
(Inherited from Inline)
max_lengthMaximum length for the text field. Zero when the length is not limited.
nameGets or sets the form field name.
next_siblingGets the node immediately following this node.
(Inherited from Node)
node_typeReturns NodeType.FORM_FIELD.
own_helpSpecifies the source of the text that’s displayed in a message box when a form field has the focus and the user presses F1.
own_statusSpecifies the source of the text that’s displayed in the status bar when a form field has the focus.
parent_nodeGets the immediate parent of this node.
(Inherited from Node)
parent_paragraphRetrieves the parent Paragraph of this node.
(Inherited from Inline)
previous_siblingGets the node immediately preceding this node.
(Inherited from Node)
rangeReturns a Range object that represents the portion of a document that is contained in this node.
(Inherited from Node)
resultGets or sets a string that represents the result of this form field.
status_textReturns or sets the text that’s displayed in the status bar when a form field has the focus.
text_input_defaultGets or sets the default string or a calculation expression of a text form field.
text_input_formatReturns or sets the text formatting for a text form field.
text_input_typeGets or sets the type of a text form field.
typeReturns the form field type.

Methods

NameDescription
accept(visitor)Accepts a visitor.
clone(is_clone_children)Creates a duplicate of the node.
(Inherited from Node)
get_ancestor(ancestor_type)Gets the first ancestor of the specified object type.
(Inherited from Node)
get_ancestor(ancestor_type)Gets the first ancestor of the specified NodeType.
(Inherited from Node)
get_text()Gets the text of this node and of all its children.
(Inherited from Node)
next_pre_order(root_node)Gets next node according to the pre-order tree traversal algorithm.
(Inherited from Node)
node_type_to_string(node_type)A utility method that converts a node type enum value into a user friendly string.
(Inherited from Node)
previous_pre_order(root_node)Gets the previous node according to the pre-order tree traversal algorithm.
(Inherited from Node)
remove()Removes itself from the parent.
(Inherited from Node)
remove_field()Removes the complete form field, not just the form field special character.
set_text_input_value(new_value)Applies the text format specified in FormField.text_input_format and stores the value in FormField.result.
to_string(save_format)Exports the content of the node into a string in the specified format.
(Inherited from Node)
to_string(save_options)Exports the content of the node into a string using the specified save options.
(Inherited from Node)

Examples

Shows how to insert a combo box.

doc = aw.Document()
builder = aw.DocumentBuilder(doc)

builder.write("Please select a fruit: ")

# Insert a combo box which will allow a user to choose an option from a collection of strings.
combo_box = builder.insert_combo_box("MyComboBox", ["Apple", "Banana", "Cherry"], 0)

self.assertEqual("MyComboBox", combo_box.name)
self.assertEqual(aw.fields.FieldType.FIELD_FORM_DROP_DOWN, combo_box.type)
self.assertEqual("Apple", combo_box.result)

# The form field will appear in the form of a "select" html tag.
doc.save(ARTIFACTS_DIR + "FormFields.create.html")

Shows how to formatting the entire FormField, including the field value.

doc = aw.Document(MY_DIR + "Form fields.docx")

form_field = doc.range.form_fields[0]
form_field.font.bold = True
form_field.font.size = 24
form_field.font.color = drawing.Color.red

form_field.result = "Aspose.FormField"

doc = DocumentHelper.save_open(doc)

form_field_run = doc.first_section.body.first_paragraph.runs[1]

self.assertEqual("Aspose.FormField", form_field_run.text)
self.assertEqual(True, form_field_run.font.bold)
self.assertEqual(24, form_field_run.font.size)
self.assertEqual(drawing.Color.red.to_argb(), form_field_run.font.color.to_argb())

See Also