[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1475
  • Last Modified:

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
0
DipehKhakhkhar
Asked:
DipehKhakhkhar
  • 3
  • 2
1 Solution
 
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
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now