我的代码示例如下
@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);
}
}
}
此代码在个位数页码的情况下工作正常。但是当页面增加到两位数时,很少有文本会转移到下一行。例如,如果文本是第 10 页,共 10 页,则“of”一词会出现在下一行。
千巷猫影
相关分类