在java中将HTML转换为RTF?

在java中将HTML转换为RTF?,java,html,converter,rtf,Java,Html,Converter,Rtf,我需要将HTML转换为RTF,我正在使用以下代码: private static String convertToRTF(String htmlStr) { OutputStream os = new ByteArrayOutputStream(); HTMLEditorKit htmlEditorKit = new HTMLEditorKit(); RTFEditorKit rtfEditorKit = new RTFEditorKit(); String rt

我需要将HTML转换为RTF,我正在使用以下代码:

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;
}

如何转换数字和项目符号?

这些库可能会有帮助:

  • (开源;请参阅和)
  • (免费或商业;参见和)
  • (商业;参见和)

@JimGarrison是的,我做到了!我在java代码中找不到任何解决方案!1.您的
\par
已关闭。2.您希望跟踪正确的数字并将其作为文本插入,还是希望使用RTF列表自动生成数字?(后者被称为“代码> > Listale”。然后添加处理<代码> OL<代码>(重置计数器)和<代码> LI>代码>(插入数字)。@ Jongware,您能写一个答案吗?您是否考虑过使用合适的HTML解析器?最好在这里包括答案的基本部分,并提供链接供参考。如果链接页面发生更改,则“仅链接”答案可能无效。@如果此用户未发布链接到内容在答案中有意义的文章;从本质上来说,这并不是一个只有链接的答案。我可以看看使用ApacheFop从html转换为rtf的工作示例吗?问题是之前应该使用的状态。这使得它真的很难使用。。。
<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>
{\rtf1\ansi
{\fonttbl\f0\fnil Monospaced;\f1\fnil 'Segoe UI';}

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