Search in sources :

Example 1 with HtmlSettings

use of org.docx4j.convert.out.html.AbstractHtmlExporter.HtmlSettings in project Java-Tutorial by gpcodervn.

the class ConvertInXHTMLDocument method main.

public static void main(String[] args) throws Exception {
    // The input would generally be an XHTML document,
    // but for convenience, this sample can convert a
    // docx to XHTML first (ie round trip).
    String inputfilepath = System.getProperty("user.dir") + "/sample-docs/word/sample-docx.docx";
    // Create an empty docx package
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
    NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
    wordMLPackage.getMainDocumentPart().addTargetPart(ndp);
    ndp.unmarshalDefaultNumbering();
    XHTMLImporterImpl xHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
    xHTMLImporter.setHyperlinkStyle("Hyperlink");
    if (inputfilepath.endsWith("html")) {
        // Convert the XHTML, and add it into the empty docx we made
        wordMLPackage.getMainDocumentPart().getContent().addAll(xHTMLImporter.convert(new File(inputfilepath), null));
    } else if (inputfilepath.endsWith("docx")) {
        // Round trip docx -> XHTML -> docx
        WordprocessingMLPackage docx = WordprocessingMLPackage.load(new File(inputfilepath));
        AbstractHtmlExporter exporter = new HtmlExporterNG2();
        // Use file system, so there is somewhere to save images (if any)
        OutputStream os = new java.io.FileOutputStream(inputfilepath + ".html");
        HtmlSettings htmlSettings = new HtmlSettings();
        htmlSettings.setImageDirPath(inputfilepath + "_files");
        htmlSettings.setImageTargetUri(inputfilepath.substring(inputfilepath.lastIndexOf("/") + 1) + "_files");
        javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult(os);
        exporter.html(docx, result, htmlSettings);
        // Now after all that, we have XHTML we can convert
        wordMLPackage.getMainDocumentPart().getContent().addAll(xHTMLImporter.convert(new File(inputfilepath + ".html"), null));
    } else {
        return;
    }
    System.out.println(XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true));
    wordMLPackage.save(new java.io.File(System.getProperty("user.dir") + "/html_output.docx"));
}
Also used : XHTMLImporterImpl(org.docx4j.convert.in.xhtml.XHTMLImporterImpl) NumberingDefinitionsPart(org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart) OutputStream(java.io.OutputStream) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File) HtmlSettings(org.docx4j.convert.out.html.AbstractHtmlExporter.HtmlSettings) AbstractHtmlExporter(org.docx4j.convert.out.html.AbstractHtmlExporter) HtmlExporterNG2(org.docx4j.convert.out.html.HtmlExporterNG2) File(java.io.File)

Example 2 with HtmlSettings

use of org.docx4j.convert.out.html.AbstractHtmlExporter.HtmlSettings in project mdw-designer by CenturyLinkCloud.

the class DocxBuilder method toHtml.

public String toHtml() throws Exception {
    String top = "<com.centurylink.mdw.doc>";
    String tail = "</com.centurylink.mdw.doc>";
    AbstractHtmlExporter exporter = new HtmlExporterNG2();
    HtmlSettings htmlSettings = new HtmlSettings();
    // TODO image path
    htmlSettings.setUserBodyTop(top);
    htmlSettings.setUserBodyTail(tail);
    OutputStream os = new ByteArrayOutputStream();
    StreamResult result = new StreamResult(os);
    exporter.html(wordMLPackage, result, htmlSettings);
    String html = os.toString();
    int start = html.indexOf(top) + top.length();
    int stop = html.indexOf(tail);
    return "<html>" + html.substring(start, stop) + "</html>";
}
Also used : StreamResult(javax.xml.transform.stream.StreamResult) HtmlSettings(org.docx4j.convert.out.html.AbstractHtmlExporter.HtmlSettings) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) AbstractHtmlExporter(org.docx4j.convert.out.html.AbstractHtmlExporter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HtmlExporterNG2(org.docx4j.convert.out.html.HtmlExporterNG2)

Example 3 with HtmlSettings

use of org.docx4j.convert.out.html.AbstractHtmlExporter.HtmlSettings in project Java-Tutorial by gpcodervn.

the class DocxToXhtmlAndBack method main.

public static void main(String[] args) throws Exception {
    // String baseURL = "file:///C:/Users/jharrop/git/docx4j-ImportXHTML/images";
    Docx4jProperties.setProperty("docx4j.Convert.Out.HTML.OutputMethodXML", true);
    try {
        getInputFilePath(args);
    } catch (IllegalArgumentException e) {
    }
    System.out.println(inputfilepath);
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(dir + inputfilepath));
    // XHTML export
    AbstractHtmlExporter exporter = new HtmlExporterNG2();
    HtmlSettings htmlSettings = new HtmlSettings();
    htmlSettings.setWmlPackage(wordMLPackage);
    htmlSettings.setImageDirPath(dir + inputfilepath + "_files");
    htmlSettings.setImageTargetUri(dir + inputfilepath + "_files");
    String htmlFilePath = dir + "/DocxToXhtmlAndBack.html";
    OutputStream os = new java.io.FileOutputStream(htmlFilePath);
    // javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult(os);
    // exporter.html(wordMLPackage, result, htmlSettings);
    // os.flush();
    // os.close();
    Docx4J.toHTML(htmlSettings, os, Docx4J.FLAG_NONE);
    // XHTML to docx
    String stringFromFile = FileUtils.readFileToString(new File(htmlFilePath), "UTF-8");
    WordprocessingMLPackage docxOut = WordprocessingMLPackage.createPackage();
    NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
    docxOut.getMainDocumentPart().addTargetPart(ndp);
    ndp.unmarshalDefaultNumbering();
    XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(docxOut);
    XHTMLImporter.setHyperlinkStyle("Hyperlink");
    docxOut.getMainDocumentPart().getContent().addAll(XHTMLImporter.convert(stringFromFile, null));
    docxOut.save(new java.io.File(dir + "/DocxToXhtmlAndBack.docx"));
}
Also used : XHTMLImporterImpl(org.docx4j.convert.in.xhtml.XHTMLImporterImpl) NumberingDefinitionsPart(org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart) OutputStream(java.io.OutputStream) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File) HtmlSettings(org.docx4j.convert.out.html.AbstractHtmlExporter.HtmlSettings) AbstractHtmlExporter(org.docx4j.convert.out.html.AbstractHtmlExporter) HtmlExporterNG2(org.docx4j.convert.out.html.HtmlExporterNG2) File(java.io.File)

Aggregations

OutputStream (java.io.OutputStream)3 AbstractHtmlExporter (org.docx4j.convert.out.html.AbstractHtmlExporter)3 HtmlSettings (org.docx4j.convert.out.html.AbstractHtmlExporter.HtmlSettings)3 HtmlExporterNG2 (org.docx4j.convert.out.html.HtmlExporterNG2)3 File (java.io.File)2 XHTMLImporterImpl (org.docx4j.convert.in.xhtml.XHTMLImporterImpl)2 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)2 NumberingDefinitionsPart (org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 StreamResult (javax.xml.transform.stream.StreamResult)1