我可以使用 JEditorPane 来解析 rtf 文本并将其转换为 html。但是 html 输出缺少某种格式,即本例中的删除线标记。正如您在输出中看到的那样,下划线文本正确地包裹在 <u> 中,但没有删除线包裹。任何想法?
public void testRtfToHtml()
{
JEditorPane pane = new JEditorPane();
pane.setContentType("text/rtf");
StyledEditorKit kitRtf = (StyledEditorKit) pane.getEditorKitForContentType("text/rtf");
try
{
kitRtf.read(
new StringReader(
"{\\rtf1\\ansi \\deflang1033\\deff0{\\fonttbl {\\f0\\froman \\fcharset0 \\fprq2 Times New Roman;}}{\\colortbl;\\red0\\green0\\blue0;} {\\stylesheet{\\fs20 \\snext0 Normal;}} {\\plain \\fs26 \\strike\\fs26 This is supposed to be strike-through.}{\\plain \\fs26 \\fs26 } {\\plain \\fs26 \\ul\\fs26 Underline text here} {\\plain \\fs26 \\fs26 .{\\u698\\'20}}"),
pane.getDocument(), 0);
kitRtf = null;
StyledEditorKit kitHtml =
(StyledEditorKit) pane.getEditorKitForContentType("text/html");
Writer writer = new StringWriter();
kitHtml.write(writer, pane.getDocument(), 0, pane.getDocument().getLength());
System.out.println(writer.toString());
}
catch (Exception e)
{
e.printStackTrace();
}
}
输出:
<html>
<head>
<style>
<!--
p.Normal {
RightIndent:0.0;
FirstLineIndent:0.0;
LeftIndent:0.0;
}
-->
</style>
</head>
<body>
<p class=default>
<span style="color: #000000; font-size: 13pt; font-family: Times New Roman">
This is supposed to be strike-through.
</span>
<span style="color: #000000; font-size: 13pt; font-family: Times New Roman">
</span>
<span style="color: #000000; font-size: 13pt; font-family: Times New Roman">
<u>Underline text here</u>
</span>
<span style="color: #000000; font-size: 13pt; font-family: Times New Roman">
.?
</span>
</p>
</body>
</html>