java wmf转svg图片格式最好的方法wmfToSvg

java | 2019-09-13 10:02:39

java wmf转svg图片格式最好的方法


第一种方法 这个方法兼容性比较好

public static void wmfToSvg(String src, String dest) {  
        boolean compatible = false;  
            try {
                                InputStream in = new FileInputStream(src);  
                                WmfParser parser = new WmfParser();  
                                final SvgGdi gdi = new SvgGdi(compatible);  
                                parser.parse(in, gdi);  
  
                                Document doc = gdi.getDocument();  
                                OutputStream out = new FileOutputStream(dest);  
                                if (dest.endsWith(".svgz")) {  
                                    out = new GZIPOutputStream(out);  
                                } 
                                
                                doc.getFirstChild().getAttributes().removeNamedItem("height");
                                doc.getFirstChild().getAttributes().removeNamedItem("width");
                                
                                output(doc, out);
                                
                                in.close();
                        } catch (FileNotFoundException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        } catch (SvgGdiException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        } catch (DOMException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        } catch (WmfParseException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }  
        
    }  
  
 
    private static 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));  
        out.flush();  
        out.close();  
        out = null;  
    }


第二种方法很简单

WMFTranscoder wmfTranscoder = new WMFTranscoder();
                           TranscoderInput wmfInput = new TranscoderInput(new File(fileName).toURI().toString());
                           FileOutputStream svgFos = new FileOutputStream(fileName+".svg");
                           TranscoderOutput svgOutput = new TranscoderOutput(new OutputStreamWriter(svgFos,"utf-8"));
                           wmfTranscoder.addTranscodingHint(WMFTranscoder.KEY_WIDTH, new Float(1000));
                           wmfTranscoder.transcode(wmfInput, svgOutput);
                           svgFos.flush();
                           svgFos.close();


登录后即可回复 登录 | 注册
    
关注编程学问公众号