Itext pageNumber以PDF格式显示

时间:2011-11-04 13:25:44

标签: java pdf-generation itext

大家好我有一个java类,用于在iText PDF中显示页眉和页脚。

页码显示在页面顶部,页脚在页脚部分正确显示.. 现在我想在页脚位置显示页码...

以下代码使用

public HeaderAndFooter() {

header = new Phrase("**** Header ****");

footer = new PdfPTable(1);

footer.setTotalWidth(130);

footer.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);

footer.addCell(new Phrase(new Chunk(

"**** Footer summary****")

.setAction(new PdfAction(PdfAction.FIRSTPAGE))));

}

public void onEndPage(PdfWriter writer, Document document) {

PdfContentByte cb = writer.getDirectContent();

ColumnText.showTextAligned(cb, Element.ALIGN_RIGHT, new Phrase(String.format("%d", writer.getPageNumber()),

PDFUtil.getNormalFont()), (document

.right() - document.left())

/ 2 + document.leftMargin(), document.top() + 10, 0);

footer.writeSelectedRows(0, -1,

(document.right() - document.left() - 300) / 2

+ document.leftMargin(), document.bottom() - 10, cb);

}


}

我得到像这样的输出

                                        1 (header)

2011年8月26日Rentaldetails 8月---- 5500月

杂---- 500 总计--- 5500

                                                                                                               **** Footer summary (footer)
                            **** 

请建议我如何在页脚部分显示页面编号...提前感谢xxx

3 个答案:

答案 0 :(得分:1)

伙计们,我已经找到了解决方案并且也在课堂上进行了分析......这里是itzzz ......

public class HeaderAndFooter extends PdfPageEventHelper {

public void onEndPage (PdfWriter writer, Document document) {
    Rectangle rect = writer.getBoxSize("art");
    switch(writer.getPageNumber() % 2) {
    case 0:
        ColumnText.showTextAligned(writer.getDirectContent(),                            Element.ALIGN_RIGHT, new Phrase("even header"),
                rect.getBorderWidthRight(), rect.getBorderWidthTop(), 0);
        break;
    case 1:
        ColumnText.showTextAligned(writer.getDirectContent(),
                Element.ALIGN_CENTER, new Phrase(String.format("%d", writer.getPageNumber())),
                300f, 62f, 0);
        break;
    }

下面的代码,你必须在你的打印类中给出最后字节流排出... 那里你必须给予

baos = new ByteArrayOutputStream();
        Document document = new Document(PageSize.A4, 60, 60, 120, 80);
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        HeaderAndFooter event = new HeaderAndFooter();
        writer.setPageEvent(event);
        document.open();

对于PageNumber生成,它的工作正常而简单.... 注意:我使用过iText 2.1.0.jar

答案 1 :(得分:1)

来自iTEXT的完整工人阶级

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.ExceptionConverter;
import com.lowagie.text.Image;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;

class TableHeader extends PdfPageEventHelper {
    /** The header text. */
    String header;
    /** The template with the total number of pages. */
    PdfTemplate total;

    /**
     * Allows us to change the content of the header.
     * 
     * @param header
     *            The new header String
     */
    public void setHeader(String header) {
        this.header = header;
    }

    /**
     * Creates the PdfTemplate that will hold the total number of pages.
     * 
     * @see com.itextpdf.text.pdf.PdfPageEventHelper#onOpenDocument(com.itextpdf.text.pdf.PdfWriter,
     *      com.itextpdf.text.Document)
     */
    public void onOpenDocument(PdfWriter writer, Document document) {
        total = writer.getDirectContent().createTemplate(30, 16);
    }

    /**
     * Adds a header to every page
     * 
     * @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(com.itextpdf.text.pdf.PdfWriter,
     *      com.itextpdf.text.Document)
     */
    public void onEndPage(PdfWriter writer, Document document) {
        PdfPTable table = new PdfPTable(3);
        try {
            table.setWidths(new int[] { 24, 24, 2 });
            table.setTotalWidth(527);
            table.setLockedWidth(true);
            table.getDefaultCell().setFixedHeight(20);
            table.getDefaultCell().setBorder(Rectangle.BOTTOM);
            table.addCell(header);
            table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(String.format("Page %d of", writer.getPageNumber()));
            PdfPCell cell = new PdfPCell(Image.getInstance(total));
            cell.setBorder(Rectangle.BOTTOM);
            table.addCell(cell);
            table.writeSelectedRows(0, -1, 34, 50, writer.getDirectContent());
        } catch (DocumentException de) {
            throw new ExceptionConverter(de);
        }
    }

    /**
     * Fills out the total number of pages before the document is closed.
     * 
     * @see com.itextpdf.text.pdf.PdfPageEventHelper#onCloseDocument(com.itextpdf.text.pdf.PdfWriter,
     *      com.itextpdf.text.Document)
     */
    public void onCloseDocument(PdfWriter writer, Document document) {
        ColumnText.showTextAligned(total, Element.ALIGN_LEFT, new Phrase(String.valueOf(writer.getPageNumber() - 1)), 2, 2, 0);
    }
}

答案 2 :(得分:0)

它有关于添加页码的信息(仅在页面中,而不是特别在页脚中)How to add total page number on every page with iText?