我的数据库中有一个包含 RTF 格式文本的列。如何使用 Java 仅获取它的纯文本?
问问题
11111 次
4 回答
1
RTFEditorKit rtfParser = new RTFEditorKit();
Document document = rtfParser.createDefaultDocument();
rtfParser.read(new ByteArrayInputStream(rtfBytes), document, 0);
String text = document.getText(0, document.getLength());
这应该工作
于 2017-04-07T20:10:36.490 回答
0
如果您可以尝试“AdvancedRTFEditorKit”,它可能会很酷。试试这里http://java-sl.com/advanced_rtf_editor_kit.html
我已经用它创建了一个完整的 RTF 编辑器,具有 MS Word 的所有支持。
于 2012-08-08T15:13:29.037 回答
0
Apache POI 还将读取 Microsoft Word 格式,而不仅仅是 RTF。
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
public String getRtfText(String fileName) {
File rtfFile = null;
WordExtractor rtfExtractor = null ;
try {
rtfFile = new File(fileName);
//A FileInputStream obtains input bytes from a file.
FileInputStream inStream = new FileInputStream(rtfFile.getAbsolutePath());
//A HWPFDocument used to read document file from FileInputStream
HWPFDocument doc=new HWPFDocument(inStream);
rtfExtractor = new WordExtractor(doc);
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
//This Array stores each line from the document file.
String [] rtfArray = rtfExtractor.getParagraphText();
String rtfString = "";
for(int i=0; i < rtfArray.length; i++) rtfString += rtfArray[i];
System.out.println(rtfString);
return rtfString;
}
于 2012-08-08T15:27:28.913 回答
0
如果 RTF 文本位于 JEditorPane 中,则此方法有效
String s = getPlainText(aJEditorPane.getDocument());
String getPlainText(Document doc) {
try {
return doc.getText(0, doc.getLength());
}
catch (BadLocationException ex) {
System.err.println(ex);
return null;
}
}
于 2017-11-22T11:03:44.780 回答