FieldGetFieldCode Method

Returns text between field start and field separator (or field end if there is no separator). Both field code and field result of child fields are included.

Namespace:  Aspose.Words.Fields
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 21.1.0

ExpandedSyntax

public string GetFieldCode()

Return Value

Type: String

ExpandedExamples

Shows how to insert a field into a document using a field code.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Field field = builder.InsertField("DATE \\@ \"dddd, MMMM dd, yyyy\"");

Assert.AreEqual(FieldType.FieldDate, field.Type);
Assert.AreEqual("DATE \\@ \"dddd, MMMM dd, yyyy\"", field.GetFieldCode());

// This overload of the InsertField method automatically updates inserted fields.
Assert.That(DateTime.Parse(field.Result), Is.EqualTo(DateTime.Today).Within(1).Days);
Shows how to get a field's field code.
// Open a document which contains a MERGEFIELD inside an IF field.
Document doc = new Document(MyDir + "Nested fields.docx");
FieldIf fieldIf = (FieldIf)doc.Range.Fields[0];

// There are two ways of getting a field's field code:
// 1 -  Omit its inner fields:
Assert.AreEqual(" IF  > 0 \" (surplus of ) \" \"\" ", fieldIf.GetFieldCode(false));

// 2 -  Include its inner fields:
Assert.AreEqual($" IF \u0013 MERGEFIELD NetIncome \u0014\u0015 > 0 \" (surplus of \u0013 MERGEFIELD  NetIncome \\f $ \u0014\u0015) \" \"\" ",
    fieldIf.GetFieldCode(true));

// By default, the GetFieldCode method displays inner fields.
Assert.AreEqual(fieldIf.GetFieldCode(), fieldIf.GetFieldCode(true));

ExpandedSee Also