Hi All,
Is it possible to use docx4j to recreate/create a docx from a document.xml file?
Thanks
//Erik
Is it possible to use docx4j to recreate/create a docx from a document.xml file?
String filename = "document.xml";
String fileOut = "new.docx";
// Create a package
WordprocessingMLPackage wmlPack = new WordprocessingMLPackage();
// Create main document part
org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart wordDocumentPart
= new org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart();
// Make a JAXB Object out of the document.xml
JAXBContext jc = Context.jc;
Unmarshaller u = jc.createUnmarshaller();
u.setEventHandler(new org.docx4j.jaxb.JaxbValidationEventHandler());
Object o = u.unmarshal( new File(filename ) );
// for other options, see http://java.sun.com/webservices/docs/1.6/api/javax/xml/bind/Unmarshaller.html
// Attach it
wordDocumentPart.setJaxbElement(o);
// Add the main document part to the package relationships
// (creating it if necessary)
wmlPack.addTargetPart(wordDocumentPart);
// Now save the package
wmlPack.save(new java.io.File(System.getProperty("user.dir") + fileOut) );
Users browsing this forum: No registered users and 105 guests