什么是将html转换为Java中的RTF的最佳方法

问题描述 投票:-1回答:1

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

java servlets struts
1个回答
0
投票

通过查看标题,您只需从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);
    }
}
}

希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.