Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RTF to Plain Text in Java

Tags:

java

rtf

How do you convert an RTF string to plain text in Java? The obvious answer is to use Swing's RTFEditorKit, and that seems to be the common answer around the Internet. However the write method that claims to return plain text isn't actually implemented... it's hard-coded to just throw an IOException in Java6.

like image 657
edm3 Avatar asked Apr 28 '11 22:04

edm3


1 Answers

I use Swing's RTFEditorKit in Java 6 like this:

RTFEditorKit rtfParser = new RTFEditorKit();
Document document = rtfParser.createDefaultDocument();
rtfParser.read(new ByteArrayInputStream(rtfBytes), document, 0);
String text = document.getText(0, document.getLength());

and thats working.

like image 84
morja Avatar answered Oct 13 '22 00:10

morja