Page 1 of 1

customize header and footer

PostPosted: Tue Sep 18, 2012 9:57 am
by sarad
Hi,
I want to append the text "continued on the following page" in the footer if there is next page. Also , I want to append the text "continued from the previous page.." in the header of the page if it has a previous page.

The following is the footer code I am using. This gives the same footer in all the pages. Could you please suggest me , how can I achieve the customized footer /header.
BTW , I tried footerReference.setType(HdrFtrRef.DEFAULT); changing the type to First and others, but that too went in vain. Thanks !!!

Code: Select all
public void addFooter(WordprocessingMLPackage wordMLPackage) throws InvalidFormatException, JAXBException {

    FooterPart footerPart = new FooterPart();
    Relationship rel = wordMLPackage.getMainDocumentPart().addTargetPart(footerPart);

    String ftrXml = "<w:ftr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">"
            + "<w:p>"
            + "<w:pPr>"
            + "<w:pStyle w:val=\"Footer\"/>"
            + "<w:jc w:val=\"right\"/>"
            + "</w:pPr>"
            + "<w:fldSimple w:instr=\" PAGE \\* MERGEFORMAT \">"
            + "<w:r>"
            + "<w:rPr>"
            + "<w:noProof/>"
            + "</w:rPr>"
            + "</w:r>"
            + "</w:fldSimple>"
            + "</w:p>"
            + "</w:ftr>";




    Ftr ftr = (Ftr) XmlUtils.unmarshalString(ftrXml);
    footerPart.setJaxbElement(ftr);

    // Now Create foooter reference
    ObjectFactory objectFactory = new ObjectFactory();
    List<SectionWrapper> sections = wordMLPackage.getDocumentModel().getSections();

    SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
    // There is always a section wrapper, but it might not contain a sectPr
    if (sectPr == null) {
      sectPr = objectFactory.createSectPr();
      wordMLPackage.getMainDocumentPart().addObject(sectPr);
      sections.get(sections.size() - 1).setSectPr(sectPr);
    }

    FooterReference footerReference = objectFactory.createFooterReference();
    footerReference.setId(rel.getId());
    footerReference.setType(HdrFtrRef.DEFAULT);
    sectPr.getEGHdrFtrReferences().add(footerReference);



  }

customize header and footer

PostPosted: Tue Sep 18, 2012 10:37 am
by sarad
Hi,
I want to customized the header and footer. To start with, I want to append text " continued on the following page" on the footer of a page if it has a following page. Similarly, I want to add "continued from previous page" on the header from second page till the last but not on the first.

I have a dynamic html which I am rendering in the body of the document as altChunk. So, I don't know how many pages the file would have / where the page will start and end in advance. The whole thing has to be done in run time.


I used the following implementation:

Code: Select all
public void addFooter(WordprocessingMLPackage wordMLPackage) throws InvalidFormatException, JAXBException {

    FooterPart footerPart = new FooterPart();
    Relationship rel = wordMLPackage.getMainDocumentPart().addTargetPart(footerPart);

    String ftrXml = "<w:ftr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">"
            + "<w:p>"
            + "<w:pPr>"
            + "<w:pStyle w:val=\"Footer\"/>"
            + "<w:jc w:val=\"right\"/>"
            + "</w:pPr>"
            + "<w:r>"
            + "<w:t xml:space=\"preserve\">PAGE </w:t>"
            + "</w:r>"
            + "<w:fldSimple w:instr=\" PAGE \\* MERGEFORMAT \">"
            + "<w:r>"
            + "<w:rPr>"
            + "<w:noProof/>"
            + "</w:rPr>"
            //+"<w:t>17</w:t>"  // remove this?
            + "</w:r>"
            + "</w:fldSimple>"
            + "</w:p>"
            + "</w:ftr>";

    Ftr ftr = (Ftr) XmlUtils.unmarshalString(ftrXml);
    footerPart.setJaxbElement(ftr);

    // Now Create foooter reference
    ObjectFactory objectFactory = new ObjectFactory();
    List<SectionWrapper> sections = wordMLPackage.getDocumentModel().getSections();

    SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
    // There is always a section wrapper, but it might not contain a sectPr
    if (sectPr == null) {
      sectPr = objectFactory.createSectPr();
      wordMLPackage.getMainDocumentPart().addObject(sectPr);
      sections.get(sections.size() - 1).setSectPr(sectPr);
    }

    FooterReference footerReference = objectFactory.createFooterReference();
    footerReference.setId(rel.getId());
    footerReference.setType(HdrFtrRef.DEFAULT);
    sectPr.getEGHdrFtrReferences().add(footerReference);



  }


This gives me the page number on every page. Well so far a good start. However, I tried to play with DefAULT/ first /even in footerReference.setType(HdrFtrRef.DEFAULT); , but in vain.
Could you please suggest how to approach this problem.

Thanks!!!

Re: customize header and footer

PostPosted: Tue Sep 18, 2012 4:03 pm
by jason
You can append the text "continued from the previous page.." in the header of the page if there is a previous page, just by having a different first page header (ie one which doesn't say that).

Appending the text "continued on the following page" in the footer if there is next page, is going to be harder. You might be able to do it using macros or fields. I'd suggest you ask in a Word users forum to find out. It would be easy if you could have a new section before the last page (as opposed to text just flowing over into it).

In any case, the trick is to get it working with a docx in Word, and then to look at the resulting XML then replicate that using docx4j. If you have any problems once you get to that last step, then please post the XML and the code you have written, and we may be able to help.