使用itext在pdf中显示总页码错误

时间:2017-06-20 18:39:50

标签: java itext

生成的pdf中的总页数并不是不相符的。

阅读以下帖子PDF Page后,我生成了以下代码,但它显示以下输出:

第一页: -

        Page 1 of 1

第二页: -

第2页,共1页

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

public class LogoHeaderFooter extends PdfPageEventHelper {

    String header;
    int totalNumber = 0;
    PdfTemplate total;

    public void setHeader(String header) {
        this.header = header;
    }

    public void onOpenDocument(PdfWriter writer, Document document) {
        total = writer.getDirectContent().createTemplate(30, 12);
        System.out.println("No : "+writer.getPageNumber());
        System.out.println("PDF Template : "+total);
    }

    public void onStartPage(PdfWriter writer, Document document) {
        //total = writer.getDirectContent().createTemplate(30, 12);
        writer.setPageCount(++totalNumber);
        System.out.println("Page No : "+writer.getPageNumber());
        System.out.println("Total No : "+totalNumber);
        //totalNumber++; 
    }
    public void onEndPage(PdfWriter writer, Document document) {

        Font ffont = new Font(Font.FontFamily.UNDEFINED, 10, Font.NORMAL);

        PdfPTable table = new PdfPTable(3);
        try {
            table.setWidths(new int[]{24, 24, 2});
            table.getDefaultCell().setFixedHeight(10);
            table.getDefaultCell().setBorder(Rectangle.NO_BORDER);

            PdfPCell cell = new PdfPCell();
            cell.setBorder (0);
            cell.setBorderWidthTop (1);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setPhrase(new Phrase("", ffont));
            table.addCell(cell);

            cell = new PdfPCell();
            cell.setBorder (0);
            cell.setBorderWidthTop (1);
            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            cell.setPhrase(new Phrase(String.format("Page %d of", writer.getPageNumber()), ffont));
            table.addCell(cell);

            cell = new PdfPCell(Image.getInstance(total));
            cell.setBorder (0);
            cell.setBorderWidthTop (1);
            table.addCell(cell);
            table.setTotalWidth(document.getPageSize().getWidth()
                    - document.leftMargin() - document.rightMargin());
            table.writeSelectedRows(0, -1, document.leftMargin(),
                    document.bottomMargin() - 15, writer.getDirectContent());

        }
        catch(DocumentException de) {
            throw new ExceptionConverter(de);
        }
    }

    public void onCloseDocument(PdfWriter writer, Document document) {

        Font ffont = new Font(Font.FontFamily.UNDEFINED, 10, Font.NORMAL);
        ColumnText.showTextAligned(total, Element.ALIGN_LEFT,
                new Phrase(String.valueOf(writer.getPageNumber() - 1), ffont),
                2, 1, 0);
    }

}

无法在此代码中找到错误,我们将不胜感激。

1 个答案:

答案 0 :(得分:1)

您的代码对我来说很好,创建文档时一定有问题。

在下面的小测试中,我使用您的助手类创建一个包含三个页面的PDF文档,每个页面都有正确的页脚集( 1/3 2/3 3/3 )。

public static void main(String[] args) throws FileNotFoundException, DocumentException {
    Document document = new Document();

    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("dest.pdf"));
    LogoHeaderFooter logoHeaderFooter = new LogoHeaderFooter();
    writer.setPageEvent(logoHeaderFooter);

    document.open();

    for (int i = 0; i < 3; ) {
        i++;
        document.add(new Paragraph("Paragraph " + i));
        document.newPage();
    }
    document.close();
}