DocumentNormalizeFieldTypes Method |
Namespace: Aspose.Words
Use this method after document changes that affect field types.
To change field type values in a specific part of the document use NormalizeFieldTypes.
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);