开发者

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

开发者 https://www.devze.com 2023-02-21 19:48 出处:网络
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 page

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 m开发者_StackOverflowethods:

 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?


@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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消