Search in sources :

Example 11 with XHTMLImporterImpl

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

Example 12 with XHTMLImporterImpl

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

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

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