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.

Reading Screenshot

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

Upload your file(Drag files here)

Download an input file

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

class Sample
    static void Main(string[] args)
        // If using Professional version, put your serial key below.

        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>" : "");

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.

        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>", ""))


    End Sub

End Module