Solved

C# read rtf file?

Posted on 2013-06-25
3
2,338 Views
Last Modified: 2013-06-30
I have a rtf file that contains some text and would like to read and parse its contents and probably load them in database.

I can read and process txt files using StreamReader's readline method. but when I try the same thing on rtf file it adds formatting and some non-readable text in the data string so the end result is not what I expect.

How can I process this rtf file? Would I need to manually copy the content of this rtf file and paste it in txt file before processing or is there a better way to directly process rtf files?


using (StreamReader sr = new StreamReader(FileName))
      {
            string strInputRcd;

// Read and display lines from the file until the end of
// the file is reached.
      while ((strInputRcd = sr.ReadLine()) != null)
            {
                                 
                                 }

Thanks
0
Comment
Question by:Ricky66
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 12

Assisted Solution

by:duttcom
duttcom earned 100 total points
ID: 39276750
You really should go with a text file instead. The RTF file will contain a whole lot of formatting information which makes no sense at all to the stream reader. Your database only cares about the text, not what colour, size, font or position that text should have.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 400 total points
ID: 39276823
I generally try to avoid importing massive libraries just for the sake of one feature, but in the case of RTF I tend to make an exception. If, counter to duttcom's suggestioni, you are going to stick with an RTF file, then you might consider adding a reference to the System.Windows.Forms.dll library. Then you can create an instance of a RichTextBox control. You can load the RTB with your RTF code, then extract just the text by accessing the Text property.

e.g.

// with a reference to System.Windows.Forms added


namespace _28167832
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Windows.Forms.RichTextBox rtb = new System.Windows.Forms.RichTextBox();

            rtb.Rtf = @"{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard Hello World.\par}";

            string text = rtb.Text;
        }
    }
}

Open in new window

0
 

Author Closing Comment

by:Ricky66
ID: 39288918
Thanks!
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question