Convert Word to Text Document and Vice Versa

24 Jul 20266 minutes to read

The Essential® DocIO converts a Word document to a Text file and vice versa without Microsoft Word or interop dependencies.

To quickly start converting a Word document to Text and vice versa, please check out this video:

Convert Word to Text

The following code example shows how to convert a Word document into a text file.

NOTE

Refer to the appropriate tabs in the code snippets section: C# [Cross-platform] for ASP.NET Core, Blazor, Xamarin, UWP, .NET MAUI, and WinUI; C# [Windows-specific] for WinForms and WPF; VB.NET [Windows-specific] for VB.NET applications.

NOTE

The output text file contains only the textual content of the Word document. Document formatting such as bold, italics, lists, tables, and images is not preserved in the text file. For full-fidelity conversion, consider Word-to-PDF or Word-to-DOCX.

//Opens an existing document from the file system through the constructor of the WordDocument class
using (FileStream fileStreamPath = new FileStream("Template.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
{
    //Saves the Word document to a MemoryStream
    MemoryStream stream = new MemoryStream();
    document.Save(stream, FormatType.Txt);
    //Closes the Word document
    document.Close();
}
//Loads a template document
WordDocument document = new WordDocument("Template.docx");
//Saves the document as a text file
document.Save("WordToText.txt", FormatType.Txt);
//Closes the document
document.Close();
'Loads a Word document
Dim document As New WordDocument("Template.docx")
'Saves the document as a text file
document.Save("WordToText.txt", FormatType.Txt)
'Closes the document
document.Close()

You can download a complete working sample from GitHub.

Convert Text to Word

The following code example shows how to convert a Text file into a Word document.

FileStream fileStreamPath = new FileStream("Template.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//Opens an existing document from file system through constructor of WordDocument class
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Txt))
{
    //Saves the Word document to MemoryStream
    MemoryStream stream = new MemoryStream();
    document.Save(stream, FormatType.Docx);
    //Closes the Word document
    document.Close();
}
//Loads a text file
WordDocument document = new WordDocument("Template.txt");
//Saves the document as a Word document
document.Save("TextToWord.docx", FormatType.Docx);
//Closes the document
document.Close();
'Loads a text file
Dim document As New WordDocument("Template.txt")
'Saves the document as a Word document
document.Save("TextToWord.docx", FormatType.Docx)
'Closes the document
document.Close()

You can download a complete working sample from GitHub.

Retrieve Word Document as Plain Text

The following code example shows how to retrieve the textual contents of a Word document and place them into a new Word document. This is useful when you need to strip out section breaks, page breaks, and other non-textual elements while keeping the content editable.

NOTE

document.GetText() returns the document’s content as a string that may include section breaks, paragraph marks, and other control characters. If you only need the visible text, iterate the body items (sections, paragraphs, tables) and concatenate the inner text instead.

FileStream fileStreamPath = new FileStream("Template.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//Opens an existing document from file system through constructor of WordDocument class
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
{
    //Gets the document text
    string text = document.GetText();
    //Creates a new Word document
    WordDocument newdocument = new WordDocument();
    //Adds a new section
    IWSection section = newdocument.AddSection();
    //Adds a new paragraph
    IWParagraph paragraph = section.AddParagraph();
    //Appends the text to the paragraph
    paragraph.AppendText(text);
    //Saves the new Word document to a MemoryStream
    MemoryStream stream = new MemoryStream();
    newdocument.Save(stream, FormatType.Docx);
    //Closes the Word document
    document.Close();
    newdocument.Close();
}
//Loads a template document
WordDocument document = new WordDocument("Template.docx");
//Gets the document text
string text = document.GetText();
//Creates a new Word document
WordDocument newdocument = new WordDocument();
//Adds a new section
IWSection section = newdocument.AddSection();
//Adds a new paragraph
IWParagraph paragraph = section.AddParagraph();
//Appends the text to the paragraph
paragraph.AppendText(text);
//Saves and closes the document
newdocument.Save("Sample.docx", FormatType.Docx);
newdocument.Close();
document.Close();
'Loads a template document
Dim document As New WordDocument("Template.docx")
'Gets the document text
Dim text As String = document.GetText()
'Creates a new Word document
Dim newdocument As New WordDocument()
'Adds a new section
Dim section As IWSection = newdocument.AddSection()
'Adds a new paragraph
Dim paragraph As IWParagraph = section.AddParagraph()
'Appends the text to the paragraph
paragraph.AppendText(text)
'Saves and closes the document
newdocument.Save("Sample.docx", FormatType.Docx)
newdocument.Close()
document.Close()

You can download a complete working sample from GitHub.

See Also