Search in sources :

Example 1 with OutputDirFilterHandler

use of org.docx4j.template.handler.OutputDirFilterHandler in project docx4j-template by vindell.

the class WordprocessingMLPackageWriter method writeToHtml.

/**
 * 将 {@link org.docx4j.openpackaging.packages.WordprocessingMLPackage} 存为 html
 */
public File writeToHtml(WordprocessingMLPackage wmlPackage, File outFile) throws IOException, Docx4JException {
    Assert.notNull(wmlPackage, " wmlPackage is not specified!");
    Assert.isTrue(outFile.exists(), " outFile is not founded !");
    OutputStream output = null;
    try {
        String imageTargetUri = Docx4jProperties.getProperty(Docx4jConstants.DOCX4J_CONVERT_OUT_HTML_IMAGETARGETURI, "images");
        File[] files = outFile.listFiles(new OutputDirFilterHandler(imageTargetUri));
        if (files.length != 1) {
            File imageDir = new File(outFile, imageTargetUri);
            imageDir.setWritable(true);
            imageDir.setReadable(true);
            imageDir.mkdir();
        }
        // 创建文件输出流
        output = new FileOutputStream(outFile);
        // 创建Html输出设置
        HTMLSettings htmlSettings = Docx4J.createHTMLSettings();
        htmlSettings.setImageDirPath(outFile.getParent());
        htmlSettings.setImageTargetUri(imageTargetUri);
        htmlSettings.setWmlPackage(wmlPackage);
        // d
        htmlSettings.setHyperlinkHandler(DEFAULT_HYPERLINK_HANDLER);
        htmlSettings.setScriptElementHandler(DEFAULT_SCRIPT_ELEMENT_HANDLER);
        htmlSettings.setStyleElementHandler(DEFAULT_STYLE_ELEMENT_HANDLER);
        Docx4jProperties.setProperty(Docx4jConstants.DOCX4J_PARAM_04, true);
        // Docx4J.toHTML(settings, outputStream, flags);
        // Docx4J.toHTML(wmlPackage, imageDirPath, imageTargetUri, outputStream);
        Docx4J.toHTML(htmlSettings, output, Docx4J.FLAG_EXPORT_PREFER_XSL);
    } finally {
        IOUtils.closeQuietly(output);
    }
    return outFile;
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) HTMLSettings(org.docx4j.convert.out.HTMLSettings) OutputDirFilterHandler(org.docx4j.template.handler.OutputDirFilterHandler) File(java.io.File)

Aggregations

File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 HTMLSettings (org.docx4j.convert.out.HTMLSettings)1 OutputDirFilterHandler (org.docx4j.template.handler.OutputDirFilterHandler)1