Reading a Microsoft Word Document (.docx) using Telerik

This post explains how to read content from text form .docx files using Progress Telerik

Introduction
 
This is a sample class that reads Microsoft Word 2007 - 365 documents and returns the content.
 
Microsoft introduced a new document format with contained data in Office 2007. This means that .docx files are like a zip file.
The code below shows how to read the text from .docx documents. 
 
This technique uses Progress Telerik.
  1. using System.IO;  
  2. using Telerik.WinForms.Documents.FormatProviders.OpenXml.Docx;  
  3. using Telerik.WinForms.Documents.FormatProviders.Txt;  
  4.   
  5. namespace Telerik.WinForms.Documents  
  6. {  
  7.     public static class  
  8.     {  
  9.         public static string ReadDocxContent(string file)  
  10.         {  
  11.             var docxFormatProvider = new DocxFormatProvider();  
  12.             using var input = File.OpenRead(file);  
  13.             var document = docxFormatProvider.Import(input);  
  14.             var txtFormatProvider = new TxtFormatProvider();  
  15.             return txtFormatProvider.Export(document);  
  16.         }  
  17.     }  
  18. }  
Just pass a document file with a full path and the function returns its content. 
  1. var txt = ReadDocx.ReadDocxContent(@".\desktop\document1.docx");  
This code works with .Net Framework 4.x too, with a small refactoring:
  1. using System.IO;  
  2. using Telerik.WinForms.Documents.FormatProviders.OpenXml.Docx;  
  3. using Telerik.WinForms.Documents.FormatProviders.Txt;  
  4.   
  5. namespace Telerik.WinForms.Documents  
  6. {  
  7.     public static class ReadDocx  
  8.     {  
  9.         public static string ReadDocxContent(string file)  
  10.         {  
  11.             var docxFormatProvider = new DocxFormatProvider();  
  12.             using (var input = File.OpenRead(file))  
  13.             {  
  14.                 var document = docxFormatProvider.Import(input);  
  15.                 var txtFormatProvider = new TxtFormatProvider();  
  16.                 return txtFormatProvider.Export(document);  
  17.             }  
  18.         }  
  19.     }  
Happy coding!