0

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

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

当我的 doc 文件中有 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; 
    } 

但我得到的图片不包含文字,像这样: 在此处输入图像描述

有人知道我该怎么做吗?

4

0 回答 0