DocumentNormalizeFieldTypes Method

Changes field type values FieldType of FieldStart, FieldSeparator, FieldEnd in the whole document so that they correspond to the field types contained in the field codes.

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.3
Syntax
public void NormalizeFieldTypes()
Remarks

Use this method after document changes that affect field types.

To change field type values in a specific part of the document use NormalizeFieldTypes.

Examples
Shows how to get the keep a field's type up to date with its field code.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// We'll add a date field
Field field = builder.InsertField("DATE", null);

// The FieldDate field type corresponds to the "DATE" field so our field's type property gets automatically set to it
Assert.AreEqual(FieldType.FieldDate, field.Type);
Assert.AreEqual(1, doc.Range.Fields.Count);

// We can manually access the content of the field we added and change it
Run fieldText = (Run) doc.FirstSection.Body.FirstParagraph.GetChildNodes(NodeType.Run, true)[0];
Assert.AreEqual("DATE", fieldText.Text);
fieldText.Text = "PAGE";

// We changed the text to "PAGE" but the field's type property did not update accordingly
Assert.AreEqual("PAGE", fieldText.GetText());
Assert.AreNotEqual(FieldType.FieldPage, field.Type);

// The type of the field as well as its components is still "FieldDate"
Assert.AreEqual(FieldType.FieldDate, field.Type);
Assert.AreEqual(FieldType.FieldDate, field.Start.FieldType);
Assert.AreEqual(FieldType.FieldDate, field.Separator.FieldType);
Assert.AreEqual(FieldType.FieldDate, field.End.FieldType);

doc.NormalizeFieldTypes();

// After running this method the type changes everywhere to "FieldPage", which matches the text "PAGE"
Assert.AreEqual(FieldType.FieldPage, field.Type);
Assert.AreEqual(FieldType.FieldPage, field.Start.FieldType);
Assert.AreEqual(FieldType.FieldPage, field.Separator.FieldType);
Assert.AreEqual(FieldType.FieldPage, field.End.FieldType);
See Also