How to read rtf file line by linle...

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
LVL 1
DipehKhakhkharAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chester_M_RagelCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MuntiCommented:
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

DipehKhakhkharAuthor Commented:
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
Chester_M_RagelCommented:
Hi,

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

Chester.
0
Chester_M_RagelCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.