I have created a method that adds a table in the Word document footer with one row and three columns. Now I'm trying to add the page number to the middle column and all I've found is adding the page number directly on the footer. I've use that code but I haven't been able to do it. Can anyone help please?
This is the code that works for the 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");
}
Then I've tried to add the page number using this but I can't figure it out
CTPageNumber pgNum = sectPr.isSetPgNumType() ? sectPr.getPgNumType() : sectPr.addNewPgNumType();
pgNum.setStart(BigInteger.valueOf(1));
See How to add page numbers in format X of Y while creating a word document using apache poi api? for how to create page numbering fields in Word
.
To do so, create a text run and insert fields "PAGE \\* MERGEFORMAT"
and/or "NUMPAGES \\* MERGEFORMAT"
in those text runs. This is what Word
' s GUI also does.
...
run = paragraph.createRun();
run.setText("Page ");
paragraph.getCTP().addNewFldSimple().setInstr("PAGE \\* MERGEFORMAT");
run = paragraph.createRun();
run.setText(" of ");
paragraph.getCTP().addNewFldSimple().setInstr("NUMPAGES \\* MERGEFORMAT");
...
Those text runs having those fields of course also can be in table cells.
Complete example:
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();
}
}