生成页脚段落
XWPFDocument document = new XWPFDocument();
/*
* 生成页脚段落
* 给段落设置宽度为占满一行
* */
CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(document, sectPr);
XWPFFooter footer = headerFooterPolicy.createFooter(STHdrFtr.DEFAULT);
System.out.println(footer);
List<XWPFParagraph> paragraphs = footer.getParagraphs();
System.out.println(paragraphs);
XWPFParagraph paragraph = footer.getParagraphArray(0);
System.out.println(paragraph);
paragraph.setAlignment(ParagraphAlignment.CENTER);
paragraph.setVerticalAlignment(TextAlignment.CENTER);
paragraph.setBorderTop(Borders.THICK);
CTTabStop tabStop = paragraph.getCTP().getPPr().addNewTabs().addNewTab();
tabStop.setVal(STTabJc.RIGHT);
int twipsPerInch = 1440;
tabStop.setPos(BigInteger.valueOf(6 * twipsPerInch));
/*
* 给段落创建元素
* 设置元素字面为公司地址+公司电话
* */
XWPFRun run = paragraph.createRun();
setXWPFRunStyle(run, "宋体", 8);
run.addTab();
// String s=run.getFontFamily();
/*
* 生成页码
* 页码右对齐
* */
run = paragraph.createRun();
run.setText("第");
setXWPFRunStyle(run, "宋体", 8);
run = paragraph.createRun();
CTFldChar fldChar = run.getCTR().addNewFldChar();
fldChar.setFldCharType(STFldCharType.Enum.forString("begin"));
setXWPFRunStyle(run, "宋体", 8);
run = paragraph.createRun();
CTText ctText = run.getCTR().addNewInstrText();
ctText.setStringValue("PAGE \\* MERGEFORMAT");
ctText.setSpace(SpaceAttribute.Space.Enum.forString("preserve"));
setXWPFRunStyle(run, "宋体", 8);
fldChar = run.getCTR().addNewFldChar();
fldChar.setFldCharType(STFldCharType.Enum.forString("end"));
run = paragraph.createRun();
run.setText("页/共");
setXWPFRunStyle(run, "宋体", 8);
run = paragraph.createRun();
fldChar = run.getCTR().addNewFldChar();
fldChar.setFldCharType(STFldCharType.Enum.forString("begin"));
run = paragraph.createRun();
ctText = run.getCTR().addNewInstrText();
ctText.setStringValue("NUMPAGES \\* MERGEFORMAT ");
ctText.setSpace(SpaceAttribute.Space.Enum.forString("preserve"));
setXWPFRunStyle(run, "宋体", 8);
fldChar = run.getCTR().addNewFldChar();
fldChar.setFldCharType(STFldCharType.Enum.forString("end"));
run = paragraph.createRun();
run.setText("页");
setXWPFRunStyle(run, "宋体", 8);
标签:createRun,run,页脚,setXWPFRunStyle,fldChar,宋体,paragraph,word,poi3.13
From: https://blog.51cto.com/u_15973676/6069214