Reputation: 1141
I tried to read rtf file using Apache POI but I found issues with it. It reports Invalid Header exception. It seems like POI doesn't support rtf files. Is there any way to read .rtf using any open source java API. (I heard about Aspose API but it's not free)
Any solutions??
Upvotes: 1
Views: 19719
Reputation: 2017
You can try the RTFEditorKit. It supports images and text as well.
Or look at this answer: Java API to convert RTF file to Word document (97-2003 format)
There is no free library that supports this. But it may not be that hard to create a basic compare function yourself. You can read in an rtf file and then extract the text like this:
// read rtf from file
JEditorPane p = new JEditorPane();
p.setContentType("text/rtf");
EditorKit rtfKit = p.getEditorKitForContentType("text/rtf");
rtfKit.read(new FileReader(fileName), p.getDocument(), 0);
rtfKit = null;
// convert to text
EditorKit txtKit = p.getEditorKitForContentType("text/plain");
Writer writer = new StringWriter();
txtKit.write(writer, p.getDocument(), 0, p.getDocument().getLength());
String documentText = writer.toString();
Upvotes: 6