Search in sources :

Example 6 with WordprocessingMLPackage

use of org.docx4j.openpackaging.packages.WordprocessingMLPackage 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 7 with WordprocessingMLPackage

use of org.docx4j.openpackaging.packages.WordprocessingMLPackage 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 8 with WordprocessingMLPackage

use of org.docx4j.openpackaging.packages.WordprocessingMLPackage 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)

Example 9 with WordprocessingMLPackage

use of org.docx4j.openpackaging.packages.WordprocessingMLPackage 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 10 with WordprocessingMLPackage

use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project docx4j-template by vindell.

the class Docx4J_简单例子 method main.

public static void main(String[] args) throws Exception {
    Docx4J_简单例子 t = new Docx4J_简单例子();
    WordprocessingMLPackage wordMLPackage = t.createWordprocessingMLPackage();
    MainDocumentPart mp = wordMLPackage.getMainDocumentPart();
    ObjectFactory factory = Context.getWmlObjectFactory();
    // 页眉
    Relationship relationship = t.createHeaderPart(wordMLPackage, mp, factory);
    t.createHeaderReference(wordMLPackage, mp, factory, relationship);
    t.addParagraphTest(wordMLPackage, mp, factory);
    t.addPageBreak(wordMLPackage, factory);
    // 页脚
    t.createNormalTableTest(wordMLPackage, mp, factory);
    relationship = t.createFooterPageNumPart(wordMLPackage, mp, factory);
    t.createFooterReference(wordMLPackage, mp, factory, relationship);
    t.saveWordPackage(wordMLPackage, new File("f:/saveFile/temp/s_simple.docx"));
}
Also used : ObjectFactory(org.docx4j.wml.ObjectFactory) MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) Relationship(org.docx4j.relationships.Relationship) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File)

Aggregations

WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)67 File (java.io.File)50 MainDocumentPart (org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)22 Docx4JException (org.docx4j.openpackaging.exceptions.Docx4JException)11 XHTMLImporterImpl (org.docx4j.convert.in.xhtml.XHTMLImporterImpl)9 OutputStream (java.io.OutputStream)8 ObjectFactory (org.docx4j.wml.ObjectFactory)8 Test (org.junit.Test)8 P (org.docx4j.wml.P)7 Text (org.docx4j.wml.Text)7 FileOutputStream (java.io.FileOutputStream)6 FileInputStream (java.io.FileInputStream)5 Body (org.docx4j.wml.Body)5 Tbl (org.docx4j.wml.Tbl)5 TraversalUtil (org.docx4j.TraversalUtil)4 NumberingDefinitionsPart (org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart)4 R (org.docx4j.wml.R)4 RPr (org.docx4j.wml.RPr)4 SpecExample (com.vladsch.flexmark.spec.SpecExample)3 IOException (java.io.IOException)3