如何使用Apache POI将vsd / vsdx文件转换为图像(例如jpg png)

时间:2018-11-12 09:32:03

标签: java apache-poi xwpf hwpf

我正在使用 apache poi 来读取doc / docx文件。

现在我可以从文档文件中提取段落和图片了。

我的文档文件中有vsd时,如何将vsd转换为png图像?

我尝试过:

private byte[] emfConversionPng(DocPictureData pictureData) { 

            EMFRenderer emfRenderer = null; 
            InputStream iStream = new ByteArrayInputStream(pictureData.getContent()); 
            EMFInputStream emfInputStream = null; 
            ByteArrayOutputStream baos = null; 
            ImageOutputStream imageOutputStream = null; 

            byte[] by = null; 

            try { 
                    emfInputStream = new EMFInputStream(iStream, EMFInputStream.DEFAULT_VERSION); 
                    emfRenderer = new EMFRenderer(emfInputStream); 

                    int width = (int) emfInputStream.readHeader().getBounds().getWidth(); 
                    int height = (int) emfInputStream.readHeader().getBounds().getHeight(); 

                    BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); 
                    Graphics2D graphics2d = result.createGraphics(); 

                    emfRenderer.paint(graphics2d); 


                    baos = new ByteArrayOutputStream(); 
                    imageOutputStream = ImageIO.createImageOutputStream(baos); 
                    ImageIO.write(result, "png", imageOutputStream); 

                    by = baos.toByteArray(); 
            } catch (IOException e) { 
                    // TODO Auto-generated catch block 
                    e.printStackTrace(); 
            } finally { 
                    try { 
                            if (imageOutputStream != null) { 
                                    imageOutputStream.close(); 
                            } 
                            if (baos != null) { 
                                    baos.close(); 
                            } 
                            if (emfRenderer != null) { 
                                    emfRenderer.closeFigure(); 
                            } 
                    } catch (IOException e) { 
                            // TODO Auto-generated catch block 
                            e.printStackTrace(); 
                    } 

            } 

            return by; 
    } 

但是我得到的图片没有文字,像这样: enter image description here

有人知道我该怎么做吗?

0 个答案:

没有答案