use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project Java-Tutorial by gpcodervn.
the class Write_HelloWorld method main.
public static void main(String[] args) throws Docx4JException {
WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage();
MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart();
mainDocumentPart.addStyledParagraphOfText("Title", "Hello World!");
mainDocumentPart.addParagraphOfText("Welcome To Baeldung");
File exportFile = new File("output/welcome1.docx");
wordPackage.save(exportFile);
System.out.println("Done!");
}
use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project Java-Tutorial by gpcodervn.
the class Write_Table method main.
public static void main(String[] args) throws Exception {
WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage();
MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart();
mainDocumentPart.addStyledParagraphOfText("Title", "Hello World!");
mainDocumentPart.addParagraphOfText("Welcome To Baeldung");
ObjectFactory factory = Context.getWmlObjectFactory();
P p = factory.createP();
R r = factory.createR();
Text t = factory.createText();
t.setValue("Cell data");
r.getContent().add(t);
p.getContent().add(r);
int writableWidthTwips = wordPackage.getDocumentModel().getSections().get(0).getPageDimensions().getWritableWidthTwips();
int columnNumber = 3;
Tbl tbl = TblFactory.createTable(3, 3, writableWidthTwips / columnNumber);
List<Object> rows = tbl.getContent();
for (Object row : rows) {
Tr tr = (Tr) row;
List<Object> cells = tr.getContent();
for (Object cell : cells) {
Tc td = (Tc) cell;
td.getContent().add(p);
}
}
mainDocumentPart.getContent().add(tbl);
File exportFile = new File("output/welcome4.docx");
wordPackage.save(exportFile);
System.out.println("Done!");
}
use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project Java-Tutorial by gpcodervn.
the class ContentControlsMergeXML281 method main.
public static void main(String[] args) throws Exception {
// the docx 'template'
String input_DOCX = System.getProperty("user.dir") + "/sample-docs/word/databinding/binding-simple.docx";
// the instance data
String input_XML = System.getProperty("user.dir") + "/sample-docs/word/databinding/binding-simple-data.xml";
// resulting docx
String OUTPUT_DOCX = System.getProperty("user.dir") + "/OUT_ContentControlsMergeXML.docx";
// Load input_template.docx
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(input_DOCX));
// Inject data_file.xml
// (this code assumes it is not a StandardisedAnswersPart)
CustomXmlDataStoragePart customXmlDataStoragePart = CustomXmlDataStoragePartSelector.getCustomXmlDataStoragePart(wordMLPackage);
if (customXmlDataStoragePart == null) {
System.out.println("Couldn't find CustomXmlDataStoragePart! exiting..");
return;
}
System.out.println("Getting " + input_XML);
FileInputStream fis = new FileInputStream(new File(input_XML));
customXmlDataStoragePart.getData().setDocument(fis);
SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
OpenDoPEHandler odh = null;
try {
// Process conditionals and repeats
odh = new OpenDoPEHandler(wordMLPackage);
odh.preprocess();
OpenDoPEIntegrity odi = new OpenDoPEIntegrity();
odi.process(wordMLPackage);
if (DEBUG) {
String save_preprocessed;
if (OUTPUT_DOCX.lastIndexOf(".") == -1) {
save_preprocessed = OUTPUT_DOCX + "_INT.docx";
} else {
save_preprocessed = OUTPUT_DOCX.substring(0, OUTPUT_DOCX.lastIndexOf(".")) + "_INT.docx";
}
// System.out.println(
// XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true)
// );
saver.save(save_preprocessed);
System.out.println("Saved: " + save_preprocessed);
}
} catch (Docx4JException d) {
// Probably this docx doesn't contain OpenDoPE convention parts
System.out.println(d.getMessage());
}
// Apply the bindings
BindingHandler.setHyperlinkStyle("Hyperlink");
// For docx4j <= 3.2.0
// BindingHandler.applyBindings(wordMLPackage.getMainDocumentPart());
// For docx4j > 3.2.0, replace that with:
AtomicInteger bookmarkId = odh.getNextBookmarkId();
BindingHandler bh = new BindingHandler(wordMLPackage);
bh.setStartingIdForNewBookmarks(bookmarkId);
bh.applyBindings(wordMLPackage.getMainDocumentPart());
// If you inspect the output, you should see your data in 2 places:
// 1. the custom xml part
// 2. (more importantly) the main document part
// System.out.println(
// XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true)
// );
// Strip content controls
RemovalHandler rh = new RemovalHandler();
rh.removeSDTs(wordMLPackage, Quantifier.ALL);
saver.save(OUTPUT_DOCX);
System.out.println("Saved: " + OUTPUT_DOCX);
}
use of org.docx4j.openpackaging.packages.WordprocessingMLPackage 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"));
}
use of org.docx4j.openpackaging.packages.WordprocessingMLPackage 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"));
}
Aggregations