How to convert a string to RTF in C#? -


question

how convert string "européen" rtf-formatted string "europ\'e9en"?

[testmethod] public void convert_a_word_to_rtf() {     // arrange     string word = "européen";     string expected = "europ\'e9en";     string actual = string.empty;      // act     // actual = ... // how?      // assert     assert.areequal(expected, actual); } 

what have found far

richtextbox

richtextbox can used things. example:

richtextbox richtextbox = new richtextbox(); richtextbox.text = "européen"; string rtfformattedstring = richtextbox.rtf; 

but rtfformattedstring turns out entire rtf-formatted document, not string "europ\'e9en".

stackoverflow

google

i've found bunch of other resources on web, nothing quite solved problem.

answer

brad christie's answer

had add trim() remove preceeding space in result. other that, brad christie's solution seems work.

i'll run solution though have bad gut feeling since have substring , trim heck out of richtextbox rtf-formatted string.

test case:

[testmethod] public void test_to_verify_brad_christies_stackoverflow_answer() {         assert.areequal(@"europ\'e9en", "européen".converttortf());         assert.areequal(@"d\'e9finitif", "définitif".converttortf());         assert.areequal(@"\'e0", "à".converttortf());         assert.areequal(@"h\'e4user", "häuser".converttortf());         assert.areequal(@"t\'fcren", "türen".converttortf());         assert.areequal(@"b\'f6den", "böden".converttortf()); } 

logic extension method:

public static class stringextensions {     public static string converttortf(this string value)     {         richtextbox richtextbox = new richtextbox();         richtextbox.text = value;         int offset = richtextbox.rtf.indexof(@"\f0\fs17") + 8; // offset = 118;         int len = richtextbox.rtf.lastindexof(@"\par") - offset;         string result = richtextbox.rtf.substring(offset, len).trim();         return result;     } } 

doesn't richtextbox have same header/footer? read content based on off-set location, , continue using parse. (i think? please correct me if i'm wrong)

there libraries available, i've never had luck them (though found method before exhausting possibilities). in addition, of better ones include nominal fee.


edit
kind of hack, should through need through (i hope):

richtextbox rich = new richtextbox(); console.write(rich.rtf);  string[] words = { "européen", "apple", "carrot", "touché", "résumé", "a européen eating apple while writing résumé, touché!" }; foreach (string word in words) {     rich.text = word;     int32 offset = rich.rtf.indexof(@"\f0\fs17") + 8;     int32 len = rich.rtf.lastindexof(@"\par") - offset;     console.writeline("{0,-15} : {1}", word, rich.rtf.substring(offset, len).trim()); } 

edit 2

the breakdown of codes rtf control code follows:

  • header
    • \f0 - use 0-index font (first font in list, typically microsoft sans serif (noted in font table in header: {\fonttbl{\f0\fnil\fcharset0 microsoft sans serif;}}))
    • \fs17 - font formatting, specify size 17 (17 being in half-points)
  • footer
    • \par specifying it's end of paragraph.

hopefully clears things up. ;-)


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -