- Code: Select all
WordDocument doc = new WordDocument();
WordParagraph p1 = new WordParagraph("Sample Text");
WordParagraph p2 = new WordParagraph("Sample Text2");
doc.add(p1);
doc.add(p2);
But there is always an empty line below the content of the paragraph (this looks like spacing or margin or something). Like this:
- Code: Select all
Sample Text
Sample Text2
But in the normal case I don't want to have this empty space below the paragraph. I'm still trying to set spacing = 0, but where is the problem?
The class WordParagraph :
- Code: Select all
public class WordParagraph {
public WordParagraph(){
docx4jParagraph = factory.createP();
run = factory.createR();
docx4jParagraph.getContent().add(run);
pPr = factory.createPPr();
//spacing 0
PPrBase.Spacing space = new PPrBase.Spacing();
space.setAfter(BigInteger.ZERO);
space.setBefore(BigInteger.ZERO);
pPr.setSpacing(space);
PStyle style = factory.createPPrBasePStyle();
style.setVal(styleID);
pPr.setPStyle(style);
jc = factory.createJc();
jc.setVal(JcEnumeration.LEFT);
pPr.setJc(jc);
docx4jParagraph.setPPr(pPr);
}
public void add(String text, String styleID, WordParagraph.TYPE type){
if (text != null) {
this.styleID = styleID;
org.docx4j.wml.Text t = factory.createText();
t.setValue(text);
P p = new P();
R tmpRun = factory.createR();
tmpRun.getContent().add(t);
org.docx4j.wml.RPr rpr = factory.createRPr();
switch(type){
case BOLD:
setBold(rpr);
break;
case ITALIC:
setItalic(rpr);
break;
case UNDERLINED:
setUnderlined(rpr);
break;
case ITALICUNDERLINED:
setItalic(rpr);
setUnderlined(rpr);
break;
case BOLDUNDERLINED:
setBold(rpr);
setUnderlined(rpr);
break;
}
tmpRun.setRPr(rpr);
CTSignedTwipsMeasure ctsm = new CTSignedTwipsMeasure();
ctsm.setVal(BigInteger.ZERO);
rpr.setSpacing(ctsm);
p.getContent().add(tmpRun);
PStyle style = factory.createPPrBasePStyle();
style.setVal(styleID);
PPr properties = factory.createPPr();
properties.setPStyle(style);
PPrBase.Spacing space = new PPrBase.Spacing();
space.setAfter(BigInteger.ZERO);
space.setBefore(BigInteger.ZERO);
properties.setSpacing(space);
properties.setJc(jc);
p.setPPr(properties);
add(p);
}
public P getP(){
return docx4jParagraph;
}
}
The class WordDocument:
- Code: Select all
public class WordDocument {
private static final Logger log = Logger.getLogger(WordDocument.class);
public enum ALIGNMENT {LEFT, RIGHT, CENTER};
private String templateFile = "./src/sv/vorlage.docx";
private String dateiname = "";
private String headerTitel = "";
private MainDocumentPart mainDocumentPart = null;
private ObjectFactory objectFactory = null;
private static WordprocessingMLPackage wordMLPackage = null;
private PgMar pgMar;
private Body body;
public WordDocument(String[] fileinfos){
if (log.isInfoEnabled())
log.info("WordDocument - ENTER");
//liest aus dem uebergebenen Array die fileinfos
getFileinfos(fileinfos);
try {
objectFactory = new ObjectFactory();
wordMLPackage = WordprocessingMLPackage.createPackage();
mainDocumentPart = wordMLPackage.getMainDocumentPart();
body = mainDocumentPart.getJaxbElement().getBody();
//Default Seitenabstaende setzen
PageDimensions page = new PageDimensions();
pgMar = page.getPgMar(); //default 1,44 cm Abstand
SectPr sectPr = objectFactory.createSectPr(); //Section Properties
body.setSectPr(sectPr);
sectPr.setPgMar(pgMar); //Page Margin setzen
//fuege die Formatvorlagen und Header / Footer dem Dokument hinzu
loadPartsFromTemplate();
} catch (Exception e) {
log.error("WordDocument - Exception", e);
}
}
public boolean add(WordParagraph wP){
if(wP == null) return false;
mainDocumentPart.addObject(wP.getP());
return true;
}