How to convert RTF to Plain Text in Java?

3.39K viewsJava
1

How to convert RTF String to Plain Text in Java?

 

Changed status to publish
Add a Comment
1

Use below code :

import javax.swing.text.Document;
import javax.swing.text.rtf.RTFEditorKit;

String rtfString = "e1xydGYxXGZiaWRpc1xhbnNpXGFuc2ljcGcxMjUyXGRlZmYwXGRlZmxhbmcxMDMze1xmb250dGJse1xmMFxmcm9tYW5cZnBycTJcZmNoYXJzZXQwIFRpbWVzIE5ldyBSb21hbjt9e1xmMVxmbmlsXGZjaGFyc2V0MTc3IE1pY3Jvc29mdCBTYW5zIFNlcmlmO319DQp7XGNvbG9ydGJsIDtccmVkMjU1XGdyZWVuMFxibHVlMDtccmVkMFxncmVlbjBcYmx1ZTA7fQ0KXHZpZXdraW5kNFx1YzFccGFyZFxsdHJwYXJcY2YxXGYwXGZzMjIgWW91IGhhdmUgY2hvc2VuIHRvIGJvb2sgYSBTcGljZU1heCBmYXJlLiBUaGlzIGZhcmUgaXMgXGNmMlxiIGVudGl0bGVzIHlvdSBhIGhvc3Qgb2YgcHJpdmlsZWdlcyBsaWtlIGV4dHJhIGxlZyByb29tIGNvbWZvcnRhYmxlIHNlYXQsIGNvbXBsaW1lbnRhcnkgbWVhbHMsIEluLWZsaWdodCBlbnRlcnRhaW5tZW50LCBwcmlvcml0eSBwcml2aWxlZ2VzIGFuZCBtb3JlXGNmMVxiMCAuIENhbmNlbGxhdGlvbnMgYXJlIHBlcm1pdHRlZCBhdCBhcHBsaWNhYmxlIGNoYXJnZXMuXGNmMFxwYXINClxwYXJkXGx0cnBhclxmMVxydGxjaFxmczE3XHBhcg0KfQ0K";
		byte[] b = rtfString .getBytes();
		RTFEditorKit rtfParser = new RTFEditorKit();
		Document document = rtfParser.createDefaultDocument();
		rtfParser.read(new ByteArrayInputStream(rtfString), document, 0);
		String text = document.getText(0, document.getLength());
		System.out.println(text);

 

Changed status to publish
Add a Comment
Write your answer.