[英]How to add page header with some information and footer with page number in .docx file using docx4j with Java?
[英]Add footer text and page number in same line on docx file with docx4j java
我正在嘗試在同一行的 .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);
}
我一直在考慮在頁腳上添加一個表格或類似的東西,以將元素放在同一行中,但似乎我把這些東西復雜化了。
或者我可以將頁碼附加到文本段落
你怎么看?
提前致謝!
您可以在 Word 中以任何方式執行此操作,例如,使用制表位。 (或者如你所說,表格,但如果你想要居中然后向右,我會說一個中心對齊然后一個右對齊的標簽)
最簡單的方法是在 Word 中正確設置,然后使用 docx4j webapp 或 Docx4j Helper Word AddIn,從該示例文檔生成相應的 Java 代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.