Show the number of pages in a pdf generated using itext only on the first page

試著忘記壹切 提交于 2020-01-05 07:44:32

问题


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

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.

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?

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();

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();
}

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?


回答1:


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

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
    }

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.




回答2:


You can:

  1. Create the PDF in-memory (create it on ByteArrayOutputStream)
  2. Process it to add header/footer
  3. Create a new PDF in-memory with watermark
  4. Process the PDF again to merge watermark + number of pages on first page. PDFStamper should help with that.



回答3:


I would have to look up the detailed code, but I would create the document and once done use stamping to add that information to the first page only.

hth

Mario



来源:https://stackoverflow.com/questions/5504988/show-the-number-of-pages-in-a-pdf-generated-using-itext-only-on-the-first-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!