【问题标题】:Convert HTML to RTF in java?在java中将HTML转换为RTF?
【发布时间】:2014-11-15 09:32:40
【问题描述】:

我需要将 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. 一个
  2. 两个

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

这是转换结果:

两个

RTF:

{\rtf1\ansi
{\fonttbl\f0\fnil Monospaced;\f1\fnil 'Segoe UI';}

\par
\f1 one\f1 two\par \par
}

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

【问题讨论】:

  • @JimGarrison 是的,我做到了!我在java代码中找不到任何解决方案!
  • 1.您的 \pars 已关闭。 2. 您想跟踪正确的数字并将它们作为文本插入,还是想使用 RTF 列表来获得自动数字? (后者称为\listlevel。)
  • 然后为ol(重置计数器)和li(插入数字)添加处理。
  • @Jongware 你能写一个答案吗?
  • 您是否考虑过使用合适的 HTML 解析器,例如 jsoup

标签: java html converter rtf


【解决方案1】:

这些库可能会有所帮助:

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
  • @ElectricCoffee 该用户没有发布指向在答案中有意义的文章的链接;这不是一个仅链接的答案,本身。
  • 我可以看到使用 Apache FOP 从 html 转换为 rtf 的工作示例吗?
  • 问题是FAQ of Apache FOP 声明应该在之前使用html2fo。这使得它真的很难使用......
猜你喜欢
  • 1970-01-01
  • 2012-02-17
  • 2014-04-18
  • 1970-01-01
  • 2010-10-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-28
  • 1970-01-01
相关资源
最近更新 更多