java - 表格中的页码
问题描述
我创建了一种方法,可以在 Word 文档页脚中添加一个表格,其中包含一行和三列。现在我正在尝试将页码添加到中间列,而我发现的只是直接在页脚上添加页码。我已经使用了该代码,但我无法做到。有人可以帮忙吗?
这是适用于表格的代码
static public void footer(XWPFDocument doc) {
CTSectPr sectPr = doc.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy footerPolicy= new XWPFHeaderFooterPolicy(doc, sectPr);
XWPFFooter footer = footerPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);
XWPFRun run;
// create table in footer
XWPFParagraph pgh1 = footer.createParagraph();
XmlCursor cursor = pgh1.getCTP().newCursor();
XWPFTable table = footer.insertNewTbl(cursor);
XWPFTableRow row = table.getRow(0);
if (row == null) row = table.createRow();
int twipsPerInch = 1440;
table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(7 * twipsPerInch));
XWPFTableCell cell = row.getCell(0);
if (cell == null) cell = row.createCell();
CTTblWidth tblWidth = cell.getCTTc().addNewTcPr().addNewTcW();
tblWidth.setW(BigInteger.valueOf(2 * twipsPerInch));
tblWidth.setType(STTblWidth.DXA);
pgh1 = cell.getParagraphs().get(0);
run = pgh1.createRun();
run.setText("blah blah blah");
cell = row.getCell(1);
if (cell == null) cell = row.createCell();
tblWidth = cell.getCTTc().addNewTcPr().addNewTcW();
tblWidth.setW(BigInteger.valueOf(3 * twipsPerInch));
tblWidth.setType(STTblWidth.DXA);
XWPFParagraph pageNumberParagraph = cell.getParagraphs().get(0);
pageNumberParagraph.setAlignment(ParagraphAlignment.CENTER);
run = pageNumberParagraph.createRun();
run.setText("1");
cell = row.getCell(2);
if (cell == null) cell = row.createCell();
tblWidth = cell.getCTTc().addNewTcPr().addNewTcW();
tblWidth.setW(BigInteger.valueOf(2 * twipsPerInch));
tblWidth.setType(STTblWidth.DXA);
pgh1 = cell.getParagraphs().get(0);
pgh1.setAlignment(ParagraphAlignment.RIGHT);
run = pgh1.createRun();
run.setText("blah blah blah");
}
然后我尝试使用它添加页码,但我无法弄清楚
CTPageNumber pgNum = sectPr.isSetPgNumType() ? sectPr.getPgNumType() : sectPr.addNewPgNumType();
pgNum.setStart(BigInteger.valueOf(1));
解决方案
请参阅如何在使用 apache poi api 创建 word 文档时以 X of Y 格式添加页码?了解如何在Word
.
为此,请创建一个文本运行并插入字段"PAGE \\* MERGEFORMAT"
和/或"NUMPAGES \\* MERGEFORMAT"
在这些文本运行中。这也是Word
GUI 的作用。
...
run = paragraph.createRun();
run.setText("Page ");
paragraph.getCTP().addNewFldSimple().setInstr("PAGE \\* MERGEFORMAT");
run = paragraph.createRun();
run.setText(" of ");
paragraph.getCTP().addNewFldSimple().setInstr("NUMPAGES \\* MERGEFORMAT");
...
那些具有这些字段的文本当然也可以在表格单元格中。
完整示例:
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.wp.usermodel.HeaderFooterType;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageMar;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
import org.apache.xmlbeans.XmlCursor;
import java.math.BigInteger;
public class CreateWordHeaderFooterTable {
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
// the body content
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run=paragraph.createRun();
run.setText("The Body:");
paragraph = document.createParagraph();
run=paragraph.createRun();
run.setText("Lorem ipsum.... page 1");
paragraph = document.createParagraph();
run=paragraph.createRun();
run.addBreak(BreakType.PAGE);
run.setText("Lorem ipsum.... page 2");
paragraph = document.createParagraph();
run=paragraph.createRun();
run.addBreak(BreakType.PAGE);
run.setText("Lorem ipsum.... page 3");
// create header start
XWPFHeader header = document.createHeader(HeaderFooterType.DEFAULT);
paragraph = header.getParagraphArray(0);
if (paragraph == null) paragraph = header.createParagraph();
paragraph.setAlignment(ParagraphAlignment.LEFT);
run = paragraph.createRun();
run.setText("The Header");
// create footer start
XWPFFooter footer = document.createFooter(HeaderFooterType.DEFAULT);
// create table in footer
paragraph = footer.getParagraphArray(0);
if (paragraph == null) paragraph = footer.createParagraph();
XmlCursor cursor = paragraph.getCTP().newCursor();
XWPFTable table = footer.insertNewTbl(cursor);
XWPFTableRow row = table.getRow(0); if (row == null) row = table.createRow();
int twipsPerInch = 1440;
table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(7 * twipsPerInch));
for (int i = 0; i < 3; i++) {
XWPFTableCell cell = row.getCell(i); if (cell == null) cell = row.createCell();
CTTblWidth tblWidth = cell.getCTTc().addNewTcPr().addNewTcW();
tblWidth.setW(BigInteger.valueOf(((i==1)?3:2) * twipsPerInch));
tblWidth.setType(STTblWidth.DXA);
paragraph = cell.getParagraphs().get(0);
run = paragraph.createRun();
if (i == 0) {
paragraph.setAlignment(ParagraphAlignment.LEFT);
run.setText("Left footer text");
} else if (i == 1) {
paragraph.setAlignment(ParagraphAlignment.CENTER);
run.setText("Page ");
paragraph.getCTP().addNewFldSimple().setInstr("PAGE \\* MERGEFORMAT");
run = paragraph.createRun();
run.setText(" of ");
paragraph.getCTP().addNewFldSimple().setInstr("NUMPAGES \\* MERGEFORMAT");
} else if (i == 2) {
paragraph.setAlignment(ParagraphAlignment.RIGHT);
run.setText("Right footer text");
}
}
FileOutputStream out = new FileOutputStream("CreateWordHeaderFooterTable.docx");
document.write(out);
out.close();
document.close();
}
}
推荐阅读
- javascript - 如何获取文本输入中写入的文本
- optimization - 如何在 CPLEX 中运行模拟实验(迭代过程)?
- angular - 如何从同一局域网内的另一台计算机访问 Big Sur 上的 localhost:4200 (Angular App)?
- python - 使用 perl 进行格式化的 Python 子进程给出不完整的输出
- php - 使用 .htaccess 时未加载 CSS
- sql - prisma introspect 生成无效模型
- python - 更改类的属性不会触发 setter 并且打印它不会触发 getter
- firebase - 使用 dart 在函数外使用局部变量
- wso2 - wso2 与 orion 和 Authzforce 的集成以及访问控制
- javascript - 获取 dom 元素中的文本,但不是子元素