我试图在.docx文档的页脚上添加小文本(左侧)和页码(右侧),并在同一行中添加页码。
到目前为止,我可以添加文本和页码,但只需2行。
TextVersionv02312
1
但我需要它
TextVersionv02312 1
我用来添加文本和页码的代码是:
private static Ftr createFooter(WordprocessingMLPackage wordMLPackage, String content, ObjectFactory factory, Part sourcePart, InputStream is) throws IOException, Throwable {
Ftr footer = factory.createFtr();
P paragraph = factory.createP();
R run = factory.createR();
/*
* Change the font size to 8 points(the font size is defined to be in half-point
* size so set the value as 16).
*/
RPr rpr = new RPr();
HpsMeasure size = new HpsMeasure();
size.setVal(BigInteger.valueOf(16));
rpr.setSz(size);
run.setRPr(rpr);
Text text = new Text();
text.setValue(content);
run.getContent().add(text);
paragraph.getContent().add(run);
footer.getContent().add(paragraph);
// add page number
P pageNumParagraph = factory.createP();
addFieldBegin(factory, pageNumParagraph);
addPageNumberField(factory, pageNumParagraph);
addFieldEnd(factory, pageNumParagraph);
footer.getContent().add(pageNumParagraph);
return footer;
}
private static void addPageNumberField(ObjectFactory factory, P paragraph) {
R run = factory.createR();
PPr ppr = new PPr();
Jc jc = new Jc();
jc.setVal(JcEnumeration.RIGHT);
ppr.setJc(jc);
paragraph.setPPr(ppr);
Text txt = new Text();
txt.setSpace("preserve");
txt.setValue(" PAGE \\* MERGEFORMAT ");
run.getContent().add(factory.createRInstrText(txt));
paragraph.getContent().add(run);
}
我一直在考虑在页脚上添加一个表格或类似的东西,把元素放在同一行中,但我似乎把事情搞得太复杂了。
或者我可以将页码附加到文本段落中。
你认为如何?
提前谢谢!
发布于 2018-03-16 19:29:13
您可以以任何方式在Word中这样做,例如,使用制表符。(或者就像你说的,桌子,但如果你想要中间对右,我会用中心对齐然后右对齐)
最简单的方法是在Word中正确处理,然后使用docx4j get应用程序或Docx4j Helper,从该示例文档生成相应的Java代码。
https://stackoverflow.com/questions/49324270
复制相似问题