Share via


RTF text To Plain Text

Question

Tuesday, February 26, 2013 11:18 AM

Hi
I'm trying to convert my RTF text to Plain Text. But the conversion failed. 
I've write this code by reference of  Link
My Code

string path = RicheditControl1.Document.GetRtfText(field.ResultRange);
                RichTextBox rtBox = new RichTextBox();
                string plainText = rtBox.Text;
                string rtfText = path;
                rtBox.Rtf = rtfText;
                plainText = rtBox.Text;
                File.WriteAllText(@"D:\output.txt", plainText);

Desire Input RTF Content:

{\\rtf1\\deff0{\\fonttbl{\\f0 Times New Roman;}{\\f1 Segoe UI;}}{\\colortbl\\red0\\green0\\blue0 ;\\red0\\green0\\blue255 ;\\red64\\green64\\blue64 ;\\red229\\green229\\blue229 ;\\red166\\green166\\blue166 ;\\red255\\green255\\blue255 ;}{\\*\\defchp \\f1\\fs18}{\\*\\listoverridetable}{\\stylesheet {\\ql\\f1\\fs18 Normal;}{\\*\\cs1\\f1\\fs18 Default Paragraph Font;}{\\*\\cs2\\sbasedon1\\f1\\fs18 Line Number;}{\\*\\cs3\\ul\\f1\\fs18\\cf1 Hyperlink;}{\\*\\ts4\\tsrowd\\f1\\fs18\\ql\\trautofit1\\tscellpaddfl3\\tscellpaddl108\\tscellpaddfr3\\tscellpaddr108\\tsvertalt\\cltxlrtb Normal Table;}{\\*\\ts5\\tsrowd\\sbasedon4\\f1\\fs18\\ql\\trbrdrt\\brdrs\\brdrw10\\brdrcf0\\trbrdrl\\brdrs\\brdrw10\\brdrcf0\\trbrdrb\\brdrs\\brdrw10\\brdrcf0\\trbrdrr\\brdrs\\brdrw10\\brdrcf0\\trautofit1\\tscellpaddfl3\\tscellpaddl108\\tscellpaddfr3\\tscellpaddr108\\tsvertalt\\cltxlrtb Table Simple 1;}{\\*\\ts6\\tsrowd\\sbasedon4\\f1\\fs18\\cf2\\ql\\trautofit1\\tsvertalc\\tsbrdrb\\brdrs\\brdrw20\\brdrcf3\\cltxlrtb\\clpadfb3\\clpadb43\\clpadfl3\\clpadl43 List1;}{\\*\\ts7\\tsrowd\\sbasedon6\\f1\\fs18\\cf2\\ql\\trautofit1\\tblindtype3\\tblind0\\tsvertalc\\tsbrdrb\\brdrs\\brdrw20\\brdrcf3\\cltxlrtb\\clpadfb3\\clpadb43\\clpadfl3\\clpadl43 List2;}}\\nouicompat\\splytwnine\\htmautsp\\sectd\\pard\\plain\\ql\\v\\f1\\fs18\\cf0\\par\\trowd\\irow0\\irowband0\\ts7\\trleft-108\\trftsWidth4\\trwWidth5000\\trautofit1\\trpaddfl3\\trpaddl108\\trpaddfr3\\trpaddr108\\tblindtype3\\tblind0\\clvertalc\\clcbpat4\\clbrdrt\\brdrcf0\\clbrdrl\\brdrnil\\clbrdrb\\brdrcf0\\clbrdrr\\brdrs\\brdrw20\\brdrcf5\\cltxlrtb\\clftsWidth4\\clwWidth5000\\clpadfb3\\clpadb43\\clpadfl3\\clpadl43\\clpadfr3\\clpadr108\\clpadft3\\clpadt108\\cellx7094\\pard\\plain\\ql\\intbl\\yts7{\\f1\\fs18\\cf5 Text}\\f1\\fs18\\cf5\\cell\\trowd\\irow0\\irowband0\\ts7\\trleft-108\\trftsWidth4\\trwWidth5000\\trautofit1\\trpaddfl3\\trpaddl108\\trpaddfr3\\trpaddr108\\tblindtype3\\tblind0\\clvertalc\\clcbpat4\\clbrdrt\\brdrcf0\\clbrdrl\\brdrnil\\clbrdrb\\brdrcf0\\clbrdrr\\brdrs\\brdrw20\\brdrcf5\\cltxlrtb\\clftsWidth4\\clwWidth5000\\clpadfb3\\clpadb43\\clpadfl3\\clpadl43\\clpadfr3\\clpadr108\\clpadft3\\clpadt108\\cellx7094\\row\\pard\\plain\\ql\\intbl\\yts7{\\f1\\fs18\\cf2 \\{\\\\rtf1\\\\deff0\\{\\\\fonttbl\\{\\\\f0 Times New Roman;\\}\\}\\{\\\\colortbl\\\\red0\\\\green0\\\\blue0 ;\\\\red0\\\\green0\\\\blue255 ;\\}\\{\\\\*\\\\listoverridetable\\}\\{\\\\stylesheet \\{\\\\ql Normal;\\}\\{\\\\*\\\\cs1 Default Paragraph Font;\\}\\{\\\\*\\\\cs2\\\\sbasedon1 Line Number;\\}\\{\\\\*\\\\cs3\\\\ul\\\\cf1 Hyperlink;\\}\\{\\\\*\\\\ts4\\\\tsrowd\\\\ql\\\\trautofit1\\\\tscellpaddfl3\\\\tscellpaddl108\\\\tscellpaddfr3\\\\tscellpaddr108\\\\tsvertalt\\\\cltxlrtb Normal Table;\\}\\{\\\\*\\\\ts5\\\\tsrowd\\\\sbasedon4\\\\ql\\\\trbrdrt\\\\brdrs\\\\brdrw10\\\\trbrdrl\\\\brdrs\\\\brdrw10\\\\trbrdrb\\\\brdrs\\\\brdrw10\\\\trbrdrr\\\\brdrs\\\\brdrw10\\\\trautofit1\\\\tscellpaddfl3\\\\tscellpaddl108\\\\tscellpaddfr3\\\\tscellpaddr108\\\\tsvertalt\\\\cltxlrtb Table Simple 1;\\}\\}\\\\nouicompat\\\\splytwnine\\\\htmautsp\\\\sectd\\\\pard\\\\plain\\\\ql\\{\\\\cf0 Test Social History\\}\\\\cf0\\\\par\\}}\\f1\\fs18\\cf2\\cell\\trowd\\irow1\\irowband1\\lastrow\\ts7\\trleft-108\\trftsWidth4\\trwWidth5000\\trautofit1\\trpaddfl3\\trpaddl108\\trpaddfr3\\trpaddr108\\tblindtype3\\tblind0\\clvertalc\\clbrdrt\\brdrnil\\clbrdrl\\brdrnil\\clbrdrb\\brdrs\\brdrw20\\brdrcf3\\clbrdrr\\brdrnil\\cltxlrtb\\clftsWidth4\\clwWidth5000\\clpadfb3\\clpadb43\\clpadfl3\\clpadl43\\clpadfr3\\clpadr108\\clpadft3\\clpadt108\\cellx7094\\row\\pard\\plain\\ql\\f1\\fs18\\cf0}

OUTPUT

Text
{\rtf1\deff0{\fonttbl{\f0 Times New Roman;}}{\colortbl\red0\green0\blue0 ;\red0\green0\blue255 ;}{\*\listoverridetable}{\stylesheet {\ql Normal;}{\*\cs1 Default Paragraph Font;}{\*\cs2\sbasedon1 Line Number;}{\*\cs3\ul\cf1 Hyperlink;}{\*\ts4\tsrowd\ql\trautofit1\tscellpaddfl3\tscellpaddl108\tscellpaddfr3\tscellpaddr108\tsvertalt\cltxlrtb Normal Table;}{\*\ts5\tsrowd\sbasedon4\ql\trbrdrt\brdrs\brdrw10\trbrdrl\brdrs\brdrw10\trbrdrb\brdrs\brdrw10\trbrdrr\brdrs\brdrw10\trautofit1\tscellpaddfl3\tscellpaddl108\tscellpaddfr3\tscellpaddr108\tsvertalt\cltxlrtb Table Simple 1;}}\nouicompat\splytwnine\htmautsp\sectd\pard\plain\ql{\cf0 Test Social History}\cf0\par}

Expected Output: Test Social History

All replies (7)

Tuesday, February 26, 2013 1:13 PM ✅Answered

Your input RTF looks suspicious: it starts with “{\rtf1” instead of “{\rtf1” and also contain another “{\rtf1” inside. Probably GetRtfText does not work as expected.

Try extracting the RTF substring from your first Output (i.e. remove “Text” and keep the substring from “{\rtf1” to “}”), then assign it again to rtBox.Rtf, then get rtBox.Text. It will be “Test Social History”.


Tuesday, February 26, 2013 11:45 AM

Instead of

string rtfText = path;

you should better read in the rtf file as follows:

string rtfText = System.IO.File.ReadAllText(path);

Kind regards,

wizend


Tuesday, February 26, 2013 11:55 AM

Actually i'm Not Reading the RTF text from file. i Pass the RTF text Directly. 


Tuesday, February 26, 2013 12:07 PM

can you see, what values you are getting in variable path? maybe you can try:

string path = RicheditControl1.Document.GetRtfText(field.ResultRange);
RichTextBox rtBox = new RichTextBox();
//string plainText = rtBox.Text;
//string rtfText = path;
rtBox.Rtf = path;
string plainText = rtBox.Text;
File.WriteAllText(@"D:\output.txt", plainText);

regards

joon


Tuesday, February 26, 2013 12:32 PM

Check DevExpress Support Center

How to convert between different RTF document formats without creating an editor instance

http://www.devexpress.com/Support/Center/p/K18272.aspx


Tuesday, February 26, 2013 1:09 PM

I tried. But Not use. didn't get output


Tuesday, February 26, 2013 1:18 PM

If your rtf string is not of a valid rtf format your attempt to convert it will fail. Save your string to a file, give it the ".rtf" extension and try to open it with wordpad.

Maybe, thats the issue here

wizend