java处理word公式(wmf格式转换成svg)

做word文档在线预览,把word转化成html后,里面的公式的格式为.wmf格式,在浏览器中不能显示,故我把wmf格式转化成svg格式,这样就能在浏览器中显示了吐舌头

需要第三方jar包:wmf2svg-0.9.6.jar

    //wmf格式的图片转换成svg格式
    private void convert(String file,String dest) throws Exception{
        InputStream in = new FileInputStream(file);
        WmfParser parser = new WmfParser();
        final SvgGdi gdi = new SvgGdi(false);
        parser.parse(in, gdi);
        Document doc = gdi.getDocument();
        OutputStream out = new FileOutputStream(dest);
        if (dest.endsWith(".svgz")) {
            out = new GZIPOutputStream(out);
        }
        output(doc, out);
   }

   private void output(Document doc, OutputStream out) throws Exception {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,"-//W3C//DTD SVG 1.0//EN");
        transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd");
        transformer.transform(new DOMSource(doc), new StreamResult(out));
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        transformer.transform(new DOMSource(doc), new StreamResult(bos));
        out.flush();
        out.close();
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
要将WMF转换SVG并设置stroke-width,可以使用Java的Apache Batik库。Apache Batik是一个用于处理SVGJava库,它提供了将WMF转换SVG的功能。 以下是一个将WMF转换SVG并设置stroke-width的示例代码: ```java // 读取WMF文件 InputStream is = new FileInputStream("example.wmf"); // 创建转换WMFTranscoder transcoder = new WMFTranscoder(); // 设置转换参数 TranscodingHints hints = new TranscodingHints(); hints.put(ImageTranscoder.KEY_WIDTH, (float) 400); hints.put(ImageTranscoder.KEY_HEIGHT, (float) 300); transcoder.setTranscodingHints(hints); // 执行转换 TranscoderInput input = new TranscoderInput(is); ByteArrayOutputStream os = new ByteArrayOutputStream(); TranscoderOutput output = new TranscoderOutput(os); transcoder.transcode(input, output); // 将转换后的SVG字符串解析为DOM对象 String svgString = os.toString("UTF-8"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource isource = new InputSource(new StringReader(svgString)); Document doc = builder.parse(isource); // 设置stroke-width Element root = doc.getDocumentElement(); NodeList pathNodes = root.getElementsByTagName("path"); for (int i = 0; i < pathNodes.getLength(); i++) { Element path = (Element) pathNodes.item(i); path.setAttribute("stroke-width", "2"); } // 将DOM对象写入文件 Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(new DOMSource(doc), new StreamResult(new File("example.svg"))); ``` 在上面的代码中,首先读取WMF文件并创建WMFTranscoder对象。然后设置转换参数并执行转换,将转换后的SVG字符串解析为DOM对象。接着,遍历所有的path元素并设置stroke-width属性的值为2。最后,将DOM对象写入SVG文件。 注意:在上面的代码中,设置的stroke-width值为2,可以根据需要进行修改。另外,还需要在代码中添加异常处理

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值