简体   繁体   中英

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;
}

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM