Read Word Form Controls in C# and VB.NET

After user has filled-in the form, it is time to read user's filled-in values.

GemBox.Document supports reading form field values from DOC and DOCX formats and tries to convert values to CLR type most appropriate for the given form field type.

Following sample shows how GemBox.Document reads form fields data.

Screenshot

Read Form Screenshot

See the full code below, use RUN EXAMPLE to execute.

Upload your file (Drag files here)

1using System;
2using System.Globalization;
3using System.Text;
4using GemBox.Document;
5using GemBox.Document.Tables;
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 = DocumentModel.Load("FormFilled.docx");
16
17        // Get a snapshot of all form fields in the document.
18        var formFieldsData = document.Content.FormFieldsData;
19        
20        var sb = new StringBuilder();
21
22        // Write header.
23        sb.AppendLine("Document contains following form fields:");
24        sb.AppendFormat(CultureInfo.InvariantCulture, 
25            "{0,-16}|{1,20} = {2,-20}|({3})", 
26            "Type", 
27            '"' + "Name" + '"', 
28            "Value", 
29            "Value type").
30            AppendLine().AppendLine(new string('-', 78));
31
32        // Write type, name, value and value type of each form field in the document.
33        foreach (var formFieldData in formFieldsData)
34            sb.AppendFormat(CultureInfo.InvariantCulture, 
35                "{0,-16}|{1,20} = {2,-20}|({3})", 
36                formFieldData.GetType().Name, 
37                '"' + formFieldData.Name + '"', 
38                formFieldData.Value, 
39                formFieldData.Value != null ? formFieldData.Value.GetType().FullName : "null").
40                AppendLine();
41
42        Console.WriteLine(sb.ToString());
43    }
44}
1Imports System
2Imports System.Globalization
3Imports System.Text
4Imports GemBox.Document
5Imports GemBox.Document.Tables
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 = DocumentModel.Load("FormFilled.docx")
15
16        ' Get a snapshot of all form fields in the document.
17        Dim formFieldsData = document.Content.FormFieldsData
18
19        Dim sb = New StringBuilder()
20
21        ' Write header.
22        sb.AppendLine("Document contains following form fields:")
23        sb.AppendFormat(CultureInfo.InvariantCulture,
24            "{0,-16}|{1,20} = {2,-20}|({3})",
25            "Type",
26            """"c + "Name" + """"c,
27            "Value",
28            "Value type").
29            AppendLine().AppendLine(New String("-"c, 78))
30
31        ' Write type, name, value and value type of each form field in the document.
32        For Each formFieldData In formFieldsData
33            sb.AppendFormat(CultureInfo.InvariantCulture,
34                "{0,-16}|{1,20} = {2,-20}|({3})",
35                formFieldData.GetType().Name,
36                """"c + formFieldData.Name + """"c,
37                formFieldData.Value,
38                If(formFieldData.Value IsNot Nothing, formFieldData.Value.GetType().FullName, "null")).
39                AppendLine()
40        Next
41
42        Console.WriteLine(sb.ToString())
43
44    End Sub
45
46End Module

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