3

I can use JEditorPane to parse the rtf text and convert it to html. But the html output is missing some format, namely the strike-through markups in this case. As you can see in the output, underline text was correctly wrapped within <u> but there is no strike-through wrapper. Any idea?

public void testRtfToHtml()
{
    JEditorPane pane = new JEditorPane();
    pane.setContentType("text/rtf");

    StyledEditorKit kitRtf = (StyledEditorKit) pane.getEditorKitForContentType("text/rtf");

    try
    {
        kitRtf.read(
            new StringReader(
                "{\\rtf1\\ansi \\deflang1033\\deff0{\\fonttbl {\\f0\\froman \\fcharset0 \\fprq2 Times New Roman;}}{\\colortbl;\\red0\\green0\\blue0;} {\\stylesheet{\\fs20 \\snext0 Normal;}} {\\plain \\fs26 \\strike\\fs26 This is supposed to be strike-through.}{\\plain \\fs26 \\fs26  } {\\plain \\fs26 \\ul\\fs26 Underline text here} {\\plain \\fs26 \\fs26 .{\\u698\\'20}}"),
            pane.getDocument(), 0);
        kitRtf = null;

        StyledEditorKit kitHtml =
            (StyledEditorKit) pane.getEditorKitForContentType("text/html");

        Writer writer = new StringWriter();
        kitHtml.write(writer, pane.getDocument(), 0, pane.getDocument().getLength());
        System.out.println(writer.toString());
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

Output:

<html>
  <head>
    <style>
      <!--
        p.Normal {
          RightIndent:0.0;
          FirstLineIndent:0.0;
          LeftIndent:0.0;
        }
      -->
    </style>
  </head>
  <body>
    <p class=default>
              <span style="color: #000000; font-size: 13pt; font-family: Times New Roman">
This is supposed to be strike-through.
      </span>
      <span style="color: #000000; font-size: 13pt; font-family: Times New Roman">

      </span>
       <span style="color: #000000; font-size: 13pt; font-family: Times New Roman">
<u>Underline text here</u>
      </span>
       <span style="color: #000000; font-size: 13pt; font-family: Times New Roman">
.?
      </span>

    </p>
  </body>
</html>
dcc
  • 31
  • 1
  • 2

2 Answers2

3

You could try converting with OpenOffice or LibreOffice using this converter library as described in this blog post

lfurini
  • 3,420
  • 4
  • 27
  • 40
Mike B
  • 5,270
  • 2
  • 19
  • 43
0

Here is a function I'm using to convert RTF to HTML from a .msg body. See my Outlook message parser yamp repository on GitHub.

public static String rtfToHtml(String rtfText) {
    if (rtfText != null) {
        rtfText = rtfText.replaceAll("\\{\\\\\\*\\\\[m]?htmltag[\\d]*(.*)}", "$1")
            .replaceAll("\\\\htmlrtf[1]?(.*)\\\\htmlrtf0", "")
            .replaceAll("\\\\htmlrtf[01]?", "")
            .replaceAll("\\\\htmlbase", "")
            .replaceAll("\\\\par", "\n")
            .replaceAll("\\\\tab", "\t")
            .replaceAll("\\\\line", "\n")
            .replaceAll("\\\\page", "\n\n")
            .replaceAll("\\\\sect", "\n\n")
            .replaceAll("\\\\emdash", "&#2014;")
            .replaceAll("\\\\endash", "&#2013;")
            .replaceAll("\\\\emspace", "&#2003;")
            .replaceAll("\\\\enspace", "&#2002;")
            .replaceAll("\\\\qmspace", "&#2005;")
            .replaceAll("\\\\bullet", "&#2022;")
            .replaceAll("\\\\lquote", "&#2018;")
            .replaceAll("\\\\rquote", "&#2019;")
            .replaceAll("\\\\ldblquote", "&#201C;")
            .replaceAll("\\\\rdblquote", "&#201D;")
            .replaceAll("\\\\row", "\n")
            .replaceAll("\\\\cell", "|")
            .replaceAll("\\\\nestcell", "|")
            .replaceAll("([^\\\\])\\{", "$1")
            .replaceAll("([^\\\\])}", "$1")
            .replaceAll("[\\\\](\\{)", "$1")
            .replaceAll("[\\\\](})", "$1")
            .replaceAll("\\\\u([0-9]{2,5})", "&#$1;")
            .replaceAll("\\\\'([0-9A-Fa-f]{2})", "&#x$1;")
            .replaceAll("\"cid:(.*)@.*\"", "\"$1\"");

        int index = rtfText.indexOf("<html");
        if (index != -1) {
            return rtfText.substring(index);
        }
    }

    return null;
}