简体   繁体   中英

Convert HTML to RTF in java?

I need to convert HTML to RTF, and I am using this code:

private static String convertToRTF(String htmlStr) {
    OutputStream os = new ByteArrayOutputStream();
    HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
    RTFEditorKit rtfEditorKit = new RTFEditorKit();
    String rtfStr = null;
    htmlStr = htmlStr.replaceAll("<br.*?>", "#NEW_LINE#");
    htmlStr = htmlStr.replaceAll("</p>", "#NEW_LINE#");
    htmlStr = htmlStr.replaceAll("<p.*?>", "");
    InputStream is = new ByteArrayInputStream(htmlStr.getBytes());
    try {
        Document doc = htmlEditorKit.createDefaultDocument();
        htmlEditorKit.read(is, doc, 0);
        rtfEditorKit.write(os, doc, 0, doc.getLength());
        rtfStr = os.toString();
        rtfStr = rtfStr.replaceAll("#NEW_LINE#", "\\\\par ");
    } catch (IOException e) {
        e.printStackTrace();
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
    return rtfStr;
}

The problem is when I try to convert HTML that had bullets or numbers like this:

  1. one
  2. two

This is the HTML:

<html><head>
    <style>
      <!--
      -->
    </style>
  </head>
  <body contenteditable="true">
     <p style="text-align: left;">
         <ol>
             <li><font face="'Segoe UI'">one</font></li>
             <li><font face="'Segoe UI'">two</font></li>
         </ol>
   </p>

And this the convert result:

onetwo

RTF:

{\rtf1\ansi
{\fonttbl\f0\fnil Monospaced;\f1\fnil 'Segoe UI';}

\par
\f1 one\f1 two\par \par
}

How can I convert the numbers and bullets?

These libraries might be helpful:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM