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.

Read Word file in C# and VB.NET

GemBox.Document supports reading Word documents in C# and VB.NET from a file path or a stream. The document file format is specified through LoadOptions derived classes.

The following example demonstrates how to read a Word document from a file path, iterate over all Paragraph and Run elements, and output text for all Run elements with font weight information to the Console output.

Screenshot
Reading Screenshot

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

Upload your file(Drag files here)

Download a sample file

using System;
using System.Linq;
using System.Text;
using GemBox.Document;
using GemBox.Document.Tables;
using System.Text.RegularExpressions;

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

        DocumentModel document = DocumentModel.Load("Reading.doc");

        StringBuilder sb = new StringBuilder();

        foreach (Paragraph paragraph in document.GetChildElements(true, ElementType.Paragraph))
        {
            foreach (Run run in paragraph.GetChildElements(true, ElementType.Run))
            {
                bool isBold = run.CharacterFormat.Bold;
                string text = run.Text;

                sb.AppendFormat("{0}{1}{2}", isBold ? "<b>" : "", text, isBold ? "</b>" : "");
            }
            sb.AppendLine();
        }

        Console.WriteLine(sb.ToString());
    }
}
Imports System
Imports System.Linq
Imports System.Text
Imports GemBox.Document
Imports GemBox.Document.Tables
Imports System.Text.RegularExpressions

Module Samples

    Sub Main()

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

        Dim document As DocumentModel = DocumentModel.Load("Reading.doc")

        Dim sb As New StringBuilder()

        For Each paragraph As Paragraph In document.GetChildElements(True, ElementType.Paragraph)
            For Each run As Run In paragraph.GetChildElements(True, ElementType.Run)
                Dim isBold As Boolean = run.CharacterFormat.Bold
                Dim text As String = run.Text

                sb.AppendFormat("{0}{1}{2}", If(isBold, "<b>", ""), text, If(isBold, "</b>", ""))
            Next
            sb.AppendLine()
        Next

        Console.WriteLine(sb.ToString())

    End Sub

End Module

Check next sample.