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 sample 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)

1using System;
2using System.Linq;
3using System.Text;
4using GemBox.Document;
5using GemBox.Document.Tables;
6using System.Text.RegularExpressions;
7
8class Sample
9{
10    [STAThread]
11    static void Main(string[] args)
12    {
13        // If using Professional version, put your serial key below.
14        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
15
16        DocumentModel document = DocumentModel.Load("Reading.doc");
17
18        StringBuilder sb = new StringBuilder();
19
20        foreach (Paragraph paragraph in document.GetChildElements(true, ElementType.Paragraph))
21        {
22            foreach (Run run in paragraph.GetChildElements(true, ElementType.Run))
23            {
24                bool isBold = run.CharacterFormat.Bold;
25                string text = run.Text;
26
27                sb.AppendFormat("{0}{1}{2}", isBold ? "<b>" : "", text, isBold ? "</b>" : "");
28            }
29            sb.AppendLine();
30        }
31
32        Console.WriteLine(sb.ToString());
33    }
34}
1Imports System
2Imports System.Linq
3Imports System.Text
4Imports GemBox.Document
5Imports GemBox.Document.Tables
6Imports System.Text.RegularExpressions
7
8Module Samples
9
10    Sub Main()
11
12        ' If using Professional version, put your serial key below.
13        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
14
15        Dim document As DocumentModel = DocumentModel.Load("Reading.doc")
16
17        Dim sb As New StringBuilder()
18
19        For Each paragraph As Paragraph In document.GetChildElements(True, ElementType.Paragraph)
20            For Each run As Run In paragraph.GetChildElements(True, ElementType.Run)
21                Dim isBold As Boolean = run.CharacterFormat.Bold
22                Dim text As String = run.Text
23
24                sb.AppendFormat("{0}{1}{2}", If(isBold, "<b>", ""), text, If(isBold, "</b>", ""))
25            Next
26            sb.AppendLine()
27        Next
28
29        Console.WriteLine(sb.ToString())
30
31    End Sub
32
33End Module

Check next sample or find out more about GemBox.Document and GemBox Software.