Categories: C#

Display rtf file correctly In RichTextBox – C#

If you try to load an RTF file having a table with long text and display it in a normal RichTextBox control, you will get disappointed for sure. Because the basic out of the box RichTextBox doesn’t support word wrapping by default.
For example, you have an RTF file “rtfTest.rtf“ with a content table like this

Now if you use the RichTextBox control from .NET and load the rtf file in this control

//Load the rtf file from your location like below.
 richTextBox.LoadFile("D:\\rtfTest.rtf");

You will get the output as below. The table structure even changed to accommodate the long string content.

This is not the one you expected. Because there seems limitation in the RichTextBox control.
Now the Solution is to create a custom control out of this RichTextBox control as below and load the file in that control instead .Follow the code described below.

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace RichTextBoxRTFSample
{
     class ExtendedRichTextBox : RichTextBox
     {
       [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
       static extern IntPtr LoadLibrary(string dllName);

       protected override CreateParams CreateParams
       {
          get
          {
              CreateParams baseParams = base.CreateParams;
              if (LoadLibrary("msftedit.dll") != IntPtr.Zero)
              {
                 baseParams.ClassName = "RICHEDIT50W";
              }
           return baseParams;
          }
       }
     }
  }

Load the rtf file in the above custom control.Use the below code snippet for loading and displaying the rtf file data.

 //Instance of the customized control class
 ExtendedRichTextBox richTextBox = new ExtendedRichTextBox();

 //Set the initial size of the Rich TextBox
 richTextBox.Size = new Size(500, 400);
 this.Controls.Add(richTextBox);

 //Bring the control to top level
 richTextBox.BringToFront();

 //Load the rtf file from your location like below.
 richTextBox.LoadFile("D:\\rtfTest.rtf");

Run the application and see the output,

Summary:

This post and the previous post Table in RichTextBox With Text Wrap In Columns are addressing the issue of richtextbox while displaying RTF File With Table and long content In it. I hope,after reading this post you may not say again that RichTextBox control cannot display a .rtf file correctly. Share your thoughts below and share the post.
Happy Coding!

Rajeev

View Comments

Recent Posts

OWIN Authentication in .NET Core

OWIN (Open Web Interface for .NET) is an interface between web servers and web applications…

1 year ago

Serializing and Deserializing JSON using Jsonconvertor in C#

JSON (JavaScript Object Notation) is a commonly used data exchange format that facilitates data exchange…

1 year ago

What is CAP Theorem? | What is Brewer’s Theorem?

The CAP theorem is also known as Brewer's theorem. What is CAP Theorem? CAP theorem…

1 year ago

SOLID -Basic Software Design Principles

Some of the Key factors that need to consider while architecting or designing a software…

1 year ago

What is Interface Segregation Principle (ISP) in SOLID Design Principles?

The Interface Segregation Principle (ISP) is one of the SOLID principles of object-oriented design. The…

1 year ago

What is Single Responsibility Principle (SRP) in SOLID Design Priciples?

The Single Responsibility Principle (SRP), also known as the Singularity Principle, is a software design…

1 year ago