java处理word公式(wmf格式转换成svg)(svg格式转换成png)
2018-02-10 14:46
543 查看
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,"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();
}
svg格式转换成png
导入maven依赖<dependency><groupId>com.kenai.nbpwr</groupId>
<artifactId>org-apache-batik-all</artifactId>
<version>1.7-201003011305</version>
</dependency>
<!-- https://mvnrepository.com/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(); } } } } // 读取svg public 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 block e.printStackTrace(); } catch (TranscoderException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
相关文章推荐
- java处理word公式(wmf格式转换成svg)
- json数据与JAVA数据的转换 jsonJavaBean.netApache 自己编写了一个工具类,处理页面提交json格式数据到后台,再进行处理成JAVA对象数据 1、DTO:Data T
- JAVA:借用OpenOffice将上传的Word文档转换成Html格式
- 详解 Java中日期数据类型的处理之格式转换的实例
- java中使用libreoffice将word转换成pdf格式
- java通过jacob来读取word转换为htm格式
- Java 将数据处理成 PDF 、Excel、Word格式
- java 调用OpenOffice将word等格式文件转换为pdf格式
- 用java将png图片转换成jpg格式的图片
- Java时间处理 将时间戳转换为“几天前”的格式
- java wmf格式图片转png
- java 调用OpenOffice将word格式文件转换为pdf格式
- JAVA:借用OpenOffice将上传的Word文档转换成Html格式
- JAVA:借用OpenOffice将上传的Word文档转换成Html格式
- fastJson在java后台转换json格式数据探究(二)--处理数组/List/Map
- JAVA:借用OpenOffice将上传的Word文档转换成Html格式
- java 处理word 与html转换 [undone][hanged]
- java通过jacob来读取word转换为htm格式
- [javase][json]fastJson在java后台转换json格式数据探究--处理数组/List/Map
- fastJson在java后台转换json格式数据探究(二)--处理数组/List/Map