iText PDF body content Overlapping on Footer part

只愿长相守 提交于 2019-12-08 06:37:35

问题


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

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