我正在使用 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;
}
有人知道我该怎么做吗?