title: How to add index with page numbers using itextshap?
body: <p>I’ve a requirement where I need to generate a <strong>PDF</strong> file with <strong>Index along with Page numbers</strong>. I'm using <strong>ItextSharp</strong> for generating PDF. The Document may contains hundreds of pages. The data in the PDF document will be the <strong>dynamic data</strong>.
  Here is my Code to generate the PDF :</p>
  
  <pre><code>Rectangle rec2 = new Rectangle(PageSize.A4);
  using (FileStream fs = new FileStream("D:/Sample1.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
  using (Document doc = new Document(PageSize.A4, 10f, 10f, 10f, 10f))
  using (PdfWriter writer = PdfWriter.GetInstance(doc, fs))
  {
         PdfPageEventHelper pageEventHelper = new PdfPageEventHelper();
         writer.PageEvent = pageEventHelper;
         doc.SetMargins(20, 20, 50, 50);
         doc.NewPage();
         doc.Open();
         var Content = writer.DirectContent;
         var pageBorderRect = new Rectangle(doc.PageSize);
         ………..
         ………………..
         ………………..
         ………..
         doc.Close();
  }
  </code></pre>
  
  <p>And to <strong>add</strong> the <strong>page numbers</strong> to generated document I used the following method</p>
  
  <pre><code>public void AddPageNumber(string BidNumber)
          {
              byte[] bytes = System.IO.File.ReadAllBytes(@"D:\Sample1.pdf");
              Font blackFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, BaseColor.BLACK);
              Font boldfont = FontFactory.GetFont("Arial", 14, Font.BOLD, BaseColor.BLACK);
              using (MemoryStream stream = new MemoryStream())
              {
                  PdfReader reader = new PdfReader(bytes);
                  using (PdfStamper stamper = new PdfStamper(reader, stream))
                  {
                      int pages = reader.NumberOfPages;
                     for (int j = 2; j &lt;= pages; j++)
                      {
                          ColumnText.ShowTextAligned(stamper.GetUnderContent(j), Element.ALIGN_RIGHT, new Phrase((j - 1).ToString(), blackFont), 568f, 15f, 0);
                      }
                  }
                  bytes = stream.ToArray();
              }
              System.IO.File.WriteAllBytes(@"D:\newPDF.pdf", bytes);
          }
  </code></pre>
  
  <p>Now, I need to create and add the <strong>Index with page numbers</strong> for this document. Here my problem is the index content also is dynamic data which will be some data in the document.
  While searching for my requirement, I found this,</p>
  
  <pre><code>int chapTemplateCounter = 0;
      public override void OnChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title)
      {
          base.OnChapter(writer, document, paragraphPosition, title);
  
          BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
  
          tableOfContentsTemplateList[chapTemplateCounter].BeginText();
          tableOfContentsTemplateList[chapTemplateCounter].SetFontAndSize(bfTimes, 12);
          tableOfContentsTemplateList[chapTemplateCounter].SetTextMatrix(0, 0);
          tableOfContentsTemplateList[chapTemplateCounter].ShowText("" + writer.PageNumber);
          tableOfContentsTemplateList[chapTemplateCounter].EndText();
  
          chapTemplateCounter++;
      }
  </code></pre>
  
  <p>I think it is useful for the static headers. How can I add index with page numbers in document with this code or Is it the correct way to generate PDF using <strong>Itextsharp</strong></p>
  
  <p>Thanks in Advance.</p>