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: 20.7.0


public string GetFieldCode()

Return Value

Type: String


Shows how to get text between field start and field separator (or field end if there is no separator).
// Open a document which contains a MERGEFIELD inside an IF field
Document doc = new Document(MyDir + "Nested fields.docx");

// Get the outer IF field and print its full field code
FieldIf fieldIf = (FieldIf)doc.Range.Fields[0];
Console.WriteLine($"Full field code including child fields:\n\t{fieldIf.GetFieldCode()}");

// All inner nested fields are printed by default
Assert.AreEqual(fieldIf.GetFieldCode(), fieldIf.GetFieldCode(true));

// Print the field code again but this time without the inner MERGEFIELD
Console.WriteLine($"Field code with nested fields omitted:\n\t{fieldIf.GetFieldCode(false)}");
Shows how to insert a field into a document by FieldCode.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Insert a simple Date field into the document
// When we insert a field through the DocumentBuilder class we can get the
// special Field object which contains information about the field
Field dateField = builder.InsertField(@"DATE \* MERGEFORMAT");

// Update this particular field in the document so we can get the FieldResult

// Display some information from this field
// The field result is where the last evaluated value is stored. This is what is displayed in the document
// When field codes are not showing
Assert.AreEqual(DateTime.Today, DateTime.Parse(dateField.Result));

// Display the field code which defines the behavior of the field. This can been seen in Microsoft Word by pressing ALT+F9
Assert.AreEqual(@"DATE \* MERGEFORMAT", dateField.GetFieldCode());

// The field type defines what type of field in the Document this is. In this case the type is "FieldDate" 
Assert.AreEqual(FieldType.FieldDate, dateField.Type);

// Finally let's completely remove the field from the document. This can easily be done by invoking the Remove method on the object

See Also