Page 1 of 1

Page Numbering in PDF doesn't work

PostPosted: Sat Apr 08, 2017 6:13 am
by alan1
I'm trying to add the page numbering in my document, I added the following code

Code: Select all
FooterPart footerPart = new FooterPart();
         footerPart.setPackage(wordMLPackage);
         footerPart.setRelationshipType(Namespaces.FOOTER);
         P paragrafo = factory.createP();
         PPr ppr = factory.createPPr();
         Jc jc = factory.createJc();
         jc.setVal(JcEnumeration.RIGHT);
         ppr.setJc(jc);
         PStyle pStyle = new PStyle();
         pStyle.setVal("Footer");
         ppr.setPStyle(pStyle);
         ParaRPr rPrPageNumber = factory.createParaRPr();
         RStyle rStyle = factory.createRStyle();
         rStyle.setVal("PageNumber");
         rPrPageNumber.setRStyle(rStyle);
         ppr.setRPr(rPrPageNumber);
         paragrafo.setPPr(ppr);
         
         
         R r = factory.createR();
         CTSimpleField ctSimple = factory.createCTSimpleField();
         ctSimple.setInstr("PAGE");
         JAXBElement<CTSimpleField> fldSimple = factory.createPFldSimple(ctSimple);

         R rSimple = factory.createR();
         RPr rprSimple = factory.createRPr();
         rprSimple.setNoProof(new BooleanDefaultTrue());
         Text tSimple = factory.createText();
         tSimple.setValue("1");
         rSimple.setRPr(rprSimple);
         rSimple.getContent().add(tSimple);
         fldSimple.getValue().getContent().add(rSimple);
         r.getContent().add(fldSimple);
         paragrafo.getContent().add(r);
         
         footerPart.getContent().add(paragrafo);
         
         P paragrafo2 = factory.createP();
         PPr ppr2 = factory.createPPr();
         Ind ind = factory.createPPrBaseInd();
         ind.setRight(BigInteger.valueOf(360));
         ppr2.setPStyle(pStyle);
         ppr2.setInd(ind);
         paragrafo2.setPPr(ppr2);
         footerPart.getContent().add(paragrafo2);
    
         Relationship addTargetPart = wordMLPackage.getMainDocumentPart().addTargetPart(footerPart);
    
         //Add footer reference
         FooterReference ft = factory.createFooterReference();
         ft.setType(HdrFtrRef.DEFAULT);
         ft.setId(addTargetPart.getId());
    
         SectPr sectPr = factory.createSectPr();
         sectPr.getEGHdrFtrReferences().add(ft);
         ft.setParent(sectPr);
         wordMLPackage.getMainDocumentPart().addObject(sectPr);


When generated in docx format work as expected, but when I convert to pdf using FO the footer with the page number doesn't appear

Re: Page Numbering in PDF doesn't work

PostPosted: Sun Apr 09, 2017 1:43 pm
by jason
Please attach your docx or link to it somewhere

Re: Page Numbering in PDF doesn't work

PostPosted: Mon Apr 10, 2017 11:56 pm
by alan1
This is the docx generated

Re: Page Numbering in PDF doesn't work

PostPosted: Thu Apr 13, 2017 2:18 pm
by jason
Your input docx is invalid, as indicated by the warning:

Code: Select all
WARN org.docx4j.jaxb.JaxbValidationEventHandler .handleEvent line 88 - [ERROR] : unexpected element (uri:"http://schemas.openxmlformats.org/wordprocessingml/2006/main", local:"fldSimple"). Expected ele
INFO org.docx4j.jaxb.JaxbValidationEventHandler .handleEvent line 134 - continuing (with possible element/attribute loss)


This is because you have it nested in w:r:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
        <w:r>
            <w:fldSimple w:instr="PAGE">
                <w:r>
                    <w:rPr>
                        <w:noProof/>
                    </w:rPr>
                    <w:t>1</w:t>
                </w:r>
            </w:fldSimple>
        </w:r>
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


Microsoft's Open XML SDK 2.0 Productivity Tool's validation will pick that up as well.

If you load the docx in Word, then save it again, the page number will generate correctly from that in the FO output.

You should correct your XML so it looks something like:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  <w:p>
    <w:r>
      <w:fldChar w:fldCharType="begin"/>
      <w:instrText>PAGE</w:instrText>
      <w:fldChar w:fldCharType="separate"/>
      <w:t>1</w:t>
      <w:fldChar w:fldCharType="end"/>
    </w:r>
  </w:p>
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


(It looks like the FO output can handle a complex PAGE field, but not a simple one?!)

Re: Page Numbering in PDF doesn't work

PostPosted: Tue May 09, 2017 6:26 am
by alan1
I have a few problems with my notbook, and only today I could see the answer.

I tried the approach mentioned. the result are now as below. but don't work when a convert to pdf usin the method Docx4J.toPDF(wordMLPackage, baos);

Code: Select all
<w:p>
  <w:r> 
     <w:fldChar w:fldCharType="begin"/>
     <w:instrText>PAGE</w:instrText>
     <w:fldChar w:fldCharType="separate"/>
     <w:t>1</w:t>
     <w:fldChar w:fldCharType="end"/>
  </w:r>
</w:p>


My code to generate the footer is
Code: Select all
      ObjectFactory factory = Context.getWmlObjectFactory();
      FooterPart footerPart = new FooterPart();
      footerPart.setPackage(wordMLPackage);
      footerPart.setRelationshipType(Namespaces.FOOTER);
      P paragrafo = factory.createP();
      R r = factory.createR();
      FldChar fldCharBegin = factory.createFldChar();
      fldCharBegin.setFldCharType(STFldCharType.BEGIN);
      r.getContent().add(fldCharBegin);
      Text text = factory.createText();
      JAXBElement<org.docx4j.wml.Text> textWrapped = factory.createRInstrText(text);
      r.getContent().add( textWrapped);
      text.setValue( "PAGE");
      FldChar fldCharSeparate = factory.createFldChar();
      fldCharSeparate.setFldCharType(STFldCharType.SEPARATE);
      r.getContent().add(fldCharSeparate);
      Text textNumber = factory.createText();
      textNumber.setValue("1");
      r.getContent().add(textNumber);
      FldChar fldCharEnd = factory.createFldChar();
      fldCharEnd.setFldCharType(STFldCharType.END);
      r.getContent().add(fldCharEnd);
      paragrafo.getContent().add(r);
      footerPart.getContent().add(paragrafo);
 
      Relationship addTargetPart = wordMLPackage.getMainDocumentPart().addTargetPart(footerPart);
 
      //Add footer reference
      FooterReference ft = factory.createFooterReference();
      ft.setType(HdrFtrRef.DEFAULT);
      ft.setId(addTargetPart.getId());
 
      SectPr sectPr = factory.createSectPr();
      sectPr.getEGHdrFtrReferences().add(ft);
      ft.setParent(sectPr);
      
      wordMLPackage.getMainDocumentPart().addObject(sectPr);


the generated docx is attached