Search in sources :

Example 6 with XHTMLImporterImpl

use of org.docx4j.convert.in.xhtml.XHTMLImporterImpl in project Java-Tutorial by gpcodervn.

the class DivRoundtrip method main.

public static void main(String[] args) throws Exception {
    String xhtml = "<div id=\"top\">" + "<h1>Heading</h1>" + "<div class=\"inner\">" + "<p>p1</p>" + "<p>p2</p>" + "</div>" + "<div id=\"transient-container\" class=\"IGNORE\">" + "<p>p1</p>" + "<p>p2</p>" + "</div>" + "</div>";
    // To docx, with content controls
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
    XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
    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"));
    // Back to XHTML
    HTMLSettings htmlSettings = Docx4J.createHTMLSettings();
    htmlSettings.setWmlPackage(wordMLPackage);
    // Sample sdt tag handler (tag handlers insert specific
    // html depending on the contents of an sdt's tag).
    // This will only have an effect if the sdt tag contains
    // the string class=
    SdtWriter.registerTagHandler("*", new MyTagClass());
    // output to an OutputStream.
    OutputStream os = new ByteArrayOutputStream();
    // If you want XHTML output
    Docx4jProperties.setProperty("docx4j.Convert.Out.HTML.OutputMethodXML", true);
    Docx4J.toHTML(htmlSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
    System.out.println(((ByteArrayOutputStream) os).toString());
}
Also used : XHTMLImporterImpl(org.docx4j.convert.in.xhtml.XHTMLImporterImpl) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HTMLSettings(org.docx4j.convert.out.HTMLSettings) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage)

Example 7 with XHTMLImporterImpl

use of org.docx4j.convert.in.xhtml.XHTMLImporterImpl in project docx4j-template by vindell.

the class XHTMLImporterUtils method handle.

public static WordprocessingMLPackage handle(WordprocessingMLPackage wmlPackage, Document doc, boolean fragment, boolean altChunk) throws IOException, Docx4JException {
    // 设置转换模式
    // 转为 xhtml 格式
    doc.outputSettings().syntax(Document.OutputSettings.Syntax.xml).escapeMode(Entities.EscapeMode.xhtml);
    if (altChunk) {
        // Document对象
        MainDocumentPart document = wmlPackage.getMainDocumentPart();
        // 获取Jsoup参数
        String charsetName = Docx4jProperties.getProperty(Docx4jConstants.DOCX4J_JSOUP_PARSE_CHARSETNAME, Docx4jConstants.DEFAULT_CHARSETNAME);
        // 设置转换模式
        // 转为 xhtml 格式
        doc.outputSettings().syntax(Document.OutputSettings.Syntax.xml).escapeMode(Entities.EscapeMode.xhtml);
        // 创建html导入对象
        // XHTMLImporterImpl xhtmlImporter = new XHTMLImporterImpl(wordMLPackage);
        document.addAltChunk(AltChunkType.Xhtml, (fragment ? doc.body().html() : doc.html()).getBytes(Charset.forName(charsetName)));
        // document.addAltChunk(type, bytes, attachmentPoint)
        // document.addAltChunk(type, is)
        // document.addAltChunk(type, is, attachmentPoint)
        WordprocessingMLPackage tempPackage = document.convertAltChunks();
        // 返回处理后的WordprocessingMLPackage对象
        return tempPackage;
    }
    // 创建html导入对象
    XHTMLImporterImpl xhtmlImporter = new XHTMLImporterImpl(wmlPackage);
    // 将xhtml转换为wmlPackage可用的对象
    List<Object> list = xhtmlImporter.convert((fragment ? doc.body().html() : doc.html()), doc.baseUri());
    // 导入转换后的内容对象
    wmlPackage.getMainDocumentPart().getContent().addAll(list);
    // 返回原WordprocessingMLPackage对象
    return wmlPackage;
}
Also used : XHTMLImporterImpl(org.docx4j.convert.in.xhtml.XHTMLImporterImpl) MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage)

Example 8 with XHTMLImporterImpl

use of org.docx4j.convert.in.xhtml.XHTMLImporterImpl in project Java-Tutorial by gpcodervn.

the class XhtmlToDocxAndBack method main.

public static void main(String[] args) throws Exception {
    String xhtml = "<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:100%;\"><tbody><tr><td>test</td><td>test</td></tr><tr><td>test</td><td>test</td></tr><tr><td>test</td><td>test</td></tr></tbody></table>";
    // To docx, with content controls
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
    XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
    // XHTMLImporter.setDivHandler(new DivToSdt());
    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"));
    // Back to XHTML
    HTMLSettings htmlSettings = Docx4J.createHTMLSettings();
    htmlSettings.setWmlPackage(wordMLPackage);
    // output to an OutputStream.
    OutputStream os = new ByteArrayOutputStream();
    // If you want XHTML output
    Docx4jProperties.setProperty("docx4j.Convert.Out.HTML.OutputMethodXML", true);
    Docx4J.toHTML(htmlSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
    System.out.println(((ByteArrayOutputStream) os).toString());
}
Also used : XHTMLImporterImpl(org.docx4j.convert.in.xhtml.XHTMLImporterImpl) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HTMLSettings(org.docx4j.convert.out.HTMLSettings) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage)

Example 9 with XHTMLImporterImpl

use of org.docx4j.convert.in.xhtml.XHTMLImporterImpl in project Java-Tutorial by gpcodervn.

the class ImageAddTest 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 10 with XHTMLImporterImpl

use of org.docx4j.convert.in.xhtml.XHTMLImporterImpl in project Java-Tutorial by gpcodervn.

the class ImageResizeTest method testHeightWidthInPx.

public void testHeightWidthInPx() throws Exception {
    String PNG_IMAGE_DATA = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACAgMAAAAP2OW3AAAADFBMVEUDAP//AAAA/wb//AAD4Tw1AAAACXBIWXMAAAsTAAALEwEAmpwYAAAADElEQVQI12NwYNgAAAF0APHJnpmVAAAAAElFTkSuQmCC";
    String html = "<div>" + "<p><img src='" + PNG_IMAGE_DATA + "' width='40px' height='20px' /></p>" + "<p><img src='" + PNG_IMAGE_DATA + "' style='width:40px; height:20px' /></p>" + "</div>";
    XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
    List<Object> convert = XHTMLImporter.convert(html, null);
    wordMLPackage.getMainDocumentPart().getContent().addAll(convert);
    wordMLPackage.save(new File(System.getProperty("user.dir") + "/px.docx"));
}
Also used : XHTMLImporterImpl(org.docx4j.convert.in.xhtml.XHTMLImporterImpl) File(java.io.File)

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