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
Upload your file (Drag file here)
using System;
using System.Text;
using GemBox.Document;

class Program
    static void Main()
        // If using Professional version, put your serial key below.

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

        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.Text
Imports GemBox.Document

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.

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

        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

Check next example or download examples from GitHub.