【问题标题】:Add footer Word apache poi java添加页脚 Word apache poi java
【发布时间】:2017-03-29 05:20:10
【问题描述】:

我想在 apache poi 库生成的 word 文档中添加页脚,我的方法总是在最后一页添加页脚文本的问题,我错过了什么?谢谢, 这是我的方法

 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);
}

【问题讨论】:

    标签: java ms-word apache-poi footer


    【解决方案1】:

    试试这个:

    private void addWordFooter(XWPFDocument document, String text) throws IOException {
        CTP ctp = CTP.Factory.newInstance();
        CTText t = ctp.addNewR().addNewT();
        t.setStringValue(text);
        XWPFParagraph pars[] = new XWPFParagraph[1];
        pars[0] = new XWPFParagraph(ctp, document);
    
        XWPFHeaderFooterPolicy hfp = document.createHeaderFooterPolicy();
        hfp.createFooter(XWPFHeaderFooterPolicy.DEFAULT, pars);
        //hfp.createHeader(XWPFHeaderFooterPolicy.DEFAULT, pars);
    }
    

    【讨论】:

      【解决方案2】:

      经过一些测试,我认为您仅在最后一个主体上调用此函数。

      addWordFooter() 中删除此参数CTBody body

      并将这一行添加到您的函数中

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

      它会将页脚应用于整个 .docx。

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

      【讨论】:

        猜你喜欢
        • 2014-12-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多