Search in sources :

Example 1 with XHTMLImporterImpl

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));
}
Also used : XHTMLImporterImpl(org.docx4j.convert.in.xhtml.XHTMLImporterImpl)

Example 2 with XHTMLImporterImpl

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, "");
}
Also used : XHTMLImporterImpl(org.docx4j.convert.in.xhtml.XHTMLImporterImpl)

Example 3 with XHTMLImporterImpl

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"));
}
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 4 with XHTMLImporterImpl

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"));
}
Also used : XHTMLImporterImpl(org.docx4j.convert.in.xhtml.XHTMLImporterImpl) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File)

Example 5 with XHTMLImporterImpl

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"));
}
Also used : XHTMLImporterImpl(org.docx4j.convert.in.xhtml.XHTMLImporterImpl) NumberingDefinitionsPart(org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) URL(java.net.URL)

Aggregations

XHTMLImporterImpl (org.docx4j.convert.in.xhtml.XHTMLImporterImpl)13 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)9 File (java.io.File)5 OutputStream (java.io.OutputStream)4 NumberingDefinitionsPart (org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 HTMLSettings (org.docx4j.convert.out.HTMLSettings)2 AbstractHtmlExporter (org.docx4j.convert.out.html.AbstractHtmlExporter)2 HtmlSettings (org.docx4j.convert.out.html.AbstractHtmlExporter.HtmlSettings)2 HtmlExporterNG2 (org.docx4j.convert.out.html.HtmlExporterNG2)2 URL (java.net.URL)1 MainDocumentPart (org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)1 RFonts (org.docx4j.wml.RFonts)1