Read Word form controls in C# and VB.NET

The following example shows how you can read text, checkbox, and drop-down form fields data using the GemBox.Document library.

Upload your file (Drag file here)
using GemBox.Document;
using System;

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

var document = DocumentModel.Load("FormFilled.docx");

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

Console.WriteLine($" {"Field type",-20} | {"Name",-20} | {"Value",-20} | {"Value type",-20} ");
Console.WriteLine(new string('-', 88));

// Read type, name, value and value type of each form field in the document.
foreach (FormFieldData formFieldData in formFieldsData)
{
Type fieldType = formFieldData.GetType();
string fieldName = formFieldData.Name;
object fieldValue = formFieldData.Value;
Type valueType = fieldValue.GetType();

Console.WriteLine($" {fieldType.Name,-20} | {fieldName,-20} | {fieldValue,-20} | {valueType.FullName,-20} ");
}
Imports GemBox.Document
Imports System

Module Program

Sub Main()

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

Dim document = DocumentModel.Load("FormFilled.docx")

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

Console.WriteLine($" {"Field type",-20} | {"Name",-20} | {"Value",-20} | {"Value type",-20} ")
Console.WriteLine(New String("-"c, 88))

' Read type, name, value and value type of each form field in the document.
For Each formFieldData As FormFieldData In formFieldsData

Dim fieldType As Type = formFieldData.GetType()
Dim fieldName As String = formFieldData.Name
Dim fieldValue As Object = formFieldData.Value
Dim valueType As Type = fieldValue.GetType()

Console.WriteLine($" {fieldType.Name,-20} | {fieldName,-20} | {fieldValue,-20} | {valueType.FullName,-20} ")

Next

End Sub
End Module
Reading a Word document's text, checkbox and drop-down form fields in C# and VB.NET
Screenshot of read form values from a Word file

For convenience, GemBox.Document provides form values as objects of different types. The value type depends on the type of the field and, in the case of a FormText field, on the value of FormTextData.TextType. You can read more about this in the FormFieldData.Value remarks.

Next steps

GemBox.Document is a .NET library that enables you to read, write, edit, convert, and print document files from your .NET applications using one simple API. How about testing it today?