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"));
}
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"));
}
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"));
}
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());
}
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"));
}
Aggregations