use of org.docx4j.convert.in.xhtml.XHTMLImporterImpl in project Java-Tutorial by gpcodervn.
the class ConvertInXHTMLFile method main.
public static void main(String[] args) throws Exception {
String inputfilepath = System.getProperty("user.dir") + "/somedir/some.html";
// String baseURL = "file:///C:/Users/jharrop/git/docx4j-ImportXHTML/somedir/";
String baseURL = "file:///C:/Users/jharrop/git/docx4j-ImportXHTML/";
String stringFromFile = FileUtils.readFileToString(new File(inputfilepath), "UTF-8");
String unescaped = stringFromFile;
// if (stringFromFile.contains("</") ) {
// unescaped = StringEscapeUtils.unescapeHtml(stringFromFile);
// }
// XHTMLImporter.setTableFormatting(FormattingOption.IGNORE_CLASS);
// XHTMLImporter.setParagraphFormatting(FormattingOption.IGNORE_CLASS);
System.out.println("Unescaped: " + unescaped);
// Setup font mapping
RFonts rfonts = Context.getWmlObjectFactory().createRFonts();
rfonts.setAscii("Century Gothic");
XHTMLImporterImpl.addFontMapping("Century Gothic", rfonts);
// Create an empty docx package
// WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(System.getProperty("user.dir") + "/styled.docx"));
NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
wordMLPackage.getMainDocumentPart().addTargetPart(ndp);
ndp.unmarshalDefaultNumbering();
// Convert the XHTML, and add it into the empty docx we made
XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
XHTMLImporter.setHyperlinkStyle("Hyperlink");
wordMLPackage.getMainDocumentPart().getContent().addAll(XHTMLImporter.convert(unescaped, baseURL));
System.out.println(XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true));
// System.out.println(
// XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getNumberingDefinitionsPart().getJaxbElement(), true, true));
wordMLPackage.save(new java.io.File(System.getProperty("user.dir") + "/OUT_from_XHTML.docx"));
}
use of org.docx4j.convert.in.xhtml.XHTMLImporterImpl 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"));
}
use of org.docx4j.convert.in.xhtml.XHTMLImporterImpl in project docx4j-template by vindell.
the class HtmlConverter method xhtml2word.
/**
* 将 {@link org.jsoup.nodes.Document} 对象转为 {@link org.docx4j.openpackaging.packages.WordprocessingMLPackage}
* xhtml to word
*
* @param doc
* @return
* @throws Exception
*/
protected WordprocessingMLPackage xhtml2word(Document doc) throws Exception {
// A4纸,//横版:true
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(PageSizePaper.valueOf("A4"), true);
// 配置中文字体
configSimSunFont(wordMLPackage);
XHTMLImporterImpl xhtmlImporter = new XHTMLImporterImpl(wordMLPackage);
// 导入 xhtml
wordMLPackage.getMainDocumentPart().getContent().addAll(xhtmlImporter.convert(doc.html(), doc.baseUri()));
return wordMLPackage;
}
Aggregations