Recursive AI

Convert RTF to DOCX in C#

OneDocument is a free, open-source Word, Excel and PowerPoint document library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Terminal
dotnet add package RecursiveAI.OneDocument.Documents

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneDocument.Documents;
 
using var doc = Document.Open("note.rtf");
doc.SaveAs("note.docx");
Console.WriteLine("Wrote note.docx");

Unzip the example, then dotnet run.

This writes note.docx from note.rtf in the download. The source says “Notice: Bring a notebook.” Here is the entire input (\b turns bold on and \b0 turns it off):

Text
{\rtf1\ansi \b Notice\b0 : Bring a notebook.}
Text
Wrote note.docx

The full program

The example writes the RTF above, saves a DOCX copy, and reopens the copy to read the paragraph and its bold run (a stretch of text with its own formatting).

C#
using RecursiveAI.OneDocument.Documents;
 
const string rtf = @"{\rtf1\ansi \b Notice\b0 : Bring a notebook.}";
File.WriteAllText("note.rtf", rtf);
using (var source = Document.Open("note.rtf"))
    source.SaveAs("note.docx");
 
using var converted = Document.Open("note.docx");
var paragraph = converted.Paragraphs.Single();
Console.WriteLine(paragraph.Text);
var bold = paragraph.Runs().Single(run => run.Bold == true);
Console.WriteLine($"Bold: {bold.Text}");

dotnet run prints:

Text
Notice: Bring a notebook.
Bold: Notice

If the source isn’t recognized as a supported document, Document.Open throws before SaveAs; changing the filename doesn’t fix invalid content.

Variations

Convert RTF you already have in memory

Open a seekable stream instead of writing a temporary RTF file. Keep the stream open until you’re done with the document.

C#
using System.Text;
using RecursiveAI.OneDocument.Documents;
 
const string rtf = @"{\rtf1\ansi \b Notice\b0 : Bring a notebook.}";
using var input = new MemoryStream(Encoding.ASCII.GetBytes(rtf));
using var doc = Document.Open(input);
doc.SaveAs("from-stream.docx");
Console.WriteLine("Wrote from-stream.docx");
Text
Wrote from-stream.docx

The short answer

Open the RTF using Document.Open and save it to a .docx path with SaveAs. That produces a Word document without automating a desktop copy of Word. In the sample, the word “Notice” is still bold when the program reads the DOCX back, and the rest of the paragraph is plain.

What is the difference between RTF and DOCX?

RTF stores text with control words such as \b for bold, grouped with braces. DOCX stores Word document markup in a package of parts, such as the main document, styles, and numbering. A DOCX file isn’t RTF with a new extension. The RTF reader looks for the {\rtf signature, then maps the RTF into OneDocument’s Word document model. The .docx output path tells the writer to save that model as a DOCX package.

Choose DOCX when the next step is editing the document or processing its Word parts. Keep the original RTF when you need the exact input bytes. The DOCX contains the mapped document, not a byte-for-byte copy of the RTF control words.

Can I convert an RTF file to a Word document and keep its formatting?

Common formatting has a counterpart in the Word document model. OneDocument maps character formatting such as bold, italic, and underline to Word run properties. It also maps paragraph alignment and spacing, lists, and tables. The sample reads Run.Bold from the saved document; that property checks direct formatting, not whether a style makes the text appear bold.

For complex RTF, add these lines after Document.Open in the snippet at the top to print the import report:

C#
var loss = doc.ImportLoss;
Console.WriteLine(loss is { IsLossless: false } ? loss.ToString() : "No losses reported.");

For note.rtf, this prints No losses reported. In more complex files, a drawing shape can keep its picture or text without keeping its wrapping, size, or position. An embedded object can become its preview picture. Unknown control words are skipped. A clean text check doesn’t tell you that those elements still look or behave the same, so open a converted copy and review the pages that use them before replacing the original.

All onedocument recipes