hi Jason,
Thanks for your response so far. I made few more tests and realized that the white space is created if I am loading an exisitng file in WMLPackage. When I am creating a new package then there is no such space created .
So, I am thinking -- instead of making changes in the loaded file why not to take all the JAXBElements and altChunk(html) to a new package.
So, roughly my design would be :
- Code: Select all
// 1. Load and create two WMLPackages.
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(path) );
WordprocessingMLPackage wordMLPackage2 = WordprocessingMLPackage.createPackage();
// 2. Fetch the document part
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
MainDocumentPart documentPart2 = wordMLPackage2.getMainDocumentPart();
//3. Get the elements from first documentpart
org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart
.getJaxbElement();
//4. marshal the elements to String in xml format
String xml = XmlUtils.marshaltoString(wmlDocumentEl, true);
//5. Create a map to do replacement mapping
HashMap<String, String> mappings = new HashMap<String, String>();
//6. Convert the xml with changed values back to object form.
Object obj = XmlUtils.unmarshallFromTemplate(xml, mappings);
// 7. Instead of setting documentPart of Loaded WMLPackage , do it in the WMLPackage of new Created one (second one)
documentPart2.setJaxbElement((Document)obj);
//8 . Also add the html package
wordMLPackage2.getMainDocumentPart().addAltChunk(AltChunkType.Html, html.getBytes());
//9. And save it
wordMLPackage2.save(new java.io.File("C:/test2.docx"));
I am not sure if this strategy makes sense. When I try to execute it, the word file cannot be opened and complains it is missing some part/invalid (Location: part: / word/document.xml , Line: 1, Column: 0 )
I am new to JAXB and docx4j . Would appreciate your directions.
Thanks