2016-11-15 186 views
1

我想添加页脚到我的word文档由apache poi库生成,问题是我的方法总是只在最后一页添加页脚文本,我错过了什么?谢谢,这 波纹管我的方法添加页脚Word apache poi java

private void addWordFooter(XWPFDocument document, CTBody body, String clientDate, 
     String graphName, long TabWidth) throws IOException, InvalidFormatException { 

    CTSectPr sectPr = body.getSectPr(); 
    if(sectPr==null) 
    { 
     sectPr = body.addNewSectPr(); 
    } 


    CTP footerCtp = CTP.Factory.newInstance(); 
    CTR footerCtr = footerCtp.addNewR(); 
    XWPFParagraph footerCopyrightParagraph = new XWPFParagraph(footerCtp, document); 
    document.getProperties().getExtendedProperties().getUnderlyingProperties().getPages(); 
    XWPFRun run = footerCopyrightParagraph.getRun(footerCtr); 
    run.setText(graphName); 
    run.addTab(); 
    run.setText(clientDate); 
    setTabStop(footerCtp, STTabJc.Enum.forString("right"), BigInteger.valueOf(TabWidth)); 

    XWPFParagraph[] footerParagraphs = { footerCopyrightParagraph }; 

    XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(document, sectPr); 
    headerFooterPolicy.createFooter(STHdrFtr.DEFAULT, footerParagraphs); 
} 

的setTabStop方法:

private void setTabStop(CTP oCTP, STTabJc.Enum oSTTabJc, BigInteger oPos) { 
    CTPPr oPPr = oCTP.getPPr(); 
    if (oPPr == null) { 
     oPPr = oCTP.addNewPPr(); 
    } 

    CTTabs oTabs = oPPr.getTabs(); 
    if (oTabs == null) { 
     oTabs = oPPr.addNewTabs(); 
    } 

    CTTabStop oTabStop = oTabs.addNewTab(); 
    oTabStop.setVal(oSTTabJc); 
    oTabStop.setPos(oPos); 
} 

回答

0

一些测试,我认为你只是在最后的体调用该函数后。

addWordFooter()中删除该参数CTBody body

这行添加到您的功能

CTSectPr sectPr = d.getDocument().getBody().addNewSectPr(); 

这将适用页脚到整个.DOCX。

与你的问题相反,我试图添加页脚到最后一页,并通过传递特定的CTBody body我可以复制你的问题。