Page 1 of 1

How to set Page Margins

PostPosted: Mon Aug 06, 2012 5:56 pm
by sandra
At the moment I create my document using the following code:


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();

Re: How to set Page Margins

PostPosted: Tue Aug 07, 2012 7:46 pm
by jason
sandra wrote:Is there an easy way to get the body object of my document


wordMLPackage.getMainDocumentPart().getJaxbElement().getBody()

Re: How to set Page Margins

PostPosted: Tue Aug 07, 2012 11:43 pm
by sandra
Thank you very much for your help. It works now:

Code: Select all
mainDocumentPart = wordMLPackage.getMainDocumentPart();   
body = mainDocumentPart.getJaxbElement().getBody();
PageDimensions page = new PageDimensions();
pgMar = page.getPgMar();                       
SectPr sectPr = objectFactory.createSectPr();   
body.setSectPr(sectPr);                           
sectPr.setPgMar(pgMar);