Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to read rtf file line by linle...

Posted on 2004-10-27
6
Medium Priority
?
1,404 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
Comment
Question by:DipehKhakhkhar
[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
  • 2
6 Comments
 
LVL 6

Accepted Solution

by:
Chester_M_Ragel earned 100 total points
ID: 12431355
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
ID: 12431477
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:DipehKhakhkhar
ID: 12458953
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
ID: 12541360
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
ID: 12600775
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

618 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