700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > java处理word公式(wmf格式转换成svg)(svg格式转换成png)

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

时间:2019-04-01 23:40:47

相关推荐

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

wmf格式转换成svg

把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,"/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(); }

svg格式转换成png

导入maven依赖

<dependency><groupId>com.kenai.nbpwr</groupId><artifactId>org-apache-batik-all</artifactId><version>1.7-03011305</version></dependency><!-- /artifact/xml-apis/xml-apis-ext --><dependency><groupId>xml-apis</groupId><artifactId>xml-apis-ext</artifactId><version>1.3.04</version></dependency><dependency><groupId>org.apache.xmlgraphics</groupId><artifactId>batik-codec</artifactId><version>1.7</version></dependency>

public class Test {/*** 将svg字符串转换为png* * @param svgCode* svg代码* @param pngFilePath* 保存的路径* @throws TranscoderException* svg代码异常* @throws IOException* io错误*/public static void convertToPng(String svgCode, String pngFilePath) throws IOException, TranscoderException {File file = new File(pngFilePath);FileOutputStream outputStream = null;try {file.createNewFile();outputStream = new FileOutputStream(file);convertToPng(svgCode, outputStream);} finally {if (outputStream != null) {try {outputStream.close();} catch (IOException e) {e.printStackTrace();}}}}/*** 将svgCode转换成png文件,直接输出到流中* * @param svgCode* svg代码* @param outputStream* 输出流* @throws TranscoderException* 异常* @throws IOException* io异常*/public static void convertToPng(String svgCode, OutputStream outputStream) throws TranscoderException, IOException {try {byte[] bytes = svgCode.getBytes("utf-8");PNGTranscoder t = new PNGTranscoder();TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(bytes));TranscoderOutput output = new TranscoderOutput(outputStream);t.transcode(input, output);outputStream.flush();} finally {if (outputStream != null) {try {outputStream.close();} catch (IOException e) {e.printStackTrace();}}}}// 读取svgpublic static String readToString(String fileName) {String encoding = "UTF-8";File file = new File(fileName);Long filelength = file.length();byte[] filecontent = new byte[filelength.intValue()];try {FileInputStream in = new FileInputStream(file);in.read(filecontent);in.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}try {return new String(filecontent, encoding);} catch (UnsupportedEncodingException e) {System.err.println("The OS does not support " + encoding);e.printStackTrace();return null;}}public static void main(String[] args) {String svgCode = readToString("C:\\Users\\Administrator\\Desktop\\1.svg");try {convertToPng(svgCode,"D:/1/1.png");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (TranscoderException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。