Solved

C# read rtf file?

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

627 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