显示仅在第一页上使用itext生成的pdf中的页数 [英] Show the number of pages in a pdf generated using itext only on the first page

查看:520
本文介绍了显示仅在第一页上使用itext生成的pdf中的页数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用itext 2.1.7和java servlet创建一个PDF文件,我通过覆盖PdfpageEvents插入页眉和页脚 onEndPage onCloseDocument 我成功地将页面/页脚中的页面x放在页眉/页脚中

I am creating a PDF file using itext 2.1.7 and java servlets where I am inserting header and footer by overriding PdfpageEvents the onEndPage and onCloseDocument i am successfully able to put the pagev X of Y in the header/footer

现在我想计算页面的成本(即印刷成本)。所以我需要找到不。 pdf中的页面(即假设打印页面成本为3我需要计算整个pdf的数量(3 * pdf页面总数))并且捕获量我还需要显示成本在第一页上,只在同一页的第一页

Now I want to calculate the cost of the pages (i.e. Cost of printing). So for that I need find the no. of pages in the pdf(i.e. Suppose printing of a page costs 3 I need to calculate the amount(3 * total no. of pages in pdf) for the entire pdf) and the catch is I also need to display the cost on the first page and only on the first page of the same pdf.

我尝试在 onCloseDocument上打印但是这会在每一页上打印成本

I tried printing it in the onCloseDocument but that printed the cost on each and every page

我怎样才能完成这项工作?我有错吗?

How can I get this done is there something that I am doing wrong?

此处创建PDF文件的Servlet文件的代码

Here code of the Servlet file that creates the PDF file

Document document = new Document(PageSize.A4, 20, 20, 20, 50);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(request.getRealPath("/") +"NoticeReports/"+filename+".pdf"));
MyPageEvents events = new MyPageEvents();
writer.setPageEvent(events);
document.open();
/*
 * put some content in the pdf
 *
 */
document.close();

我正在引用具有以下方法的MyPageEvents类:

I am referencing the MyPageEvents class which has the following methods:

 int costofpage=3;

// we override the onEndPage method
public void onEndPage(PdfWriter writer, Document document) {

    int pageN = writer.getPageNumber();
    String text = "Page " + pageN + " of ";
    float len = bf.getWidthPoint(text, 8);
    cb.beginText();
    cb.setFontAndSize(bf, 8);
    cb.setTextMatrix(280, 20);
    cb.showText(text);
    cb.endText();



    cb.moveTo(0, 30);
    cb.lineTo(600, 30);
    cb.moveTo(0, 820);
    cb.lineTo(600, 820);
    cb.stroke();

    cb.addTemplate(template, 280 + len, 20);
}

// we override the onCloseDocument method
public void onCloseDocument(PdfWriter writer, Document document) {
    template.beginText();
    template.setFontAndSize(bf, 8);
    template.showText(String.valueOf(writer.getPageNumber() - 1));
    template.endText();
}

我应该怎样做才能获得(总页数* costofpage) pdf的第一页?我应该在哪里放置代码来实现这个目标?

What should I do to get the (total no of pages * costofpage) in the first page of the pdf? Where should I put the code to achieve this?

推荐答案

@Shamit这样可行,但有一种更有效的方法同样的事情。

@Shamit That'll work, but there's a more efficient way of doing the same thing.

在SM09的OnEndPage:

In SM09's OnEndPage:


    if (this is the first page) {
      create two templates, one for the "page x of y" and one for the pricing info
      add them to the page
    } else {
      create the one template as usual.
      add it to the page
    }

在OnCloseDocument中,您只需更新两个模板,而不是一个模板。原始模板已存在于所有页面上,但新模板仅在第一页上。

In your OnCloseDocument, you just update both templates, instead of the one. The original template is already present on all the pages, but the new one will only be on the first page.

这篇关于显示仅在第一页上使用itext生成的pdf中的页数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆