我需要HTML轉換爲RTF,而我使用此代碼:在Java中將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;
}
問題是,當我嘗試轉換的HTML有項目符號或編號是這樣的:
- 一個
這是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>
,這將轉換結果:
ONETWO
RTF:
{\rtf1\ansi
{\fonttbl\f0\fnil Monospaced;\f1\fnil 'Segoe UI';}
\par
\f1 one\f1 two\par \par
}
如何轉換數字/子彈?
@JimGarrison是的,我做到了!我無法找到任何解決方案,在Java代碼! – 2014-09-21 07:26:21
1.您的'\ par's關閉。 2.你想跟蹤正確的數字並將它們作爲文本插入,還是你想使用RTF列表來自動編號? (後者被稱爲'\ listlevel'。) – usr2564301 2014-09-21 09:51:28
@Jongware作爲文本! – 2014-09-21 10:00:12