Search in sources :

Example 1 with HTMLSettings

use of org.docx4j.convert.out.HTMLSettings in project Java-Tutorial by gpcodervn.

the class DivRoundtrip method main.

public static void main(String[] args) throws Exception {
    String xhtml = "<div id=\"top\">" + "<h1>Heading</h1>" + "<div class=\"inner\">" + "<p>p1</p>" + "<p>p2</p>" + "</div>" + "<div id=\"transient-container\" class=\"IGNORE\">" + "<p>p1</p>" + "<p>p2</p>" + "</div>" + "</div>";
    // To docx, with content controls
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
    XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
    wordMLPackage.getMainDocumentPart().getContent().addAll(XHTMLImporter.convert(xhtml, null));
    System.out.println(XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true));
    wordMLPackage.save(new java.io.File(System.getProperty("user.dir") + "/OUT_from_XHTML.docx"));
    // Back to XHTML
    HTMLSettings htmlSettings = Docx4J.createHTMLSettings();
    htmlSettings.setWmlPackage(wordMLPackage);
    // Sample sdt tag handler (tag handlers insert specific
    // html depending on the contents of an sdt's tag).
    // This will only have an effect if the sdt tag contains
    // the string class=
    SdtWriter.registerTagHandler("*", new MyTagClass());
    // output to an OutputStream.
    OutputStream os = new ByteArrayOutputStream();
    // If you want XHTML output
    Docx4jProperties.setProperty("docx4j.Convert.Out.HTML.OutputMethodXML", true);
    Docx4J.toHTML(htmlSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
    System.out.println(((ByteArrayOutputStream) os).toString());
}
Also used : XHTMLImporterImpl(org.docx4j.convert.in.xhtml.XHTMLImporterImpl) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HTMLSettings(org.docx4j.convert.out.HTMLSettings) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage)

Example 2 with HTMLSettings

use of org.docx4j.convert.out.HTMLSettings 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)

Example 3 with HTMLSettings

use of org.docx4j.convert.out.HTMLSettings in project Java-Tutorial by gpcodervn.

the class XhtmlToDocxAndBack method main.

public static void main(String[] args) throws Exception {
    String xhtml = "<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:100%;\"><tbody><tr><td>test</td><td>test</td></tr><tr><td>test</td><td>test</td></tr><tr><td>test</td><td>test</td></tr></tbody></table>";
    // To docx, with content controls
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
    XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
    // XHTMLImporter.setDivHandler(new DivToSdt());
    wordMLPackage.getMainDocumentPart().getContent().addAll(XHTMLImporter.convert(xhtml, null));
    System.out.println(XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true));
    // wordMLPackage.save(new java.io.File(System.getProperty("user.dir")
    // + "/OUT_from_XHTML.docx"));
    // Back to XHTML
    HTMLSettings htmlSettings = Docx4J.createHTMLSettings();
    htmlSettings.setWmlPackage(wordMLPackage);
    // output to an OutputStream.
    OutputStream os = new ByteArrayOutputStream();
    // If you want XHTML output
    Docx4jProperties.setProperty("docx4j.Convert.Out.HTML.OutputMethodXML", true);
    Docx4J.toHTML(htmlSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
    System.out.println(((ByteArrayOutputStream) os).toString());
}
Also used : XHTMLImporterImpl(org.docx4j.convert.in.xhtml.XHTMLImporterImpl) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HTMLSettings(org.docx4j.convert.out.HTMLSettings) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage)

Aggregations

OutputStream (java.io.OutputStream)3 HTMLSettings (org.docx4j.convert.out.HTMLSettings)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 XHTMLImporterImpl (org.docx4j.convert.in.xhtml.XHTMLImporterImpl)2 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputDirFilterHandler (org.docx4j.template.handler.OutputDirFilterHandler)1