如何在iText java中的新记录上添加页眉和页脚?

时间:2013-02-08 04:24:33

标签: java itext

我正在使用Itext 2.1.3。如果在所有页面上调用document.newPage()方法,则如何将标头添加到PDF。我正在打印发票。如果一张发票超过一页,则标题不应显示在第二页中。在新发票上我希望标题显示 以下是我的代码:

pdf.InsertEsvheader(imagePaths[0], imagePaths[1], ap.getCarrierCode(), " ");

public HeaderFooter Insertheader(String logo1, String logo2, String carrierName,String Title){

        Font pageHeaderFont = FontFactory.getFont(null, 10);
        pageHeaderFont.setStyle(Font.BOLD);
        Table table = null;
        HeaderFooter header=null;
        try {

            table = new Table(4);
            table.setBorder(0);

            table.setWidth(100);

            Image img1 = createImage(logo1, 115, 35, Image.LEFT, Image.MIDDLE);

            Image img2 = createImage(logo2, 115, 35, Image.ALIGN_JUSTIFIED, Image.MIDDLE);

            Cell pCell1 = new Cell(img1);
            pCell1.setBorder(0);

            Cell pCell3 = new Cell(new Phrase(Title,pageHeaderFont));
            pCell3.setHorizontalAlignment(Element.ALIGN_CENTER);
            pCell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
            pCell3.setColspan(2);
            pCell3.setBorder(0);

            Cell pCell5 = new Cell(new Phrase("",pageHeaderFont));
            pCell5.setHorizontalAlignment(Element.ALIGN_CENTER);
            pCell5.setVerticalAlignment(Element.ALIGN_MIDDLE);
            pCell5.setColspan(4);
            pCell5.setBorder(0);

            Cell pCell4 = new Cell(img2);
            pCell4.setBorder(0);

            table.addCell(pCell1);
            table.addCell(pCell3);
            table.addCell(pCell4);
            table.addCell(pCell5);      


            PdfPTable ptable = new PdfPTable(1);
            ptable.setWidthPercentage(90);
            PdfPCell pcell = new PdfPCell(new Phrase(" " ));
            pcell.setHorizontalAlignment(Element.ALIGN_LEFT);
            pcell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            pcell.setBorder(0);
            ptable.addCell(pcell);

            Phrase hp = new Phrase();
            hp.add(table);
            //hp.add(ptable);
            hp.add(ptable);

            header = new HeaderFooter(hp, false);
            header.setBorder(Rectangle.NO_BORDER);
            document.setHeader(header);



        } catch (Exception de) {
            System.err.println(de.getMessage());
        } 
        return header;

}

0 个答案:

没有答案