linux下poi把word转图片,我需要将Apache POI图片从Word文档转换为html文件

我有一些代码使用Java Apache POI库打开一个Microsoft Word文档,并使用Apache

POI将其转换为html,它还会获取文档上图像的字节数组数据。但是我需要将此信息转换为html才能写出为html文件。任何提示或建议,将不胜感激。请记住,我是台式机开发人员而不是Web程序员,因此,当您提出建议时,请记住这一点。下面的代码获取图像。

private void parseWordText(File file) throws IOException {

FileInputStream fs = new FileInputStream(file);

doc = new HWPFDocument(fs);

PicturesTable picTable = doc.getPicturesTable();

if (picTable != null){

picList = new ArrayList(picTable.getAllPictures());

if (!picList.isEmpty()) {

for (Picture pic : picList) {

byte[] byteArray = pic.getContent();

pic.suggestFileExtension();

pic.suggestFullFileName();

pic.suggestPictureType();

pic.getStartOffset();

}

}

}

然后,下面的代码将文档转换为html。有没有办法在下面的代码中将byteArray添加到ByteArrayOutputStream中?

private void convertWordDoctoHTML(File file) throws ParserConfigurationException, TransformerConfigurationException, TransformerException, IOException {

HWPFDocumentCore wordDocument = null;

try {

wordDocument = WordToHtmlUtils.loadDoc(new FileInputStream(file));

} catch (IOException ex) {

Exceptions.printStackTrace(ex);

}

WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());

wordToHtmlConverter.processDocument(wordDocument);

org.w3c.dom.Document htmlDocument = wordToHtmlConverter.getDocument();

NamedNodeMap node = htmlDocument.getAttributes();

ByteArrayOutputStream out = new ByteArrayOutputStream();

DOMSource domSource = new DOMSource(htmlDocument);

StreamResult streamResult = new StreamResult(out);

TransformerFactory tf = TransformerFactory.newInstance();

Transformer serializer = tf.newTransformer();

serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

serializer.setOutputProperty(OutputKeys.INDENT, "yes");

serializer.setOutputProperty(OutputKeys.METHOD, "html");

serializer.transform(domSource, streamResult);

out.close();

String result = new String(out.toByteArray());

acDocTextArea.setText(newDocText);

htmlText = result;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值