title: Converting Multiple Images into Multiple Pages PDF using itextsharp
body: <p>I have requirement to <strong>convert images</strong> into <strong>PDF with a template</strong>. I'm able to create one page PDF using itextsharp. Assumption <strong>1 image for 1 page PDF</strong>.
  The problem comes when I have <strong>multiple images</strong>. How I can create multiple pages PDF with predefined template.
  Below is my code:</p>
  
  <pre><code>public static string CreatePDFDocument(string docname, string imagePath, string mediaField)
      {
          PdfReader pdfReader = null;
          string pdfPortrait = ConfigurationManager.AppSettings["PdfPortraitTemplate"];
          string pdfLandscape = ConfigurationManager.AppSettings["PdfLandscapeTemplate"];
  
          iTextSharp.text.Image instanceImg = iTextSharp.text.Image.GetInstance(imagePath);
  
          string filename = ConfigurationManager.AppSettings["LocalFolder"] + docname + ".pdf";
  
          FileStream pdfOutputFile = new FileStream(filename, FileMode.Create);
  
          if (instanceImg.ScaledHeight &gt;= instanceImg.ScaledWidth)
          {
              pdfReader = new PdfReader(pdfPortrait);
          }
          else
          {
              pdfReader = new PdfReader(pdfLandscape);
          }
  
          PdfStamper pdfStamper = null;
          pdfStamper = new PdfStamper(pdfReader, pdfOutputFile);
  
          AcroFields testForm = pdfStamper.AcroFields;
          testForm.SetField("MediaField", mediaField);
  
          PdfContentByte overContent = pdfStamper.GetOverContent(1);
          IList&lt;AcroFields.FieldPosition&gt; fieldPositions = testForm.GetFieldPositions("ImageField");
  
          if (fieldPositions == null || fieldPositions.Count &lt;= 0) throw new ApplicationException("Error locating field");
          AcroFields.FieldPosition fieldPosition = fieldPositions[0];
          iTextSharp.text.Rectangle imageRect = new Rectangle(fieldPosition.position.Top, fieldPosition.position.Left, fieldPosition.position.Bottom, fieldPosition.position.Right);
  
          instanceImg.ScaleToFit(imageRect.Height, -1 * imageRect.Width);
          instanceImg.SetAbsolutePosition(fieldPosition.position.Left, (fieldPosition.position.Top - (instanceImg.ScaledHeight)));
  
          overContent.AddImage(instanceImg);
          pdfStamper.FormFlattening = true;
  
          pdfStamper.Close();
          pdfReader.Close();
  
          return filename;
      }
  </code></pre>
  
  <p>Thanks in advance!</p>