GemBox.Document is a .NET component that enables you to read, write, edit, convert, and print document files from your .NET applications using one simple API.

Fields

Fields are placeholders, such as page reference numbers, replaced by their corresponding values when the document is rendered for display or print.

Following example demonstrates how to create fields of different field types and with various field arguments and switches.

Screenshot
Fields Screenshot

See the full code below, use Run Example to execute.

using System;
using System.IO;
using System.Linq;
using GemBox.Document;
using GemBox.Document.Drawing;

class Sample
{
    [STAThread]
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        DocumentModel document = new DocumentModel();

        document.Sections.Add(
            new Section(document,
                new Paragraph(document, "Press Alt + F9 to see field codes!"),
                new Paragraph(document,
                    new Run(document, "Date: "),
                    // { DATE }
                    new Field(document, FieldType.Date),
                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
                    new Run(document, "Date (formatted): "),
                    // { DATE \@ "dddd, MMMM dd, yyyy"  \* MERGEFORMAT }
                    new Field(document, FieldType.Date, "\\@ \"dddd, MMMM dd, yyyy\"  \\* MERGEFORMAT"),
                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
                    new Run(document, "Date & Time (formatted): "),
                    // { DATE  \@ "M/d/yyyy h:mm:ss am/pm"  \* MERGEFORMAT }
                    new Field(document, FieldType.Date, " \\@ \"M/d/yyyy h:mm:ss am/pm\"  \\* MERGEFORMAT"))));

        document.Save("Fields.docx");
    }
}
Imports System
Imports System.IO
Imports System.Linq
Imports GemBox.Document
Imports GemBox.Document.Drawing

Module Samples

    Sub Main()

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

        Dim document As DocumentModel = New DocumentModel

        ' 1st field: { DATE }
        ' 2nd field: { DATE \@ "dddd, MMMM dd, yyyy"  \* MERGEFORMAT }
        ' 3rd field: { DATE  \@ "M/d/yyyy h:mm:ss am/pm"  \* MERGEFORMAT }
        document.Sections.Add(
            New Section(document,
                New Paragraph(document, "Press Alt + F9 to see field codes!"),
                New Paragraph(document,
                    New Run(document, "Date: "),
                    New Field(document, FieldType.[Date]),
                    New SpecialCharacter(document, SpecialCharacterType.LineBreak),
                    New Run(document, "Date (formatted): "),
                    New Field(document, FieldType.Date, "\@ ""dddd, MMMM dd, yyyy""  \* MERGEFORMAT"),
                    New SpecialCharacter(document, SpecialCharacterType.LineBreak),
                    New Run(document, "Date & Time (formatted): "),
                    New Field(document, FieldType.Date, " \@ ""M/d/yyyy h:mm:ss am/pm""  \* MERGEFORMAT"))))

        document.Save("Fields.docx")

    End Sub

End Module

Check next sample.