Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

java - Header overlap in iText5

I created a header and footer for my PDF document using PdfPTables. I have specified the headers and footers in my PdfPageEventHelper's onStartPage and onEndPage event's respectively.

The issue I am facing is while adding a paragraph to my document.

When I create a new Paragraph as follows:

Paragraph content = new Paragraph("This is a test text");
try{
  pdfDocument.add(content);
} catch (DocumentException e){
  e.printStackTrace();
}

The content overlaps with the header. What I need is to set the pargraph between the header and the footer. Can someone tell me what do I need to do in order to put the pargraph between the header and footer, instead of on them.

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...