问题
I use itextpdf-5.5.3 version. When I generate PDF and use PdfPTable it overlapped on Footer part.
Some Code for PDF.
writer=PdfWriter.getInstance(document, out);
document.setPageSize(PageSize.A4);
document.setMargins(50, 45, 50, 40);
document.setMarginMirroring(false);
writer.setPageEvent(new HeaderAndFooter());
document.open();
For Footer,
public class HeaderAndFooter extends PdfPageEventHelper {
private Font footerFont;
public HeaderAndFooter() {
super();
footerFont = getFontObj(BaseColor.LIGHT_GRAY, 15);
footerFont.setStyle(Font.ITALIC);
}
@Override
public void onEndPage(PdfWriter writer, Document document) {
PdfContentByte canvas = writer.getDirectContent();
canvas.rectangle(50, 30, 500, 780);
canvas.setColorStroke(BaseColor.LIGHT_GRAY);
canvas.stroke();
PdfContentByte cb = writer.getDirectContent();
footerFont.setSize(15);
ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, new Phrase("____________________________________________________________",footerFont), ((document.left() + document.right())/2)+1f , document.bottom()+20, 0);
ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, new Phrase(String.format("Page %d", writer.getPageNumber()),footerFont), (document.left() + document.right())/2 , document.bottom(), 0);
}
}
Any idea to avoid Overlapping ?
回答1:
I applied Maths logic.
writer=PdfWriter.getInstance(document, out);
document.setPageSize(PageSize.A4);
document.setMargins(50, 45, 50, 60);
document.setMarginMirroring(false);
Here i increase margin from bottom from 40 to 60.
Then, in HeaderFooter Class I set margin in minus position.
ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, new Phrase("____________________________________________________________",footerFont), ((document.left() + document.right())/2)+1f , document.bottom(), 0);
ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, new Phrase(String.format("Page %d", writer.getPageNumber()),footerFont), (document.left() + document.right())/2 , document.bottom()-20, 0);
It solved a problem of PdfPTable Overlapping on Footer.
来源:https://stackoverflow.com/questions/26214007/itext-pdf-body-content-overlapping-on-footer-part