0

I have a hindi RTF file with content like:

कोलकाता, 11 दिसंबर पश्चिम बंगाल के बर्दवान जिले में कक्षा नौ की एक छात्रा ने फांसी लगाकर आत्महत्या कर ली।

In my console application I want to read that RTF file and Change some content programatically.

I using streamreader to read the file but when converting to string it is producing the following output:

ÚUæCþUèØ-SßæS‰Ø

×Âý Ñ Sßæ§Ù Üê •¤è ¼ßæ ÂØæü# ׿˜æ ×ð´ ãUôÙð •¤æ ¼æßæ

ÖæðÂæÜ, vv ç¼â¢ÕÚ (¥æ§ü°°Ù°â)Ð ×ŠØ Âý¼ðàæ ×ð´ Sßæ§Ù Üê ¥æñÚ ÇðU¢»ê âð ¥Õ Ì•¤ •¤§ü Üæð»æð´ •¤è ׿ñÌ ãUæð ¿é•¤è ãñU ¥õÚU ¥SÂÌæÜ ×ð´ ç¿ç•¤ˆâ•¤èØ âéçßÏæ¥ô´ •¤è •¤×è •ð¤ âæÍ-âæÍ ¼ßæ¥ô´ •ð¤ ¥Öæß •ð¤ Öè ¥æÚUæð ܻÌð ÚãðU ãñU¢Ð

SßæS‰Ø çßÖæ» Ùð ãUæÜæ¢ç•¤ ØãU ¼æßæ ç•¤Øæ ãñU 畤 Úæ…Ø ×ð´ ׿ñâ×è ÚUæð», Sßæ§Ù Üê •ð¤ ©Â¿æÚ •ð¤ çܰ Âý¼ðàæ •𤠥SÂÌæÜæð´ ×ð´ ¥æßàØ•¤ ¼ßæ¥æð´ •¤æ ÂØæü# ÂýÕ¢Ï ç•¤Øæ »Øæ ãñUÐ

I have tried the windows form RichTextBox to read the RTF file, but it always show Invalid File Format.

So what will be the best possible solution to read and modify rtf file in C#

StreamReader sr = new StreamReader(fpath, Encoding.Default, true);
string s1 = sr.ReadToEnd();
sr.Close();

also tried

using (System.Windows.Forms.RichTextBox rtBox = new System.Windows.Forms.RichTextBox())
{
    // Get the contents of the RTF file. Note that when it is 
    // stored in the string, it is encoded as UTF-16. 
    string s = System.IO.File.ReadAllText(fpath);

    // Convert the RTF to plain text.
    rtBox.Rtf = s; // error file format invalid
    string plainText = rtBox.Text;
}
Community
  • 1
  • 1
Abhay Prince
  • 2,032
  • 1
  • 15
  • 17

1 Answers1

0

The RichTextBox control can load an RTF file directly, do not use StreamReader to read RTF file because it can contain a lot of Control Characters.

After loading the file to the RichTextBox, use the Text property to get the plain text of the file.

RichTextBox also has a SaveFile method to save the modified content to a file.

kennyzx
  • 12,845
  • 6
  • 39
  • 83
  • i have already used the richtextbox. See the code i posted. It gives error file format invalid. Even loadfile method is giving the same error. – Abhay Prince Dec 12 '14 at 04:33
  • Can you open it in MS Word? – kennyzx Dec 12 '14 at 04:34
  • MS word is not the proper way for server. In my local system the rtf file is opened in MS but produce the above same result like – Abhay Prince Dec 12 '14 at 04:55
  • same result like ×ÙæðÚ¢U…æÙ-çâÙð׿ ¥×ðçÚU•¤è ÙõâðÙæ âð ×éÜæ•¤æÌ •¤ÚU »¼»¼ ãéU§ZU ç•¤× Üæòâ °¢…æðçÜâ, x ç¼âÕÚU (¥æ§üU°°Ù°â)Ð çÚU°çÜÅUè ÅUèßè SÅUæÚU ç•¤× •¤¼æüçàæØæ¢ •¤ãUÌè ãñ´U 畤 çÂÀÜð ×ãUèÙð âõÖæ‚Ø âð ©U‹ãð´U ¥×ðçÚ•¤è ÙõâðÙæ âð ×éÜæ•¤æÌ •¤æ ×õ•¤æ ç×ÜæÐ and when i select the Chanayka Font it shows the text in Hindi – Abhay Prince Dec 12 '14 at 04:55
  • What about `rtb.SelectAll(); rtb.SelectionFont = new Font("Chanayka",...);`, like [this](http://stackoverflow.com/q/16306184/815938)? – kennyzx Dec 12 '14 at 05:02
  • when rtb is blank what is use of SelectAll(). I have to put first the tring and then select all. Plz. show the way. – Abhay Prince Dec 12 '14 at 05:22