Solved

C# read rtf file?

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

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 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

24 Experts available now in Live!

Get 1:1 Help Now