how to convert a vsd/vsdx file to image(eg. jpg png) using apache poi

314 views Asked by At

I'm using apache poi to read doc/docx files.

Now I can extract paragraphs and pictures from my doc file.

When there is a vsd in my doc file, how can i convert the vsd to a png image?

I tried this:

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; 
    } 

But the picture i got did not contain the text, like this: enter image description here

Does anybody knows how I can do this?

0

There are 0 answers