iText PDF body content Overlapping on Footer part

2019-05-30 10:06发布

I use itextpdf-5.5.3 version. When I generate PDF and use PdfPTable it overlapped on Footer part.

enter image description here

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条回答
姐就是有狂的资本
2楼-- · 2019-05-30 10:26

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.

查看更多
登录 后发表回答