Solved

C# read rtf file?

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

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:…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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