RTF Conversions in Word Library
9 Sep 20243 minutes to read
RTF
The Rich Text Format (RTF) is one of the document formats supported by Microsoft Word and many other Word processing applications. RTF is human readable file format invented for interchanging formatted text between applications. It is an optional format for Word that retains most formatting and all content of the original document.
Most of the Word processors (including Microsoft Word) uses the XML-based file formats, Microsoft has discontinued enhancements to the RTF and still supporting it. The last version was 1.9.1 released in 2008.
Assemblies and NuGet packages required
Refer to the following links for assemblies and NuGet packages required based on platforms to convert the RTF document into Word document and vice versa using the .NET Word Library (DocIO).
The Essential DocIO converts the RTF document into Word document and vice versa. The following code shows how to convert RTF document into Word document.
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("Input.rtf", 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.Rtf))
{
//Saves the Word document to MemoryStream
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
//Closes the Word document
document.Close();
}
//Loads an existing document
WordDocument document = new WordDocument("Input.rtf", FormatType.Rtf);
//Saves the Word document as RTF file
document.Save("RtfToWord.docx", FormatType.Docx);
//Closes the document
document.Close();
'Loads an existing document
Dim document As New WordDocument("Input.rtf", FormatType.Rtf)
'Saves the Word document as RTF file
document.Save("RtfToWord.docx", FormatType.Docx)
'Closes the document
document.Close()
You can download a complete working sample from GitHub.
The following code example shows how to convert Word document into RTF document.
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.Rtf);
//Closes the Word document
document.Close();
}
//Loads an existing document
WordDocument document = new WordDocument("Template.docx", FormatType.Docx);
//Saves the Word document as RTF file
document.Save("WordToRtf.rtf", FormatType.Rtf);
//Closes the document
document.Close();
'Loads an existing document
Dim document As New WordDocument("Template.docx", FormatType.Docx)
'Saves the Word document as RTF file
document.Save("WordToRtf.rtf", FormatType.Rtf)
'Closes the document
document.Close()
You can download a complete working sample from GitHub.
Supported and Unsupported features
The supported and unsupported features of DocIO based on file formats can be referred here
Online Demo
- Explore how to convert the RTF file to Word document using the .NET Word Library (DocIO) in a live demo here.