FieldRemove Method
Removes the field from the document. Returns a node right after the field. If the field's end is the last child of its parent node, returns its parent paragraph. If the field is already removed, returns null.

Namespace: Aspose.Words.Fields
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 19.12
Syntax
public Node Remove()

Return Value

Type: Node
Examples
Inserts a field into a document using DocumentBuilder and 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.
dateField.Update();

// 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.
Console.WriteLine("FieldResult: {0}", dateField.Result);

// Display the field code which defines the behavior of the field. This can been seen in Microsoft Word by pressing ALT+F9.
Console.WriteLine("FieldCode: {0}", dateField.GetFieldCode());

// The field type defines what type of field in the Document this is. In this case the type is "FieldDate" 
Console.WriteLine("FieldType: {0}", 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.
dateField.Remove();
Examples
Shows how to work with a document's field collection.
public void FieldCollection()
{
    // Open a document that has fields
    Document doc = new Document(MyDir + "Document.ContainsFields.docx");

    // Get the collection that contains all the fields in a document
    FieldCollection fields = doc.Range.Fields;
    Assert.AreEqual(6, fields.Count);

    // Iterate over the field collection and print contents and type of every field using a custom visitor implementation
    FieldVisitor fieldVisitor = new FieldVisitor();

    using (IEnumerator<Field> fieldEnumerator = fields.GetEnumerator())
    {
        while (fieldEnumerator.MoveNext())
        {
            if (fieldEnumerator.Current != null)
            {
                fieldEnumerator.Current.Start.Accept(fieldVisitor);
                fieldEnumerator.Current.Separator?.Accept(fieldVisitor);
                fieldEnumerator.Current.End.Accept(fieldVisitor);
            }
            else
            {
                Console.WriteLine("There are no fields in the document.");
            }
        }
    }

    Console.WriteLine(fieldVisitor.GetText());

    // Get a field to remove itself
    fields[0].Remove();
    Assert.AreEqual(5, fields.Count);

    // Remove a field by reference
    Field lastField = fields[3];
    fields.Remove(lastField);
    Assert.AreEqual(4, fields.Count);

    // Remove a field by index
    fields.RemoveAt(2);
    Assert.AreEqual(3, fields.Count);

    // Remove all fields from the document
    fields.Clear();
    Assert.AreEqual(0, fields.Count);
}

/// <summary>
/// Document visitor implementation that prints field info
/// </summary>
public class FieldVisitor : DocumentVisitor
{
    public FieldVisitor()
    {
        mBuilder = new StringBuilder();
    }

    /// <summary>
    /// Gets the plain text of the document that was accumulated by the visitor.
    /// </summary>
    public String GetText()
    {
        return mBuilder.ToString();
    }

    /// <summary>
    /// Called when a FieldStart node is encountered in the document.
    /// </summary>
    public override VisitorAction VisitFieldStart(FieldStart fieldStart)
    {
        mBuilder.AppendLine("Found field: " + fieldStart.FieldType);
        mBuilder.AppendLine("\tField code: " + fieldStart.GetField().GetFieldCode());
        mBuilder.AppendLine("\tDisplayed as: " + fieldStart.GetField().Result);

        return VisitorAction.Continue;
    }

    /// <summary>
    /// Called when a FieldSeparator node is encountered in the document.
    /// </summary>
    public override VisitorAction VisitFieldSeparator(FieldSeparator fieldSeparator)
    {
        mBuilder.AppendLine("\tFound separator: " + fieldSeparator.GetText());

        return VisitorAction.Continue;
    }

    /// <summary>
    /// Called when a FieldEnd node is encountered in the document.
    /// </summary>
    public override VisitorAction VisitFieldEnd(FieldEnd fieldEnd)
    {
        mBuilder.AppendLine("End of field: " + fieldEnd.FieldType);

        return VisitorAction.Continue;
    }

    private readonly StringBuilder mBuilder;
}
See Also