use of org.docx4j.convert.in.xhtml.XHTMLImporterImpl in project Java-Tutorial by gpcodervn.
the class ImageResizeTest method getInline.
private Inline getInline(String html) throws Exception {
XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
List<Object> convert = XHTMLImporter.convert(html, null);
return ((Inline) ((Drawing) ((R) ((P) convert.get(0)).getContent().get(0)).getContent().get(0)).getAnchorOrInline().get(0));
}
use of org.docx4j.convert.in.xhtml.XHTMLImporterImpl in project Java-Tutorial by gpcodervn.
the class NumberingTest method convert.
private List<Object> convert(String xhtml, FormattingOption paragraphFormattingOption) throws Docx4JException {
XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
XHTMLImporter.setParagraphFormatting(paragraphFormattingOption);
return XHTMLImporter.convert(xhtml, "");
}
use of org.docx4j.convert.in.xhtml.XHTMLImporterImpl 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.in.xhtml.XHTMLImporterImpl in project Java-Tutorial by gpcodervn.
the class ConvertInXHTMLFragment method main.
public static void main(String[] args) throws Exception {
// String xhtml= "<div>" +
// "<h1>Heading</h1>" +
// "<table style='border:solid 1px white;'><tr><th>1</th></tr></table>" +
// "</div>";
// String xhtml = "<div><p>Hello here we <span style='background-color:red;'> were </span> and are now </p></div>";
// String xhtml = "<table><tr><td>1</td></tr></table>";
// String xhtml = "<ul>"+
// " <li> Outer 1 </li>"+
// " <li> Outer 2 </li>"+
// " <ul>"+
// " <li> Inner 1 </li>"+
// " <li> Inner 2 </li>"+
// "</ul>"+
// " <li> Outer 3 </li>"+
// "</ul>";
String PNG_IMAGE_DATA = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACAgMAAAAP2OW3AAAADFBMVEUDAP//AAAA/wb//AAD4Tw1AAAACXBIWXMAAAsTAAALEwEAmpwYAAAADElEQVQI12NwYNgAAAF0APHJnpmVAAAAAElFTkSuQmCC";
String xhtml = "<div align=\"center\">" + // "<p><img src='" + PNG_IMAGE_DATA + "' /></p>" +
"<img src='" + PNG_IMAGE_DATA + "' />" + "</div>";
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
// WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(System.getProperty("user.dir") + "/Hello.docx"));
// // Setup white list
// Set<String> cssWhiteList = new HashSet<String>();
// List lines = FileUtils.readLines(new File(System.getProperty("user.dir") + "/src/main/resources/CSS-WhiteList.txt"));
// // TODO catch exception
// for (Object o : lines) {
// String line = ((String)o).trim();
// if (line.length()>0 && !line.startsWith("#")) {
// cssWhiteList.add(line);
// }
// }
// XHTMLImporter.setCssWhiteList(cssWhiteList);
XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
// RFonts arialRFonts = Context.getWmlObjectFactory().createRFonts();
// arialRFonts.setAscii("Arial");
// arialRFonts.setHAnsi("Arial");
// XHTMLImporterImpl.addFontMapping("SomeFont", arialRFonts);
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"));
}
use of org.docx4j.convert.in.xhtml.XHTMLImporterImpl in project Java-Tutorial by gpcodervn.
the class ConvertInXHTMLURL method main.
public static void main(String[] args) throws Exception {
// Must tidy first :-(
// URL url = new URL("http://stackoverflow.com/questions/10887580/how-to-convert-a-webpage-from-an-intranet-wiki-to-an-office-document");
// URL url = new URL("http://en.wikipedia.org/wiki/Office_Open_XML");
// URL url = new URL("http://en.wikipedia.org/w/index.php?title=Office_Open_XML&printable=yes");
URL url = new URL("http://en.wikipedia.org/w/index.php?title=Microsoft_Word&printable=yes");
// Create an empty docx package
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
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(url));
System.out.println(XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true));
wordMLPackage.save(new java.io.File(System.getProperty("user.dir") + "/OUT_ConvertInXHTMLURL.docx"));
}
Aggregations