Solved

C# read rtf file?

Posted on 2013-06-25
3
2,108 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

929 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now