When working with document format conversion in .NET applications, converting RTF to Word is a frequent requirement. RTF (Rich Text Format) is widely used as a cross‑platform intermediate format, but final deliverables often require the more universally supported Word formats: .doc or .docx.
This article shows how to convert RTF to Word using only a few lines of C# code with a free .NET library. The solution does not require Microsoft Office to be installed and runs entirely in code, making it ideal for background services, web APIs, and desktop applications.
Install the Library
First, create a .NET project (console app, WebAPI, or other). Then install the Free Spire.Doc library via the NuGet Package Manager:
Install-Package FreeSpire.Doc
Note: The free version has a page limit when processing documents.
After installation, add the following namespace at the top of your code file:
using Spire.Doc;
Core Conversion Logic
With Free Spire.Doc, the conversion process is simple: load the RTF document, then save it in Word format.
Minimal Example
// Initialize a Document instance
Document doc = new Document();
// Load the RTF file
doc.LoadFromFile("input.rtf", FileFormat.Rtf);
// Save as Word 97‑2003 (.doc)
doc.SaveToFile("output.doc", FileFormat.Doc);
// Or save as modern Word 2007+ (.docx)
doc.SaveToFile("output.docx", FileFormat.Docx);
- The
LoadFromFilemethod usesFileFormat.Rtfto correctly parse RTF content. - The
SaveToFilemethod supports both legacy binary.docand modern XML‑based.docxformats.
Complete Console Example
This full example includes path definitions, basic exception handling, and dual‑format output:
using System;
using Spire.Doc;
class RtfToWordConverter
{
static void Main(string[] args)
{
string rtfPath = @"C:\docs\source.rtf";
string docxPath = @"C:\docs\result.docx";
string docPath = @"C:\docs\result.doc";
try
{
Document doc = new Document();
doc.LoadFromFile(rtfPath, FileFormat.Rtf);
// Export to DOCX
doc.SaveToFile(docxPath, FileFormat.Docx);
Console.WriteLine($"Converted successfully: {docxPath}");
// Also export to DOC
doc.SaveToFile(docPath, FileFormat.Doc);
Console.WriteLine($"Converted successfully: {docPath}");
}
catch (Exception ex)
{
Console.WriteLine($"Conversion failed: {ex.Message}");
}
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
Advanced: Convert Using Streams
For web applications or in‑memory processing, you can read and write using streams instead of physical files. This avoids temporary files and is more efficient for uploaded content.
using (FileStream inputStream = new FileStream("input.rtf", FileMode.Open))
{
Document doc = new Document();
doc.LoadFromStream(inputStream, FileFormat.Rtf);
using (FileStream outputStream = new FileStream("output.docx", FileMode.Create))
{
doc.SaveToStream(outputStream, FileFormat.Docx);
}
}
Important Considerations
Format Compatibility
Free Spire.Doc supports most common RTF features, including fonts, paragraphs, tables, and images. However, some advanced elements, such as embedded OLE objects or complex mathematical formulas, may not be perfectly preserved. Always test with your typical documents.File Permissions
Ensure your application has read access to the source RTF file and write access to the output directory. Missing permissions are a common cause of runtime errors, especially in server deployments.Batch Processing
When converting multiple RTF files in a loop, create a newDocumentinstance for each file and dispose it properly to prevent memory leaks.
Summary
Converting RTF to Word in C# is straightforward when using a lightweight library like Free Spire.Doc. The solution is clean, dependency‑free (no Office required), and supports both .doc and .docx output.
For production use, test your document layouts first, handle exceptions appropriately, and choose the output format based on compatibility and file size requirements.
Top comments (0)