Fields

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

Following sample 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.


1using System;
2using System.IO;
3using System.Linq;
4using GemBox.Document;
5using GemBox.Document.Drawing;
6
7class Sample
8{
9    [STAThread]
10    static void Main(string[] args)
11    {
12        // If using Professional version, put your serial key below.
13        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
14
15        DocumentModel document = new DocumentModel();
16
17        document.Sections.Add(
18            new Section(document,
19                new Paragraph(document, "Press Alt + F9 to see field codes!"),
20                new Paragraph(document,
21                    new Run(document, "Date: "),
22                    // { DATE }
23                    new Field(document, FieldType.Date),
24                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
25                    new Run(document, "Date (formatted): "),
26                    // { DATE \@ "dddd, MMMM dd, yyyy"  \* MERGEFORMAT }
27                    new Field(document, FieldType.Date, "\\@ \"dddd, MMMM dd, yyyy\"  \\* MERGEFORMAT"),
28                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
29                    new Run(document, "Date & Time (formatted): "),
30                    // { DATE  \@ "M/d/yyyy h:mm:ss am/pm"  \* MERGEFORMAT }
31                    new Field(document, FieldType.Date, " \\@ \"M/d/yyyy h:mm:ss am/pm\"  \\* MERGEFORMAT"))));
32
33        document.Save("Fields.docx");
34    }
35}
1Imports System
2Imports System.IO
3Imports System.Linq
4Imports GemBox.Document
5Imports GemBox.Document.Drawing
6
7Module Samples
8
9    Sub Main()
10
11        ' If using Professional version, put your serial key below.
12        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
13
14        Dim document As DocumentModel = New DocumentModel
15
16        ' 1st field: { DATE }
17        ' 2nd field: { DATE \@ "dddd, MMMM dd, yyyy"  \* MERGEFORMAT }
18        ' 3rd field: { DATE  \@ "M/d/yyyy h:mm:ss am/pm"  \* MERGEFORMAT }
19        document.Sections.Add(
20            New Section(document,
21                New Paragraph(document, "Press Alt + F9 to see field codes!"),
22                New Paragraph(document,
23                    New Run(document, "Date: "),
24                    New Field(document, FieldType.[Date]),
25                    New SpecialCharacter(document, SpecialCharacterType.LineBreak),
26                    New Run(document, "Date (formatted): "),
27                    New Field(document, FieldType.Date, "\@ ""dddd, MMMM dd, yyyy""  \* MERGEFORMAT"),
28                    New SpecialCharacter(document, SpecialCharacterType.LineBreak),
29                    New Run(document, "Date & Time (formatted): "),
30                    New Field(document, FieldType.Date, " \@ ""M/d/yyyy h:mm:ss am/pm""  \* MERGEFORMAT"))))
31
32        document.Save("Fields.docx")
33
34    End Sub
35
36End Module

Check next sample or find out more about GemBox.Document and GemBox Software.