【问题标题】:what is the best way to convert html to RTF in java by opensource java library [duplicate]通过开源java库将html转换为java中的RTF的最佳方法是什么[重复]
【发布时间】:2019-11-29 09:20:01
【问题描述】:

通过java swing在java中将html转换为rtf没有多大帮助,所以寻找开源库

【问题讨论】:

  • 为什么你的问题正文说“word doc”,而你的标题却是 RTF?
  • 我有两个选项可以直接将 html 转换为 word doc 或将 html 转换为 rtf 然后将其保存为 word doc。错误地我在 body.i 中添加了 word doc。

标签: java


【解决方案1】:

通过查看您的标题,您只需要从 HTML 页面获取 RTF。 因此,您可以使用 PD4ML 库。 首先将其添加到您的 pom 中

<dependency>
      <groupId>com.pd4ml</groupId>
      <artifactId>pd4ml</artifactId>
      <version>4.0.2</version>
    </dependency>

例子

public class Converter {
protected Dimension format = PD4Constants.A4;
protected boolean landscapeValue = false;
protected int topValue = 10;
protected int leftValue = 10;
protected int rightValue = 10;
protected int bottomValue = 10;
protected String unitsValue = "mm";
protected String proxyHost = "";
protected int proxyPort = 0;
protected int userSpaceWidth = 780;

private void runConverter(String urlstring, File output) throws IOException {
    if (urlstring.length() > 0) {
        if (!urlstring.startsWith("http://") && !urlstring.startsWith("file:")) {
            urlstring = "http://" + urlstring;
        }
        java.io.FileOutputStream fos = new java.io.FileOutputStream(output);
        if (proxyHost != null && proxyHost.length() != 0 && proxyPort != 0) {
            System.getProperties().setProperty("proxySet", "true");
            System.getProperties().setProperty("proxyHost", proxyHost);
            System.getProperties().setProperty("proxyPort", "" + proxyPort);
        }
        PD4ML pd4ml = new PD4ML();
        try {
            pd4ml.setPageSize(landscapeValue ? pd4ml.changePageOrientation(format) : format);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (unitsValue.equals("mm")) {
            pd4ml.setPageInsetsMM(new Insets(topValue, leftValue, bottomValue, rightValue));
        } else {
            pd4ml.setPageInsets(new Insets(topValue, leftValue, bottomValue, rightValue));
        }
        pd4ml.setHtmlWidth(userSpaceWidth);
        pd4ml.render(urlstring, fos);
    }
}
}

希望这会有所帮助。

【讨论】:

  • PD4ML 是开源的吗?我可以将它嵌入到我的 java 代码中吗,因为我不允许使用其他库。
  • 绝对可以使用这个依赖。只需先尝试示例项目。
猜你喜欢
  • 2011-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多