Попробуйте, потому что я тоже страдал от этого, и я также создал эту библиотеку, поэтому после многих неприятностей я получил результат, просто попытался применить логику дважды, как я упоминал в комментариях.
class PdfReportServlet extends HttpServlet
{
public static int total = 0;
public static int getTotal()
{
return total;
}
public static void setTotal(int total) {
PdfReportServlet .total = total;
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String reportFor = request.getParameter("report");
if(!"".equals(reportFor))
{
if(reportFor.equals("pdf"))
{
/*
from here logic starts
*/
response.setContentType("application/pdf");
Document document = new Document(PageSize.LETTER.rotate());
try
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PdfWriter pdfWriter = PdfWriter.getInstance(document, bos);
PdfReport pdfReport = new PdfReport();
pdfWriter.setPageEvent(pdfReport);
document.open();
String id = request.getParameter("id");
DettagliFamigliaPDFReport.generatePDFReport(document,id);
document.close();
/*
* now again give new references to Document and PdfWriter classes.
*/
document = new Document(PageSize.LETTER.rotate());
pdfWriter = PdfWriter.getInstance(document, response.getOutputStream());
PDFReport.setTotalPages(getTotal());/*##PAGES_TOTAL here i set total Pages which i get from above logic */
PdfReport pdfReport = new PdfReport();
pdfWriter.setPageEvent(pdfReport);
document.open();
id = request.getParameter("id");
PDFReport.generatePDFReport(document,id);
document.close();
}
catch(DocumentException de){
de.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
/*
Here below i write the class inherits the PdfPageEventHelper class and also having generatePDFReport() method.
*/
public class PDFReport extends PdfPageEventHelper
{
public static PdfTemplate template;
public static int totalPages=0;
public static int getTotalPages() {
return totalPages;
}
public static void setTotalPages(int totalPages) {
DettagliFamigliaPDFReport.totalPages = totalPages;
}
public static void generatePDFReport(Document document,String id)
{
// here your data to write on page of pdf.
}
@Override
public void onOpenDocument(PdfWriter writer, Document doc)
{
template = writer.getDirectContent().createTemplate(100, 100);
}
int totalPage = 0;
@Override
public void onCloseDocument(PdfWriter writer, Document doc)
{
totalPage = writer.getPageNumber() - 1;/* at the end this method calls and you will get total number of pages.*/
PdfReportServlet.setTotal(totalPage); /* while first time logic of servlet executes then i set the total pages to servlet's variable using this logic.
and second time servlet's logic is executing i passed totalPages to this class variable
where i marked ##PAGES_TOTAL */
}
@Override
public void onStartPage(PdfWriter writer, Document doc) {
// Here i write header logic when each time page starts.
}
@Override`enter code here`
public void onEndPage(PdfWriter writer, Document doc)
{
Rectangle rect = writer.getPageSize();
float width = rect.getWidth()/2;
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();
String footerOne = "This document is printed on date "+dateFormat.format(date)+" - Page. "+writer.getPageNumber()+" of "+getTotalPages();
Paragraph paraOne = new Paragraph(footerOne);
Font fontFooter = new Font();
fontFooter.setSize(8);
fontFooter.setColor(Color.gray);
paraOne.setFont(fontFooter);
Phrase footerPhraseOne = new Phrase(paraOne);
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_CENTER, footerPhraseOne,
width, 30, 0);
}
}