how to write the character `é` in an rft file?

197 views Asked by At

I need to write a string from c# into an rtf file, but having weird problems.
To write the text I simply use

 string fileName = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".rtf";
 System.IO.File.WriteAllText(fileName, body);

body is a string variable, that is filled from a varchar column from a database.

The problem is with the character é which is wrong displayed by wordpad when opening the file like this

enter image description here

If I open the file in notepad, I see this

(één schade gevonden -> ander dossier)

So for some dark reason wordpad decided to show the character é all messed up like this.

I tried writing the file as UTF8 or other unicode encodings, but then wordpad refused to see this file as rtf and just shows the plain text with all the tags

I also looked at this page where it tells me to write a tag like \uXXX? where XXX should be a number defining a Unicode UTF-16 code unit number.
But I cannot find what number to use, or any good example on how to do this.

Actually I am not even sure if its unicode related, the character é is not even a character that needs unicode in my mind, could be wrong off course.

Anyway, does anyone knows how to solve this problem ?
I just need a way to make wordpad not mess up the character é on display and on print.

1

There are 1 answers

6
GuidoG On

The problem was that I did not encoded the RTF file properly.
Using this link provided by Filburt I managed to encode the RTF file correct like this.

var iso = Encoding.GetEncoding("ISO-8859-1");
string fileName = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".rtf";
System.IO.File.WriteAllText(fileName, body, iso);