0

I was looking for the way to convert RTF string to plain text in java, but unfortunately I can't find the solution.

I need convert this RTF string to plain text

{\rtf1\ansi\ansicpg932\deff0\deflang1033\deflangfe1041{\fonttbl{\f0\fnil\fcharset128 \'82\'6c\'82\'72 \'96\'be\'92\'a9;}{\f1\fnil\fcharset128 MS UI Gothic;}} {\colortbl ;\red0\green128\blue128;} \viewkind4\uc1\pard\cf1\lang1041\b\f0\fs24\'83\'65\'83\'58\'83\'67\'82\'c5\'82\'b7\'81\'42\'83\'65\'83\'58\'83\'67\'82\'c5\'82\'b7\'81\'42\cf0\b0\f1\fs20\par \cf1\b\f0\fs24\'83\'65\'83\'58\'83\'67\'82\'c5\'82\'b7\'81\'42\cf0\b0\f1\fs20\par \cf1\b\f0\fs24\'83\'65\'83\'58\'83\'67\'82\'c5\'82\'b7\'81\'42\cf0\b0\f1\fs20\par \cf1\b\f0\fs24\'83\'65\'83\'58\'83\'67\'82\'c5\'82\'b7\'81\'42\cf0\b0\f1\fs20\par \cf1\b\f0\fs24\'83\'65\'83\'58\'83\'67\'82\'c5\'82\'b7\'81\'42\cf0\b0\f1\fs20\par \cf1\b\f0\fs24\'83\'65\'83\'58\'83\'67\'82\'c5\'82\'b7\'81\'42\cf0\b0\f1\fs20\par \cf1\b\f0\fs24\'83\'65\'83\'58\'83\'67\'82\'c5\'82\'b7\'81\'42\cf0\b0\f1\fs20\par \par }

Would you please help me?

soorapadman
  • 4,451
  • 7
  • 35
  • 47
  • @Florian Schaetz: Thank you for your answer, I have also found that link, but it seems that it provides a C# solution :( – Phong Nguyen Oct 27 '15 at 07:37

2 Answers2

1

I found the solution here:

http://akigamyl.web.fc2.com/Java/RTFio/RTFio_java.html

  • Step 1: Read data and save to a temp file
  • Step 2: Read temp file as string
0

This is a bit of code I wrote for a project that had to do something similar. You'll have to test if it works, because RTF is relatively esoteric, and the webkit translator is hardly complete. But it often gets the job done.

I hope it works for you.

private static String useWebKitToConvertRtfToPlaintext(String rtf) throws IOException {
    StringReader rtfReader = new StringReader(rtf);
    JEditorPane p = new JEditorPane();
    p.setContentType("text/rtf");
    RTFEditorKit kitRtf = new RTFEditorKit();
    try {
        kitRtf.read(rtfReader, p.getDocument(), 0);
        EditorKit plainKit = p.getEditorKitForContentType("text/plain");

        Writer writer = new StringWriter();
        plainKit.write(writer, p.getDocument(), 0, p.getDocument().getLength());
        String out = writer.toString();
        return out;
    } catch (BadLocationException e) {
        e.printStackTrace();
    }

    return null;
}
willow512
  • 122
  • 1
  • 7
  • thank you for your helping, unlucky me :( it output value "ƒeƒXƒg‚Å‚·ЃBƒeƒXƒg‚Å‚·ЃB ƒeƒXƒg‚Å‚·ЃB ƒeƒXƒg‚Å‚·ЃB ƒeƒXƒg‚Å‚·ЃB ƒeƒXƒg‚Å‚·ЃB ƒeƒXƒg‚Å‚·ЃB ƒeƒXƒg‚Å‚·ЃB" It seems that incorrect encoding :( – Phong Nguyen Oct 27 '15 at 08:14