【发布时间】:2015-11-28 02:22:26
【问题描述】:
我正在使用 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
关于我做错了什么有什么想法吗?
谢谢,
【问题讨论】:
-
你的意思是像一张桌子?一个左,一个中,一个右?也许是您想要在页面上放置有用的位置的视觉草图
-
是的,这就是基本思想。有点像 bootstrap 的行概念,有 text-left、text-center 和 text-right,但在 word 中。
-
您是否碰巧有一个看起来像什么的代码示例?因为我对如何将其转换为 Java 和 Apache POI 有点困惑,因为我不确切知道如何在 CTText 对象上设置间距(如果这是我应该使用的)。
标签: java apache-poi xwpf