gpt4 book ai didi

java - Apache POI - 将多个段落添加到同一行的页眉/页脚

转载 作者:行者123 更新时间:2023-11-30 08:07:03 26 4
gpt4 key购买 nike

我正在使用 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

对我做错了什么有什么想法吗?

谢谢,

最佳答案

添加制表位并使用它们

这是我的草稿 - 在 A4 文档上左、中、右打印我的名字。我完全不知道这些位置元素是如何计算的……添加制表位的代码来自Java Apache POI Tab Stop word document

import java.awt.Desktop;
import java.io.*;
import java.math.BigInteger;

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

public class POIExample {
public static void main(String[] args) {
try {
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();

XWPFRun tmpRun = paragraph.createRun();
tmpRun.setText("JAN");
tmpRun.addTab();
tmpRun.setText("JAN");
tmpRun.addTab();
tmpRun.setText("JAN");

BigInteger pos1 = BigInteger.valueOf(4500);
setTabStop(paragraph, STTabJc.Enum.forString("center"), pos1);
BigInteger pos2 = BigInteger.valueOf(9000);
setTabStop(paragraph, STTabJc.Enum.forString("right"), pos2);

File f = File.createTempFile("poi", ".docx");
try (FileOutputStream fo = new FileOutputStream(f)) {
document.write(fo);
}
Desktop.getDesktop().open(f);

} catch (Exception e) {
e.printStackTrace();
}
}

public static void setTabStop(XWPFParagraph oParagraph, STTabJc.Enum oSTTabJc, BigInteger oPos) {
CTP oCTP = oParagraph.getCTP();
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 - Apache POI - 将多个段落添加到同一行的页眉/页脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33940016/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com