我正在使用Apace POI处理一些文档,我想添加一个由多个段落组成的页眉/页脚,但我希望它们显示在同一行 .
这是我到目前为止的尝试:
XWPFDocument document = new XWPFDocument();
// adding header and footer
CTP ctp = CTP.Factory.newInstance();
CTR ctr = ctp.addNewR();
// create footer components
CTText footerCopyrightText = ctr.addNewT();
footerCopyrightText.setStringValue("\u00A9" + " My Website - " + Calendar.getInstance().get(Calendar.YEAR));
CTText footerPageText = ctr.addNewT();
footerPageText.setStringValue(document.getProperties().getExtendedProperties().getUnderlyingProperties().getPages() + "");
XWPFParagraph footerCopyrightParagraph = new XWPFParagraph( ctp, document );
footerCopyrightParagraph.setAlignment(ParagraphAlignment.CENTER);
XWPFParagraph footerPageParagraph = new XWPFParagraph(ctp, document);
footerPageParagraph.setAlignment(ParagraphAlignment.RIGHT);
XWPFParagraph[] footerParagraphs = {footerCopyrightParagraph, footerPageParagraph};
CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(document, sectPr );
headerFooterPolicy.createFooter(STHdrFtr.DEFAULT, footerParagraphs);
然而,到目前为止的最终结果是我得到一个右对齐的文本,它由两个连接起来的XWPFParagraph组成 .
我还在Stack Overflow上检查了一些其他示例(有一个用于Header,但我没有设法让它工作) .
我想要实现的基本想法是:http://imgur.com/jrwVO0F
关于我做错的任何想法?
谢谢,
2 回答
添加Tabstops并使用它们
这是我的草稿 - 在A4文档上打印我的名字左,中,右 . 我不知道如何计算这些位置元素...添加tabstops的代码来自Java Apache POI Tab Stop word document
所以,经过一些修补,我终于有了一个功能正常的版本 . 这里希望它对其他用户也有用 .
Creating footer object code
SetTabStop method