0

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?

Bsquare ℬℬ
  • 4,423
  • 11
  • 24
  • 44
赵祥宇
  • 497
  • 3
  • 9
  • 19
  • How is this related to `apache poi`? I cannot see any kind of `apache poi` class. What is `DocPictureData`? What is `EMFRenderer`? What is `EMFInputStream`? And where are the `*.vsd*` files coming from? An how are they coming into the `*.doc*` files? – Axel Richter Nov 12 '18 at 10:20
  • Looks like you are using FreeHep for converting your EMF stream - save it to a file and verify its text content on MS Paint. I'm currently working on an [EMF renderer for POI](http://svn.apache.org/viewvc/poi/branches/hemf/src/scratchpad/src/org/apache/poi/hemf/usermodel/HemfPicture.java?view=markup) but this has also still some rendering problems and lacking EMF+ support :( but you could try out the branch: see [HemfPictureTest::paint()](http://svn.apache.org/viewvc/poi/branches/hemf/src/scratchpad/testcases/org/apache/poi/hemf/usermodel/HemfPictureTest.java?view=markup) for the invocation – kiwiwings Nov 12 '18 at 21:48
  • @ Axel Richter:thank you for your response, actually i use HWPFDocument doc=new HWPFDocument(inputStream) to read a doc file, and then use doc.getPicturesTable() to get all pictures in the doc file,then i found vsd files that i inserted into the doc file will be readed as picture with a "emf" extension, so i try to use EMFInputStream and EMFRenderer to get the picture. what i want is to convent a vsd to png image when i use poi reading doc files, do you konw how to do this? – 赵祥宇 Nov 13 '18 at 00:28
  • @kiwiwings:thank you for your response , your links are forbidden for me :) (You don't have permission to access /viewvc/poi/branche...) – 赵祥宇 Nov 13 '18 at 00:57

0 Answers0