J Eng
J Eng

Reputation: 49

Rich Text Format to Plain Text

I am trying to convert RTF to PTF using Swing's RTFEditorKit. This is my code.

Example RTF content is -

<p>4th Review Information for Bug Id DRRaa50524 </p>
 <h5 style="text-align: center;">4th Review Information for Bug Id DRRaa50524</h5>
 <h2 style="text-align: right;">4th Review Information for Bug Id DRRaa50524</h2>

But I am getting document length as 0 and text as empty. What is wrong with my code?

public String RTFToPTConverter(String des) throws Exception {
    RTFEditorKit rtfParser = new RTFEditorKit();
    Document document = rtfParser.createDefaultDocument();
    rtfParser.read(new ByteArrayInputStream(des.getBytes()), document, 0);
    System.out.println(document.getLength());
    String text = document.getText(0, document.getLength());
    System.out.println(text);
    return text;
}

Upvotes: 0

Views: 1538

Answers (1)

Joni
Joni

Reputation: 111219

Your text is HTML, not RTF. Use HTMLEditorKit to work with it.

HTMLEditorKit htmlParser = new HTMLEditorKit();
Document document = htmlParser.createDefaultDocument();
htmlParser.read(new ByteArrayInputStream(des.getBytes()), document, 0);

Upvotes: 4

Related Questions