i'm trying to create a somewhat simple word doc. and i'm ending up with a lot of extra lines between paragraphs. here's what i've done.
i created a word doc as a template. i then uploaded to http://webapp.docx4java.org/ (great tool!) to explore get the code needed to create via docx4j. I copied the XML (method 2) code from the first 4 paragraphs. I clicked on document and then the P elements. Created methods where I could pass in a string to replace the templated value.
- Code: Select all
private P getP(String part) throws JAXBException {
String openXML = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">"
+ "<w:pPr>"
+ "<w:rPr>"
+ "<w:rFonts w:ascii=\"PT Serif\" w:hAnsi=\"PT Serif\"/>"
+ "<w:sz w:val=\"18\"/>"
+ "<w:szCs w:val=\"18\"/>"
+"</w:rPr>"
+"</w:pPr>"
+ "<w:r>"
+ "<w:rPr>"
+ "<w:rFonts w:ascii=\"PT Serif\" w:hAnsi=\"PT Serif\"/>"
+ "<w:sz w:val=\"18\"/>"
+ "<w:szCs w:val=\"18\"/>"
+"</w:rPr>"
+ "<w:t>"+ part +"</w:t>"
+"</w:r>"
+"</w:p>";
return (P) XmlUtils.unmarshalString(openXML);
}
And then assembling the document with
- Code: Select all
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
mdp.addObject(getP("Population: Some Population"));
mdp.addObject(getP("Intervention: Some intervention"));
mdp.addObject(getP("Comparator: Some comparator"));
String filename = System.getProperty("user.dir") + "/OUT_samplePICO.docx";
wordMLPackage.save(new java.io.File(filename));
I haven't noticed any difference in the XML from the template in explorer app vs what this outputs.
- Code: Select all
System.out.println(XmlUtils.marshaltoString(mdp.getJaxbElement(), true, true));
What am i missing and why do I have extra spaces?