title: Locate page number of an iTextSharp Object
body: <p>I'm trying to write an image to a specific page on a pdf file using iTextSharp.  Unfortunately, I have about 10 or 15 different pdf files in which I need to place the image on a different page.  </p>
  
  <p>Example: 
  PDFFile1: Image goes on page 3,</p>
  
  <p>PDFFile2: Image goes on page 6,</p>
  
  <p>PDFFile3: Image goes on page 5 etc...</p>
  
  <p>My current code pulls the number of pages and writes the image on the very last page.  How can I pull the page number where the textbox object "Image" is located?</p>
  
  <pre><code>    private void writePDF(string PhysicalName)
      { 
     try
              {
              string pdfTemplate = HttpContext.Current.Server.MapPath("Documents\\" + PhysicalName);
              string ConsentTemplateName = PhysicalName.Replace(".pdf", "");
              string newFile = HttpContext.Current.Server.MapPath("Documents\\").ToString() + ConsentTemplateName + Session["Number"].ToString() + ".pdf";
  string NewConsentPhysicalPath;
              NewConsentPhysicalPath = newFile;
              PdfReader pdfReader = new PdfReader(pdfTemplate);
              PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create));
              pdfStamper.SetEncryption(PdfWriter.STANDARD_ENCRYPTION_128, null, null, PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_PRINTING);
              AcroFields pdfFormFields = pdfStamper.AcroFields;
  
  iTextSharp.text.Rectangle rect = pdfStamper.AcroFields.GetFieldPositions("Image")[0].position;
              string imageFilePath = HttpContext.Current.Server.MapPath("Documents\\Images" + Convert.ToInt64(Session["Number"].ToString()) + ".png");
              iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(imageFilePath);
              png.ScaleAbsolute(rect.Width, rect.Height);
              png.SetAbsolutePosition(rect.Left, rect.Bottom);
              int numOfPages = pdfReader.NumberOfPages;
              pdfStamper.GetOverContent(numOfPages).AddImage(png); //Get page number of "Image"
              pdfStamper.Close();    
     }
              catch (Exception ex)
              {
                  Response.Write(ex.Message.ToString());
              }
          }
  </code></pre>