- Code: Select all
objectFactory = new ObjectFactory();
wordMLPackage = WordprocessingMLPackage.createPackage();
mainDocumentPart = wordMLPackage.getMainDocumentPart();
Relationship rHeader = createHeaderPart();
createHeaderReference(wordMLPackage, rHeader);
Relationship rFooter = createFooterPart();
createFooterReference(wordMLPackage, rFooter);
setStyleDefintionsPart();
This works very fine. But now I want to set my page margins to a specific value.
I already have method and PgMar Object.
- Code: Select all
private PgMar pgMar = null;
public void setMargins(long top, long right, long left, long bottom){
if(pgMar == null) pgMar = new PgMar();
pgMar.setTop( BigInteger.valueOf(top));
pgMar.setBottom( BigInteger.valueOf(bottom));
pgMar.setLeft( BigInteger.valueOf(left));
pgMar.setRight( BigInteger.valueOf(right));
return;
}
But I dont know how to add the margin to my document. I think I have to add a SecPr Object to the body, but how can I get the body?
I only found this example from the GettingStarted Guide:
- Code: Select all
// Create the package
WordprocessingMLPackage wordMLPackage = new WordprocessingMLPackage();
// Create the main document part (word/document.xml)
MainDocumentPart wordDocumentPart = new MainDocumentPart();
// Create main document part content
ObjectFactory factory = Context.getWmlObjectFactory();
org.docx4j.wml.Body body = factory .createBody();
org.docx4j.wml.Document wmlDocumentEl = factory .createDocument();
17
wmlDocumentEl.setBody(body);
// Put the content in the part
wordDocumentPart.setJaxbElement(wmlDocumentEl);
// Add the main document part to the package relationships
// (creating it if necessary)
wmlPack.addTargetPart(wordDocumentPart);
But I don't want to replace wordMLPackage = WordprocessingMLPackage.createPackage();
with this lot of code, because every time I try I get different NullPointerExceptions (guess there are some parts of the document missing which I should create by my own if I dont use createPackage).
Is there an easy way to get the body object of my document after I used
- Code: Select all
wordMLPackage = WordprocessingMLPackage.createPackage();