Solved

How to read rtf file line by linle...

Posted on 2004-10-27
1,163 Views
Last Modified: 2008-01-16
Hi,

I have one question related to programming IO in C#.

I am trying to read rtf file (this file i have created using some pdf file), now i want to read this file and store the output in csv(Comma Sepearted file).

I am not able to read the file, i mean it gives all the formating information. Then i tried to investigate about rtf files and found that it has special format.

Now the problem is how do i read that file and create a new output file.

I would be grateful if someone please inform me a way to do this by that time I will try to find some solution for it.

Here is the code which i have written:

===================================================================================================

namespace InfoExtraction
{
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      class FileHandler
      {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {

                  string line;

                  // Read the file and display it line by line. Hardcode file path for time being
                  string filePath  = "C:\\MyFiles\\participants-governments.rtf";
                  StreamReader file = new StreamReader(filePath);
                  StreamWriter sw = new StreamWriter("C:\\MyFiles\\participants-governments.csv");
                  while((line = file.ReadLine()) != null)
                  {
                        sw.WriteLine(line);
                        //Console.WriteLine(line);
                        //Console.WriteLine("\n");
                        if( counter == 5)
                        break;
                        counter++;
                  }

                  file.Close();

            }
      }
}

===================================================================================================


Thanks!
Regards,
Dipesh
0
Question by:DipehKhakhkhar
    6 Comments
     
    LVL 6

    Accepted Solution

    by:
    Do you want something like this? don't forget to refer System.Windows.Forms.dll

    using System;
    using System.IO;
    using System.Windows.Forms;

    namespace InfoExtraction
    {
          /// <summary>
          /// Summary description for Class1.
          /// </summary>
          class FileHandler
          {
                /// <summary>
                /// The main entry point for the application.
                /// </summary>
                [STAThread]
                static void Main(string[] args)
                {
                      // Read the file and display it line by line. Hardcode file path for time being
                      string filePath  = "C:\\MyFiles\\participants-governments.rtf";
                      RichTextBox rt = new RichTextBox();
                      rt.LoadFile(filePath,System.Windows.Forms.RichTextBoxStreamType.RichText);
                      StreamWriter sw = new StreamWriter("C:\\MyFiles\\participants-governments.csv");                  foreach(string line in rt.Lines)
                      {
                            sw.WriteLine(line+",");
                            //Console.WriteLine(line);
                            //Console.WriteLine("\n");
                      }
                      sw.Close();
                }
          }
    }
    0
     
    LVL 1

    Expert Comment

    by:Munti
    Try this:
    --------------------

    try
    {
          // Create an instance of StreamReader to read from a file.
          // The using statement also closes the StreamReader.
          using (StreamReader sr = new StreamReader("C:\\MyFiles\\participants-governments.rtf"))
          {
                String line;
                // Read and display lines from the file until the end of
                // the file is reached.
                
                using (StreamWriter sw = new StreamWriter("C:\\MyFiles\\participants-governments.csv"))
                {
                      while ((line = sr.ReadLine()) != null)
                      {
                            sw.WriteLine(line);
                      }
                }
                sr.Close();
          }
    }
    catch (Exception e)
    {
          // Let the user know what went wrong.
          Console.WriteLine("The file could not be read:");
          Console.WriteLine(e.Message);
    }
    0
     
    LVL 1

    Expert Comment

    by:Munti
    0
     
    LVL 1

    Author Comment

    by:DipehKhakhkhar
    Hi Chester,

    Your solution worked for me. I have found other way to do this also, which involved little bit of manual work and that was copying the rtf file into notepad and then reading notepad file into csv or some other delimiter based file.

    Thanks for your help.

    I have one more question. If i have a pdf in table format with four columns.

    -------------------------------------------------------------------------------------------------

    firstWorld     SecondWord    ThirdWord      FourthWord

    -------------------------------------------------------------------------------------------------

    The fourth column is blank many times so i am not able to write a script to take this kind of file into excel sheet. How should i go about this. Because when i convert it to rtf file, it does not write the blank column as blank line and when i convert pdf to text I am not able to modify it some consistent delimiter based file.

    I hope I am able to explain my question here. If not please inform me, i wil ltry to explain it again.

    Can somebody please provide me some hint to solve this problem.

    Thanks in advance.

    Cheers !!
    0
     
    LVL 6

    Expert Comment

    by:Chester_M_Ragel
    Hi,

    I think its better if you can post that as a seperate new question to get more feedback :)

    Chester.
    0
     
    LVL 6

    Expert Comment

    by:Chester_M_Ragel
    Hi DipehKhakhkhar,

    Do you know how to grade an answer? I think you are member here for long enf to knew these things...
    Please read this,
    http://www.experts-exchange.com/help.jsp#hi20


    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    The Complete Ruby on Rails Developer Course

    Ruby on Rails is one of the most popular web development frameworks, and a useful tool used by both startups and more established companies to build strong graphic user interfaces, and responsive websites and apps.

    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.

    856 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

    20 Experts available now in Live!

    Get 1:1 Help Now