use of org.docx4j.convert.out.html.HtmlExporterNG2 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"));
}
use of org.docx4j.convert.out.html.HtmlExporterNG2 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>";
}
use of org.docx4j.convert.out.html.HtmlExporterNG2 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"));
}
Aggregations