开发者社区> 问答> 正文

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

通过java swing将html转换为java中的html到word doc并没有太大帮助,所以寻找开源库

展开
收起
垚tutu 2019-11-29 23:13:47 1734 0
1 条回答
写回答
取消 提交回答
  • #include

    通过查看标题,您只需要从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);
        }
    }
    }
    
    2019-11-29 23:14:11
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载