My code sample is as follows
@Override
public void onEndPage(PdfWriter writer, Document document) {
addFooter(writer);
}
private void addFooter(PdfWriter writer){
PdfPTable footer = new PdfPTable(2);
try {
// set defaults
footer.setWidths(new int[]{2, 24});
footer.setWidthPercentage(50);
footer.setTotalWidth(527);
footer.setLockedWidth(true);
footer.getDefaultCell().setFixedHeight(30);
footer.getDefaultCell().setBorder(Rectangle.TOP);
footer.getDefaultCell().setBorderColor(BaseColor.RED);
// here for the text Page 100 of, word of goes below in next line.
//It should be in same line.
footer.addCell(new Phrase(String.format("Page %d of", Integer.parseInt(writer.getPageNumber()+"33")), new Font(Font.FontFamily.HELVETICA, 8)));
// add placeholder for total page count
PdfPCell totalPageCount = new PdfPCell(total);
totalPageCount.setBorder(Rectangle.TOP);
totalPageCount.setBorderColor(BaseColor.GREEN);
footer.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
footer.addCell(totalPageCount);
// write page
PdfContentByte canvas = writer.getDirectContent();
canvas.beginMarkedContentSequence(PdfName.ARTIFACT);
footer.writeSelectedRows(0, -1, 34, 20, canvas);
canvas.endMarkedContentSequence();
} catch(DocumentException de) {
throw new ExceptionConverter(de);
}
}
}
This code is working fine in case of single digit page number. But when page increases to double digits then few texts get shifted to next line. For example if the text is Page 10 of 10, then word "of" goes in next line.