简体   繁体   中英

header is not visible on first page of pdf document created with itext

I'm trying to add headers end footers to each page in my report, but on first page there is no header. I'm overwriting methods onStartPage and onEndPage of PdfPageEventHelper. My code:

public class HeaderFooterPageEvent extends PdfPageEventHelper {

public void onStartPage(PdfWriter writer, Document document) {
    Image leftUpper = Image.getInstance("leftUpper.png");
    leftUpper.setAbsolutePosition(0, 0);
    Chunk chunk = new Chunk(leftUpper, 0, -45);
    HeaderFooter header = new HeaderFooter(new Phrase(chunk), false);
    header.setBorder(Rectangle.NO_BORDER);
    document.setHeader(header);
}

public void onEndPage(PdfWriter writer, Document document) {
    Image img = Image.getInstance("reportFooter.png");
    img.setAbsolutePosition(0, 0);
    img.scaleAbsolute(595, 50)
    writer.getDirectContent().addImage(img);
}

}

Footers are working correctly, but header on first page is missing. What am I doing wrong?

Your code is really strange. You are mixing the obsolete HeaderFooter functionality (it has been removed from iText a long time ago) with the newer page event functionality.

If you want to use the obsolete HeaderFooter class, you shouldn't introduce page events. In this case your mistake is that you didn't set the header before document.open() . Note that you shouldn't use your application in a commercial context if you go for this option, because the version of iText that you are using isn't suited for commercial use as explained (among others) on the Open Source beta on StackExchange.

If you want to use page events (this is recommended), you are violation one of the basic rules: do not add any content in the onStartPage() method. All of this is explained in the free ebook The Best iText Questions on StackOverflow where you will find reference to questions (and the corresponding answers) such as:

Your onStartPage() method contains several errors. Please throw away that code, and adapt your onEndPage() method:

public void onEndPage(PdfWriter writer, Document document) {
    Image img = Image.getInstance("reportFooter.png");
    img.setAbsolutePosition(0, 0);
    img.scaleAbsolute(595, 50)
    writer.getDirectContent().addImage(img);
}

Your onEndPage() methods adds an image at the bottom of the page: the image is used as your footer. If you want to also add a header, you need to adapt this method like this:

public void onEndPage(PdfWriter writer, Document document) {
    Image header = Image.getInstance("reportHeader.png");
    header.setAbsolutePosition(792, 0);
    header.scaleAbsolute(595, 50)
    writer.getDirectContent().addImage(header);
    Image footer = Image.getInstance("reportFooter.png");
    footer.setAbsolutePosition(0, 0);
    footer.scaleAbsolute(595, 50)
    writer.getDirectContent().addImage(footer);
}

In this code snippet, I assume that the page size is A4: 842 by 595 user units, and that there's a margin of 50 user units, because I define the dimensions of the header and the footer as 595 by 50 user units. I add the footer at y = 0 and the header at y = 792 (which is 842 - 50 ). If the margins you defined for your document are smaller than 50, your content and your header or footer will overlap.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM