Convert HTML to RTF in java?

本小妞迷上赌 提交于 2019-12-22 10:53:51

问题


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?


回答1:


These libraries might be helpful:

  • Apache FOP (open-source; see basic usage and API docs)
  • PD4ML (free or commercial; see HTML2RTF and examples)
  • Aspose.Words for Java (commercial; see docs and API)


来源:https://stackoverflow.com/questions/25956775/convert-html-to-rtf-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!