Page 1 of 1

Page number in PDF

PostPosted: Tue Aug 01, 2017 1:28 am
by alan1
I'm using the version 3.3.5 of docx4j and 3.3.4 of docx4j-export-fo to generade the document. I'm trying to add page numbering, it's work properly in docx format but in pdf doesn't
That's the code that I use to generate the footer
Code: Select all
    private void addPageNumber(WordprocessingMLPackage wordMLPackage)
         throws Exception {
      // Delete the Styles part, since it clutters up our output
      MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
      Relationship styleRel = mdp.getStyleDefinitionsPart()

      // OK, the guts of this sample:
      // The 2 things you need:
      // 1. the Header part
      Relationship relationship = createFooterPart(wordMLPackage);
      // 2. an entry in SectPr
      createFooterReference(wordMLPackage, relationship);

   public Relationship createFooterPart(
         WordprocessingMLPackage wordprocessingMLPackage) throws Exception {

      FooterPart footerPart = new FooterPart();
      Relationship rel = wordprocessingMLPackage.getMainDocumentPart()

      // After addTargetPart, so image can be added properly

      return rel;

   public void createFooterReference(
         WordprocessingMLPackage wordprocessingMLPackage,
         Relationship relationship) throws InvalidFormatException {
      ObjectFactory objectFactory = Context.getWmlObjectFactory();
      List<SectionWrapper> sections = wordprocessingMLPackage

      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();
         sections.get(sections.size() - 1).setSectPr(sectPr);

      FooterReference footerReference = objectFactory.createFooterReference();
      sectPr.getEGHdrFtrReferences().add(footerReference);// add header or
      // footer references

   public Ftr getFtr(WordprocessingMLPackage wordprocessingMLPackage)
         throws Exception {
      ObjectFactory factory = Context.getWmlObjectFactory();
      Ftr ftr = factory.createFtr();

      P paragrafo = factory.createP();
      PPr ppr = factory.createPPr();
      PStyle pStyle = new PStyle();
      CTFramePr ctFramePr = factory.createCTFramePr();
      ParaRPr rPrPageNumber = factory.createParaRPr();
      RStyle rStyle = factory.createRStyle();

      R r = factory.createR();
      RPr rPrNumber = factory.createRPr();
      FldChar fldCharBegin = factory.createFldChar();

      R r2 = factory.createR();
      Text text = factory.createText();
      JAXBElement<org.docx4j.wml.Text> textWrapped = factory

      R r3 = factory.createR();
      FldChar fldCharEnd = factory.createFldChar();


      return ftr;

when I put a simple text it's appear in pdf, but when I try to put the code to show the page number, doesn't work.

that's the xml generated in footer.
Code: Select all
        <w:pStyle w:val="Footer"/>
        <w:framePr w:wrap="around" w:hAnchor="margin" w:vAnchor="text" w:xAlign="right" w:y="1"/>
            <w:rStyle w:val="PageNumber"/>
            <w:rStyle w:val="PageNumber"/>
        <w:fldChar w:fldCharType="begin"/>
             <w:rStyle w:val="PageNumber"/>
            <w:rStyle w:val="PageNumber"/>
        <w:fldChar w:fldCharType="end"/>

The docx generated is attached