Header overlap in iText5

陌路散爱 提交于 2019-11-28 13:54:18

Set Margin in bottom 20 more than required. e.g. Generally you keep marging from bottom is 40.

document.setMargins(50, 45, 50, 40);

Now, Keep it 60.

writer=PdfWriter.getInstance(document, out);
document.setPageSize(PageSize.A4);
document.setMargins(50, 45, 50, 60);
document.setMarginMirroring(false);

writer.setPageEvent(new HeaderAndFooter());
document.open();

Now in HeaderFooter PageEvent set Footer at document.bottom() - 20 position.

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 cb = writer.getDirectContent();
        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 will solve the problem of overlapping. It is working fine for me.

you need to set the margins properly:

Document(Rectangle pageSize, float marginLeft, float marginRight, float marginTop, float marginBottom)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!