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 example shows how GemBox.Document reads form fields data.

Read Form Screenshot
Upload your file (Drag file here)
using System;
using System.Globalization;
using System.Text;
using GemBox.Document;

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

        DocumentModel document = DocumentModel.Load("%InputFileName%");

        // Get a snapshot of all form fields in the document.
        var formFieldsData = document.Content.FormFieldsData;

        var sb = new StringBuilder();

        // Write header.
        sb.AppendLine("Document contains following form fields:");
        sb.AppendFormat(CultureInfo.InvariantCulture,
            "{0,-16}|{1,20} = {2,-20}|({3})",
            "Type",
            '"' + "Name" + '"',
            "Value",
            "Value type").
            AppendLine().AppendLine(new string('-', 78));

        // Write type, name, value and value type of each form field in the document.
        foreach (var formFieldData in formFieldsData)
            sb.AppendFormat(CultureInfo.InvariantCulture,
                "{0,-16}|{1,20} = {2,-20}|({3})",
                formFieldData.GetType().Name,
                '"' + formFieldData.Name + '"',
                formFieldData.Value,
                formFieldData.Value != null ? formFieldData.Value.GetType().FullName : "null").
                AppendLine();

        Console.WriteLine(sb.ToString());
    }
}
Imports System
Imports System.Globalization
Imports System.Text
Imports GemBox.Document

Module Program

    Sub Main()

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

        Dim document As DocumentModel = DocumentModel.Load("%InputFileName%")

        ' Get a snapshot of all form fields in the document.
        Dim formFieldsData = document.Content.FormFieldsData

        Dim sb = New StringBuilder()

        ' Write header.
        sb.AppendLine("Document contains following form fields:")
        sb.AppendFormat(CultureInfo.InvariantCulture,
            "{0,-16}|{1,20} = {2,-20}|({3})",
            "Type",
            """"c + "Name" + """"c,
            "Value",
            "Value type").
            AppendLine().AppendLine(New String("-"c, 78))

        ' Write type, name, value and value type of each form field in the document.
        For Each formFieldData In formFieldsData
            sb.AppendFormat(CultureInfo.InvariantCulture,
                "{0,-16}|{1,20} = {2,-20}|({3})",
                formFieldData.GetType().Name,
                """"c + formFieldData.Name + """"c,
                formFieldData.Value,
                If(formFieldData.Value IsNot Nothing, formFieldData.Value.GetType().FullName, "null")).
                AppendLine()
        Next

        Console.WriteLine(sb.ToString())

    End Sub

End Module

Check next example or download examples from GitHub.