Example #1
0
  /**
   * Creates document footer including page number
   *
   * @param doc WordprocessingMLPackage for the document.
   * @throws InvalidFormatException
   */
  private void createFooter(WordprocessingMLPackage doc) throws InvalidFormatException {
    MainDocumentPart content = doc.getMainDocumentPart();

    // Create footer
    FooterPart footer = new FooterPart();

    Ftr ftr = objectFactory.createFtr();
    P footerParagraph = objectFactory.createP();

    setStyle(footerParagraph, "footer");

    PPr parProps = objectFactory.createPPr();
    Jc al = objectFactory.createJc();
    al.setVal(JcEnumeration.RIGHT);
    parProps.setJc(al);
    footerParagraph.setPPr(parProps);

    // Add field start
    R run = objectFactory.createR();
    FldChar fldChar = objectFactory.createFldChar();
    fldChar.setFldCharType(STFldCharType.BEGIN);
    run.getContent().add(fldChar);
    footerParagraph.getContent().add(run);

    // Add pageNumber field
    run = objectFactory.createR();
    Text txt = objectFactory.createText();
    txt.setSpace("preserve");
    txt.setValue(" PAGE   \\* MERGEFORMAT ");
    run.getContent().add(objectFactory.createRInstrText(txt));
    footerParagraph.getContent().add(run);

    // Add field end
    run = objectFactory.createR();
    fldChar = objectFactory.createFldChar();
    fldChar.setFldCharType(STFldCharType.END);
    run.getContent().add(fldChar);
    footerParagraph.getContent().add(run);

    ftr.getContent().add(footerParagraph);
    footer.setJaxbElement(ftr);

    Relationship rel = content.addTargetPart(footer);

    // Relate footer to document
    List<SectionWrapper> sections = doc.getDocumentModel().getSections();

    SectPr sectPr = sections.get(sections.size() - 1).getSectPr();

    if (null == sectPr) {
      sectPr = objectFactory.createSectPr();
      content.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);
  }
Example #2
0
  /**
   * Nastaví záhlaví dokumentu. Úprava předchozí funkce.
   *
   * @param irPackage
   * @throws InvalidFormatException
   */
  private Ftr SetupFooter(WordprocessingMLPackage irPackage) throws InvalidFormatException {
    ObjectFactory lrFactory = Context.getWmlObjectFactory();

    /* create header part */
    FooterPart lrFooterPart = new FooterPart();
    lrFooterPart.setPackage(irPackage);

    Ftr lrFooter = lrFactory.createFtr();

    lrFooterPart.setJaxbElement(lrFooter);
    Relationship lrRelationship = irPackage.getMainDocumentPart().addTargetPart(lrFooterPart);
    /* end create header part */

    /* create header reference */
    List<SectionWrapper> lrSections = irPackage.getDocumentModel().getSections();
    SectPr lrSectionPr = lrSections.get(lrSections.size() - 1).getSectPr();
    // There is always a section wrapper, but it might not contain a sectPr
    if (lrSectionPr == null) {
      lrSectionPr = lrFactory.createSectPr();
      irPackage.getMainDocumentPart().addObject(lrSectionPr);
      lrSections.get(lrSections.size() - 1).setSectPr(lrSectionPr);
    }

    FooterReference lrFooterReference = lrFactory.createFooterReference();
    lrFooterReference.setId(lrRelationship.getId());
    lrFooterReference.setType(HdrFtrRef.DEFAULT);
    lrSectionPr.getEGHdrFtrReferences().add(lrFooterReference); // add header or footer references
    /* end create header reference */

    return lrFooter;
  }
 private void createFooterReference(
     WordprocessingMLPackage wordprocessingMLPackage, Relationship relationship)
     throws InvalidFormatException {
   List<SectionWrapper> sections = wordprocessingMLPackage.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 = factory.createSectPr();
     wordprocessingMLPackage.getMainDocumentPart().addObject(sectPr);
     sections.get(sections.size() - 1).setSectPr(sectPr);
   }
   FooterReference footerReference = factory.createFooterReference();
   footerReference.setId(relationship.getId());
   footerReference.setType(HdrFtrRef.DEFAULT);
   sectPr.getEGHdrFtrReferences().add(footerReference);
 }