问题
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:
- one
- 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