table 中的页码
Page number in a table
我创建了一种在 Word 文档页脚中添加一行三列的 table 的方法。现在,我正在尝试将页码添加到中间栏,但我发现的只是将页码直接添加到页脚上。我已经使用了该代码,但我无法做到。
有人可以帮忙吗?
这是适用于 table
的代码
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));
请参阅 How to add page numbers in format X of Y while creating a word document using apache poi api? 了解如何在 Word
中创建页码字段。
为此,创建文本 运行 并在这些文本 运行 中插入字段 "PAGE \* MERGEFORMAT"
and/or "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");
...
具有这些字段的文本 运行 当然也可以在 table 单元格中。
完整示例:
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();
}
}
我创建了一种在 Word 文档页脚中添加一行三列的 table 的方法。现在,我正在尝试将页码添加到中间栏,但我发现的只是将页码直接添加到页脚上。我已经使用了该代码,但我无法做到。 有人可以帮忙吗?
这是适用于 table
的代码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));
请参阅 How to add page numbers in format X of Y while creating a word document using apache poi api? 了解如何在 Word
中创建页码字段。
为此,创建文本 运行 并在这些文本 运行 中插入字段 "PAGE \* MERGEFORMAT"
and/or "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");
...
具有这些字段的文本 运行 当然也可以在 table 单元格中。
完整示例:
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();
}
}