我一直在寻找在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}
你能帮帮我吗?
答案 0 :(得分:1)
答案 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;
}