Adding footer to ms word using POI api

前端 未结 2 411
野性不改
野性不改 2020-12-21 07:02

I searched a lot and getting some results in which some sample code is there but no one is working. All are either getting null pointer exception or if document is generated

2条回答
  •  隐瞒了意图╮
    2020-12-21 07:34

    I faced this issue and the solution is We have to use 3.10 final poi jar. 3.9 having this problem.

    Please remove jars of previous version and add jars of 3.10 final version in which this bug is fixed.

    Jars requires are:

    1. poi-3.10-FINAL.jar

    2. poi-ooxml-3.10-FINAL.jar

    3. poi-ooxml-schemas-3.10-FINAL.jar

    Easily available in net:

    http://mvnrepository.com/artifact/org.apache.poi/poi/3.10-FINAL

    XWPFDocument document = new XWPFDocument();
    CTP ctp = CTP.Factory.newInstance();
    CTR ctr = ctp.addNewR();
    CTRPr rpr = ctr.addNewRPr();
    CTText textt = ctr.addNewT();
    textt.setStringValue( " Page 1" );
    XWPFParagraph codePara = new XWPFParagraph( ctp, document );
    XWPFParagraph[] newparagraphs = new XWPFParagraph[1];
    newparagraphs[0] = codePara;
    CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
    XWPFHeaderFooterPolicy headerFooterPolicy = new  XWPFHeaderFooterPolicy( document, sectPr );
    headerFooterPolicy.createFooter( STHdrFtr.DEFAULT, newparagraphs );
    

    The above code is working perfectly, please use 3.10 wars those I mentioned above.

提交回复
热议问题