Text Conversions in Word Library

29 Nov 20245 minutes to read

The Essential® DocIO converts the Word document into Text file and vice versa.

Convert Word to Text

The following code example shows how to convert the Word document into 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.

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))
{
    //Saves the Word document to  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 text file
document.Save("WordToText.txt", FormatType.Txt);
//Closes the document
document.Close();
'Loads a text file
Dim document As New WordDocument("Template.docx")
'Saves the document as 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 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 text file
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 text file
document.Save("TextToWord.docx", FormatType.Docx)
'Closes the document
document.Close()

You can download a complete working sample from GitHub.

The following code example shows how to retrieve the Word document contents as a plain text.

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 new Word document
    WordDocument newdocument = new WordDocument();
    //Adds new section
    IWSection section = newdocument.AddSection();
    //Adds new paragraph
    IWParagraph paragraph = section.AddParagraph();
    //Appends the text to the paragraph
    paragraph.AppendText(text);
    //Saves the Word document to 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 new Word document
WordDocument newdocument = new WordDocument();
//Adds new section
IWSection section = newdocument.AddSection();
//Adds new paragraph
IWParagraph paragraph = section.AddParagraph();
//Appends the text to the paragraph
paragraph.AppendText(text);
//Saves and closes the document
newdocument.Save("Sample.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 new Word document
Dim newdocument As New WordDocument()
'Adds new section
Dim section As IWSection = newdocument.AddSection()
'Adds 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")
newdocument.Close()
document.Close()

You can download a complete working sample from GitHub.