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));