gpt4 book ai didi

java - 在java中将HTML转换为RTF?

转载 作者:可可西里 更新时间:2023-11-01 13:32:40 25 4
gpt4 key购买 nike

我需要将 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 时:

  1. one
  2. two

这是 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
}

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

最佳答案

这些库可能会有帮助:

关于java - 在java中将HTML转换为RTF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25956775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com