使用xdocreport将带有图像的.doc转换为.html

时间:2016-10-27 15:54:27

标签: java apache apache-poi xdocreport

我正在使用以下代码将doc转换为html

private static final String docName = "This is a test page.docx";
private static final String outputlFolderPath = "C://";
String htmlNamePath = "docHtml1.html";
String zipName="_tmp.zip";
static File docFile = new File(outputlFolderPath+docName);
File zipFile = new File(zipName);

public void ConvertWordToHtml() {
    try {
        InputStream doc = new FileInputStream(new File(outputlFolderPath+docName));
        System.out.println("InputStream"+doc);
        XWPFDocument document = new XWPFDocument(doc);
        XHTMLOptions options = XHTMLOptions.create(); //.URIResolver(new FileURIResolver(new File("word/media")));;
        String root = "target";
        File imageFolder = new File( root + "/images/" + doc );
        options.setExtractor( new FileImageExtractor( imageFolder ) );
        options.URIResolver( new FileURIResolver( imageFolder ) );
        OutputStream out = new FileOutputStream(new File(htmlPath()));
        XHTMLConverter.getInstance().convert(document, out, options);
    } catch (Exception ex) {

    }
}

public static void main(String[] args) throws IOException, ParserConfigurationException, Exception {
    Convertion cwoWord=new Convertion();
    cwoWord.ConvertWordToHtml();    

}


public String htmlPath(){
    return outputlFolderPath+htmlNamePath;
}

public String zipPath(){
    // d:/_tmp.zip
    return outputlFolderPath+zipName;
}

以上代码将doc转换为html。当我尝试转换具有图形的doc文件时出现问题 像圆圈一样(如截图所示),在这种情况下,图形不会显示在html文件中。

enter image description here

请帮我解释如何在转换后将图形从doc文件保存到html文件。在此先感谢

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码将图像嵌入到html中:

Base64ImageExtractor imageExtractor = new Base64ImageExtractor();
options.setExtractor(imageExtractor);
options.URIResolver(imageExtractor);

Base64ImageExtractor的样子:

public class Base64ImageExtractor implements IImageExtractor, IURIResolver {

    private byte[] picture;

    public void extract(String imagePath, byte[] imageData) throws IOException {
        this.picture = imageData;
    }

    private static final String EMBED_IMG_SRC_PREFIX = "data:;base64,";


    public String resolve(String uri) {
        StringBuilder sb = new StringBuilder(picture.length + EMBED_IMG_SRC_PREFIX.length())
                .append(EMBED_IMG_SRC_PREFIX)
                .append(Base64Utility.encode(picture));
        return sb.toString();
    }
}