Click here to Skip to main content
14,451,737 members

Java: How to convert RTF into HTML

Rate this:
4.91 (9 votes)
Please Sign up or sign in to vote.
4.91 (9 votes)
19 Dec 2010CPOL
This small piece of code shows how one can use Java standard libs (non-third party) to convert a RTF text into HTML.
I came up with the following solution of converting a RTF text into HTML format without dealing with third party libraries (which usually not free):
public static String rtfToHtml(Reader rtf) throws IOException {
    JEditorPane p = new JEditorPane();
    EditorKit kitRtf = p.getEditorKitForContentType("text/rtf");
    try {, p.getDocument(), 0);
        kitRtf = null;
        EditorKit kitHtml = p.getEditorKitForContentType("text/html");
        Writer writer = new StringWriter();
        kitHtml.write(writer, p.getDocument(), 0, p.getDocument().getLength());
        return writer.toString();
    } catch (BadLocationException e) {
    return null;

For example, if your RTF is stored in a String variable, you can use the function like this:
String rtfText = ...;
String htmlText = rtfToHtml(new StringReader(rtfText));

And if it's in a file, you can do something like this:
String htmlText = rtfToHtml(new FileReader(new File("myfile.rtf")));


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Arman S.
Software Developer 13
United States United States
No Biography provided

Comments and Discussions

Questionto sucessfully test this code.. Pin
lupus5117-Sep-12 7:54
Memberlupus5117-Sep-12 7:54 
Questionhow to convert html to rtf Pin
Deepika Rani C23-Jul-12 23:57
MemberDeepika Rani C23-Jul-12 23:57 
GeneralHow to convert HTML into RTF Pin
sovannoty29-Nov-11 1:23
Membersovannoty29-Nov-11 1:23 
GeneralGood Work Pin
Abdul Quader Mamun16-Dec-10 9:39
MemberAbdul Quader Mamun16-Dec-10 9:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 14 Dec 2010

Tagged as


7 bookmarked