How to convert RTF to Plain Text in Java?

5.72K viewsJava
1

How to convert RTF String to Plain Text in Java?

 

Manohar Changed status to publish October 3, 2019
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);

 

Manohar Changed status to publish October 3, 2019
Add a Comment
Write your answer.