Fields

Field elements in Word documents are dynamic elements, placeholders for dynamic data or changing aspects of the document. For instance, a field can be a page reference, property reference, or date and time value.

Fields are defined by Field.FieldType and optionally the Field.InstructionInlines, which may contain additional field arguments and switches or even other, nested, fields. The field's instructions (the field's code) defines how the Field.ResultInlines (the field's value) should be updated or calculated.

The following example shows how you can create fields of different field types and with various field arguments and switches.

Word document with Field result
Screenshot of Word file with fields result
Word document with Field code
Screenshot of Word file with fields code
using GemBox.Document;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        var document = new DocumentModel();

        var section = new Section(document);
        document.Sections.Add(section);

        // Add '{ AUTHOR }' field.
        section.Blocks.Add(
            new Paragraph(document,
                new Run(document, "Author: "),
                new Field(document, FieldType.Author, null, "Mario at GemBox")));

        // Add '{ DATE }' field.
        section.Blocks.Add(
            new Paragraph(document,
                new Run(document, "Date: "),
                new Field(document, FieldType.Date)));

        // Add '{ DATE \@ "dddd, MMMM dd, yyyy" }' field.
        section.Blocks.Add(
            new Paragraph(document,
                new Run(document, "Date with specified format: "),
                new Field(document, FieldType.Date, "\\@ \"dddd, MMMM dd, yyyy\"")));

        document.Save("Fields.%OutputFileType%");
    }
}
Imports GemBox.Document

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim document As New DocumentModel()

        Dim section As New Section(document)
        document.Sections.Add(section)

        ' Add '{ AUTHOR }' field.
        section.Blocks.Add(
            New Paragraph(document,
                New Run(document, "Author: "),
                New Field(document, FieldType.Author, Nothing, "Mario at GemBox")))

        ' Add '{ DATE }' field.
        section.Blocks.Add(
            New Paragraph(document,
                New Run(document, "Date: "),
                New Field(document, FieldType.Date)))

        ' Add '{ DATE \@ "dddd, MMMM dd, yyyy" }' field.
        section.Blocks.Add(
            New Paragraph(document,
                New Run(document, "Date with specified format: "),
                New Field(document, FieldType.Date, "\@ ""dddd, MMMM dd, yyyy""")))

        document.Save("Fields.%OutputFileType%")

    End Sub
End Module

On the following page you can find a list of field codes in Word. It also contains a description of each field, its syntax, and available switches.

When viewing a Word document in the Word application, by default the field's value is displayed and the field's instructions are hidden. However, by pressing ALT + F9 you can toggle between the field's code and its result.

Interactive form fields are a special type of Field elements which contain Field.FormData objects. With these types of fields you can create, read, and update fillable forms in your Word documents.

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Document yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.