FieldEnd

FieldEnd class

Represents an end of a Word field in a document.

To learn more, visit the Working with Fields documentation article.

public class FieldEnd : FieldChar

Properties

NameDescription
CustomNodeId { get; set; }Specifies custom node identifier.
virtual Document { get; }Gets the document to which this node belongs.
FieldType { get; }Returns the type of the field.
Font { get; }Provides access to the font formatting of this object.
HasSeparator { get; }Returns true if this field has a separator.
virtual IsComposite { get; }Returns true if this node can contain other nodes.
IsDeleteRevision { get; }Returns true if this object was deleted in Microsoft Word while change tracking was enabled.
IsDirty { get; set; }Gets or sets whether the current result of the field is no longer correct (stale) due to other modifications made to the document.
IsFormatRevision { get; }Returns true if formatting of the object was changed in Microsoft Word while change tracking was enabled.
IsInsertRevision { get; }Returns true if this object was inserted in Microsoft Word while change tracking was enabled.
IsLocked { get; set; }Gets or sets whether the parent field is locked (should not recalculate its result).
IsMoveFromRevision { get; }Returns true if this object was moved (deleted) in Microsoft Word while change tracking was enabled.
IsMoveToRevision { get; }Returns true if this object was moved (inserted) in Microsoft Word while change tracking was enabled.
NextSibling { get; }Gets the node immediately following this node.
override NodeType { get; }Returns FieldEnd.
ParentNode { get; }Gets the immediate parent of this node.
ParentParagraph { get; }Retrieves the parent Paragraph of this node.
PreviousSibling { get; }Gets the node immediately preceding this node.
Range { get; }Returns a Range object that represents the portion of a document that is contained in this node.

Methods

NameDescription
override Accept(DocumentVisitor)Accepts a visitor.
Clone(bool)Creates a duplicate of the node.
GetAncestor(NodeType)Gets the first ancestor of the specified NodeType.
GetAncestor(Type)Gets the first ancestor of the specified object type.
GetField()Returns a field for the field char.
override GetText()Gets the special character that this node represents.
NextPreOrder(Node)Gets next node according to the pre-order tree traversal algorithm.
PreviousPreOrder(Node)Gets the previous node according to the pre-order tree traversal algorithm.
Remove()Removes itself from the parent.
ToString(SaveFormat)Exports the content of the node into a string in the specified format.
ToString(SaveOptions)Exports the content of the node into a string using the specified save options.

Remarks

FieldEnd is an inline-level node and represented by the FieldEndChar control character in the document.

FieldEnd can only be a child of Paragraph.

A complete field in a Microsoft Word document is a complex structure consisting of a field start character, field code, field separator character, field result and field end character. Some fields only have field start, field code and field end.

To easily insert a new field into a document, use the InsertField method.

Examples

Shows how to work with a collection of fields.

public void FieldCollection()
{
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    builder.InsertField(" DATE \\@ \"dddd, d MMMM yyyy\" ");
    builder.InsertField(" TIME ");
    builder.InsertField(" REVNUM ");
    builder.InsertField(" AUTHOR  \"John Doe\" ");
    builder.InsertField(" SUBJECT \"My Subject\" ");
    builder.InsertField(" QUOTE \"Hello world!\" ");
    doc.UpdateFields();

    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());
}

/// <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