XHTMLConverter不生成图像

XHTMLConverter不生成图像

本文介绍了org.apache.poi.xwpf.converter.xhtml.XHTMLConverter不生成图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用org.apache.poi.xwpf.converter.xhtml.XHTMLConverter类将docx转换为html。下面是我的groovy代码

I am using org.apache.poi.xwpf.converter.xhtml.XHTMLConverter class to convert docx to html. Below is my groovy code

public Map convert(String wordDocPath, String htmlPath,
        Map conversionParams)
{
    log.info("Converting word file "+wordDocPath)
    try
    {
        ...
        String notificationWorkingFolder = "C:\tomcats\Notification\store\Notification1234"

        FileInputStream fis = new FileInputStream(wordDocPath);
        XWPFDocument document = new XWPFDocument(fis);
        XHTMLOptions options = XHTMLOptions.create().URIResolver(new FileURIResolver(new File(notificationWorkingFolder)));
        File htmlFile = new File(htmlPath);
        OutputStream out = new FileOutputStream(htmlFile)
        XHTMLConverter.getInstance().convert(document, out, options);

        log.info("Converted to HTML file "+htmlPath)

        return [success:true,htmlFileName:getFileName(htmlPath)]
    }
    catch(Exception e)
    {
        log.error("Exception :"+e.getMessage(),e)
        return [success:false]
    }

}

上面的代码将docx成功转换为html,但是如果docx包含任何它放置的图像< img src =C:\tomcats\Notification\store\Notification1234\word\media\image1.png> 但不要将图像复制到该文件夹​​。因此,当我打开html标签时,所有图像都显示为空。我在代码中丢失了什么吗?有没有一种方法来生成图像链接而不是绝对路径,如< img src =http:// localhost:8080 / webapp / image1.png>

The above code is converting docx to html successfully, but if docx contains any images it puts <img src="C:\tomcats\Notification\store\Notification1234\word\media\image1.png"> but do not copy the image to that folder. As a result, when I open html tag, all images appears empty. Am I missing something in code? Is there a way to generate an image srouce link instead of absolute path, like <img src="http://localhost:8080/webapp/image1.png">

推荐答案

我从这个链接lychaox.com/java/poi/Word07toHtml.html得到了第一个问题的答案。我不得不添加一行代码 options.setExtractor(new FileImageExtractor(imageFolderFile)); 来生成图像。
我通过模式搜索和替换解决了第二个问题。

I got answer for first question from this link lychaox.com/java/poi/Word07toHtml.html. I had to add one line of code options.setExtractor(new FileImageExtractor(imageFolderFile)); to generate images.Second question I resolved by pattern search and replacement.

这篇关于org.apache.poi.xwpf.converter.xhtml.XHTMLConverter不生成图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 19:03