title: Total page number is displaying wrong in pdf using itext
body: <p>Total Page number is not dispplaying in generated pdf.</p>
  
  <p>After reading the following post <a href="https://stackoverflow.com/questions/11205777/pdf-page-numbering-in-java-itext">PDF Page</a>    I generated the following code, but it show the following output:</p>
  
  <p>First Page:-</p>
  
  <pre><code>        Page 1 of 1
  </code></pre>
  
  <p>Second Page:-</p>
  
  <h2>            Page 2 of 1</h2>
  
  <pre><code>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);
      }
  
  }
  </code></pre>
  
  <p>not able to find the bug in this code, Any help will be appreciated.</p>