Skip to content

FenPhoenix/ReasonableRTF

Repository files navigation

ReasonableRTF - Fast RTF to Plain Text Converter 🚀

A lightweight and performant C# library designed for rapidly converting Rich Text Format (RTF) files into plain text.


Features

  • Fast: Over 100x faster than RichTextBox
  • Accurate: All characters converted correctly — even Wingdings
  • Fully cross-platform
  • Can read from forward-only streams such as DeflateStream
  • We even support this obscure nonsense

Installation

Install the library via NuGet:

dotnet add package ReasonableRTF.Standard

Quick Start

Converting An RTF File

Use the static ConvertRtf.ToText() method to do a one-off conversion:

using ReasonableRTF;
using ReasonableRTF.Enums;
using ReasonableRTF.Models;

// ...

RtfResult result = ConvertRtf.ToText(@"C:\rtf_files\some_file.rtf");

if (result.Error == RtfError.OK)
{
    // Conversion was successful
    string plainText = result.Text;
    Console.WriteLine("Converted Text:\n" + plainText);
}
else
{
    // Handle conversion errors
    Console.WriteLine($"Conversion Error: {result.Error} at byte position: {result.BytePositionOfError}");
    if (result.Exception != null)
    {
        Console.WriteLine($"Exception Details: {result.Exception.Message}");
    }
}

Converting Many RTF Files Efficiently

You can create and reuse an RtfToTextConverter instance to minimize overhead when converting many files:

using ReasonableRTF;
using ReasonableRTF.Enums;
using ReasonableRTF.Models;

// ...

RtfToTextConverter converter = new RtfToTextConverter();
foreach (string fileName in Directory.EnumerateFiles(@"C:\rtf_files\", "*.rtf"))
{
    RtfResult result = converter.Convert(fileName);
    if (result.Error == RtfError.OK)
    {
        // Conversion was successful
        string plainText = result.Text;
        Console.WriteLine("Converted Text:\n" + plainText);
    }
    else
    {
        // Handle conversion errors
        Console.WriteLine($"Conversion Error: {result.Error} at byte position: {result.BytePositionOfError}");
        if (result.Exception != null)
        {
            Console.WriteLine($"Exception Details: {result.Exception.Message}");
        }
    }
}

For efficiency, an RtfToTextConverter will not trim its internal growable buffers between conversions. You can use the ResetMemory() method to reset these buffers back to their default sizes. This way, you can keep one RtfToTextConverter instance around indefinitely without worrying about memory use.


The RtfResult Struct

The RtfResult struct provides full details about the conversion process, ensuring you can robustly handle success and failure cases.

Property Type Description
Text string The converted plain text.
Error RtfError The error code. This will be RtfError.OK upon successful conversion.
BytePositionOfError int The approximate position in the data stream where the error occurred, or -1 if no error.
Exception Exception? The caught exception, or null if no exception occurred during conversion.

Conversion Options

For more control over the output, you can provide an instance of the RtfToTextConverterOptions class.

RtfToTextConverterOptions options = new RtfToTextConverterOptions
{
    ConvertHiddenText = true,          // Include text marked as hidden
    LineBreakStyle = LineBreakStyle.LF // Use Unix-style line breaks
};

RtfResult result = ConvertRtf.ToText(@"C:\rtf_files\some_file.rtf", options);
// ... check result

The available options are documented in RtfToTextConverterOptions.cs.


Benchmarks

.NET 10 64-bit


BenchmarkDotNet v0.15.8, Windows 11 (10.0.26200.8457/25H2/2025Update/HudsonValley2)
AMD Ryzen 5 5600 3.50GHz, 1 CPU, 12 logical and 6 physical cores
.NET SDK 10.0.300
  [Host]     : .NET 10.0.8 (10.0.8, 10.0.826.23019), X64 RyuJIT x86-64-v3
  DefaultJob : .NET 10.0.8 (10.0.8, 10.0.826.23019), X64 RyuJIT x86-64-v3


Method Mean Error StdDev Speed
RichTextBox_FullSet 3,331.340 ms 6.2250 ms 5.5183 ms 43.59 MB/s 1x
RichTextBox_NoImageSet 1,432.217 ms 3.7089 ms 3.4693 ms 2.47 MB/s 1x
ReasonableRTF_FullSet 16.050 ms 0.0402 ms 0.0376 ms 9047.46 MB/s 208x
ReasonableRTF_NoImageSet 3.746 ms 0.0036 ms 0.0033 ms 945.99 MB/s 382x
ReasonableRTF_FullSet_Streamed 17.813 ms 0.0235 ms 0.0196 ms 8152.01 MB/s 187x
ReasonableRTF_NoImageSet_Streamed 3.815 ms 0.0032 ms 0.0030 ms 928.88 MB/s 375x

.NET Framework 4.8 64-bit


BenchmarkDotNet v0.15.8, Windows 11 (10.0.26200.8457/25H2/2025Update/HudsonValley2)
AMD Ryzen 5 5600 3.50GHz, 1 CPU, 12 logical and 6 physical cores
  [Host]     : .NET Framework 4.8.1 (4.8.9325.0), X64 RyuJIT VectorSize=256
  DefaultJob : .NET Framework 4.8.1 (4.8.9325.0), X64 RyuJIT VectorSize=256


Method Mean Error StdDev Speed
RichTextBox_FullSet 2,779.775 ms 3.9318 ms 3.2833 ms 52.24 MB/s 1x
RichTextBox_NoImageSet 992.237 ms 2.5478 ms 2.2585 ms 3.57 MB/s 1x
ReasonableRTF_FullSet 19.672 ms 0.0750 ms 0.0701 ms 7381.65 MB/s 141x
ReasonableRTF_NoImageSet 4.923 ms 0.0059 ms 0.0052 ms 719.82 MB/s 202x
ReasonableRTF_FullSet_Streamed 22.238 ms 0.0565 ms 0.0528 ms 6529.89 MB/s 125x
ReasonableRTF_NoImageSet_Streamed 4.990 ms 0.0166 ms 0.0155 ms 710.15 MB/s 199x

.NET Framework 4.8 32-bit


BenchmarkDotNet v0.15.8, Windows 11 (10.0.26200.8457/25H2/2025Update/HudsonValley2)
AMD Ryzen 5 5600 3.50GHz, 1 CPU, 12 logical and 6 physical cores
  [Host]     : .NET Framework 4.8.1 (4.8.9325.0), X86 LegacyJIT
  DefaultJob : .NET Framework 4.8.1 (4.8.9325.0), X86 LegacyJIT


Method Mean Error StdDev Speed
RichTextBox_FullSet 6,932.056 ms 131.6848 ms 140.9013 ms 20.95 MB/s 1x
RichTextBox_NoImageSet 2,885.139 ms 57.0121 ms 81.7651 ms 1.23 MB/s 1x
ReasonableRTF_FullSet 41.565 ms 0.0809 ms 0.0757 ms 3493.61 MB/s 167x
ReasonableRTF_NoImageSet 8.121 ms 0.0292 ms 0.0273 ms 436.36 MB/s 355x
ReasonableRTF_FullSet_Streamed 45.709 ms 0.1658 ms 0.1551 ms 3176.87 MB/s 152x
ReasonableRTF_NoImageSet_Streamed 8.089 ms 0.0095 ms 0.0080 ms 438.08 MB/s 357x

Supported RTF features

Supported

  • All basic plain text, hex-encoded chars, Unicode-encoded chars
  • Symbol fonts (Wingdings 1, 2 and 3, Webdings, Symbol, and Zapf Dingbats) converted to Unicode equivalents
  • Characters specified as "SYMBOL" field instructions
  • Undocumented use of the \langN keyword to specify character encoding - old versions of RichTextBox used to support this

Partially supported

  • Tables: Cells and rows have spaces between them, but not much functionality beyond that.
  • Lists: Numbers and bullets show up (that's better than RichTextBox most of the time), but indentation usually doesn't.

Not currently supported

  • Footnotes
  • "HYPERLINK" field instruction value
  • Math objects

License and Attribution

Code License

The original code for this RTF converter was written by Brian Tobin and is licensed under the MIT License (Copyright 2024-2026 Brian Tobin). For the full license text, please refer to the LICENSE file ReasonableRTF.

Flamifly contributed to the readme (installation, quick start, documentation, etc), and ported the original .NET-only version to .NET Standard.

Releases

No releases published

Packages

 
 
 

Contributors

Languages