如何在java中将rft字符串转换为纯文本?

时间:2015-10-27 07:30:11

标签: java

我一直在寻找在java中将RTF字符串转换为纯文本的方法,但遗憾的是我无法找到解决方案。

我需要将此RTF字符串转换为纯文本

  

{\ RTF1 \ ANSI \ ansicpg932 \ deff0 \ deflang1033 \ deflangfe1041 {\ fonttbl {\ F0 \ fnil \ fcharset128   \ '82 \'6c \ '82 \ '72 \'96 \'是\'92 \'a9;} {\ f1 \ fnil \ fcharset128 MS UI   Gothic;}} {\ colortbl; \ red0 \ green128 \ blue128;}   \ viewkind4 \ UC1 \ PARD \ CF1 \ lang1041 \ B \ F0 \ FS24 \ '83 \ '65 \ '83 \ '58 \ '83 \ '67 \ '82 \ 'C5 \ '82 \' B7 \ '81 \ '42 \ '83 \ '65 \ '83 \ '58 \ '83 \ '67 \ '82 \ 'C5 \ '82 \' B7 \ '81 \ '42 \ CF0 \ B0 \ F1 \ FS20 \帕   \ CF1 \ B \ F0 \ FS24 \ '83 \ '65 \ '83 \ '58 \ '83 \ '67 \ '82 \ 'C5 \ '82 \' B7 \ '81 \ '42 \ CF0 \ B0 \ F1 \ FS20 \看齐   \ CF1 \ B \ F0 \ FS24 \ '83 \ '65 \ '83 \ '58 \ '83 \ '67 \ '82 \ 'C5 \ '82 \' B7 \ '81 \ '42 \ CF0 \ B0 \ F1 \ FS20 \看齐   \ CF1 \ B \ F0 \ FS24 \ '83 \ '65 \ '83 \ '58 \ '83 \ '67 \ '82 \ 'C5 \ '82 \' B7 \ '81 \ '42 \ CF0 \ B0 \ F1 \ FS20 \看齐   \ CF1 \ B \ F0 \ FS24 \ '83 \ '65 \ '83 \ '58 \ '83 \ '67 \ '82 \ 'C5 \ '82 \' B7 \ '81 \ '42 \ CF0 \ B0 \ F1 \ FS20 \看齐   \ CF1 \ B \ F0 \ FS24 \ '83 \ '65 \ '83 \ '58 \ '83 \ '67 \ '82 \ 'C5 \ '82 \' B7 \ '81 \ '42 \ CF0 \ B0 \ F1 \ FS20 \看齐   \ CF1 \ B \ F0 \ FS24 \ '83 \ '65 \ '83 \ '58 \ '83 \ '67 \ '82 \ 'C5 \ '82 \' B7 \ '81 \ '42 \ CF0 \ B0 \ F1 \ FS20 \看齐   \ par}

你能帮帮我吗?

2 个答案:

答案 0 :(得分:1)

我在这里找到了解决方案:

http://akigamyl.web.fc2.com/Java/RTFio/RTFio_java.html

  • 步骤1:读取数据并保存到临时文件
  • 步骤2:将临时文件读为字符串

答案 1 :(得分:0)

这是我为一个必须做类似事情的项目编写的一些代码。您必须测试它是否有效,因为RTF相对深奥,并且webkit转换器很难完成。但它经常完成工作。

我希望它适合你。

private static String useWebKitToConvertRtfToPlaintext(String rtf) throws IOException {
    StringReader rtfReader = new StringReader(rtf);
    JEditorPane p = new JEditorPane();
    p.setContentType("text/rtf");
    RTFEditorKit kitRtf = new RTFEditorKit();
    try {
        kitRtf.read(rtfReader, p.getDocument(), 0);
        EditorKit plainKit = p.getEditorKitForContentType("text/plain");

        Writer writer = new StringWriter();
        plainKit.write(writer, p.getDocument(), 0, p.getDocument().getLength());
        String out = writer.toString();
        return out;
    } catch (BadLocationException e) {
        e.printStackTrace();
    }

    return null;
}