Page 1 of 1

Creating a footer respecting default margins

PostPosted: Mon Oct 14, 2013 8:00 am
by RockinRhythm
I am creating a default footer for every section where it's not present. Here's the code:
Code: Select all
      FooterPart footerPart = new FooterPart();
      footerPart.setPackage(wordMLPackage);
      footerPart.getContent().add(createStampP()); // predefined paragraph with a sample text
      Relationship relationship = wordMLPackage.getMainDocumentPart().addTargetPart(footerPart);
      
      SectPr sectPr = sectionWrapper.getSectPr();
      if (sectPr == null) {
         sectPr = factory.createSectPr();
         wordMLPackage.getMainDocumentPart().addObject(sectPr);
         sectionWrapper.setSectPr(sectPr);
      }

      FooterReference footerReference = factory.createFooterReference();
      footerReference.setId(relationship.getId());
      footerReference.setType(hdrFtrRef); // DEFAULT
      sectPr.getEGHdrFtrReferences().add(footerReference);

It is based on this example http://www.docx4java.org/svn/docx4j/trunk/docx4j/src/main/java/org/docx4j/samples/HeaderFooterCreate.java

Now here's the expected outcome: Image
That's a default footer created through LibreOffice.

And here's the actual outcome (programmatically created footer): Image
Ignore the different font size and family, I only care about respecting the default margins in this case.

From what I've found out, the bottom page margin has changed to 0 after the footer creation, not sure why. I want the created footer to respect the page's original margins and appear as in first screenshot (and sorta respect the default footer parameters as if it was made in LibreOffice). Any tips and advices on how to achieve that are welcomed.

Re: Creating a footer respecting default margins

PostPosted: Mon Oct 14, 2013 3:00 pm
by jason
unzip the docx, find the main document part, then have a look at the contents of the sectPr element (feel free to copy it here).

Re: Creating a footer respecting default margins

PostPosted: Tue Oct 15, 2013 1:05 am
by RockinRhythm
jason wrote:unzip the docx, find the main document part, then have a look at the contents of the sectPr element (feel free to copy it here).


I had a look at the sectPr element and found a difference in <w:pgMar bottom=""> value. I am now adjusting this value on the fly, and although it's not the same offset as if the footer/header was created directly in LibreOffice, it's not on the very edge of the page anymore. So thank you for this hint. ;)