title: Multipage PDF document from predefined template
body: <p>I have a requirement to generate invoice reports in PDF using some predefined company templates. I am able to create/generate <strong>SINGLE PAGE</strong> PDF reports using iTextSharp.</p>
  
  <p>Problem: The problem comes when the invoice statement spans <strong>MULTIPLE PAGES</strong>. I am not able to extend the report(invoice statement) to next(2nd) page. If all the data can not be accommodated on one page it should be written on 2nd page, while still using the company template.</p>
  
  <p>The template is present at following path:</p>
  
  <pre><code>HostingEnvironment.MapPath("~/Content/InvoiceTemplate/invoiceTemplate.pdf")
  </code></pre>
  
  <p>I am using iTextSharp library to create documents. Below is the code used to generate the PDF:</p>
  
  <pre><code>public class pdfStatementController : Controller {
  
          Models.DYNAMICS_EXTEntities _db = new Models.DYNAMICS_EXTEntities();
  
          //
          // GET: /pdfStatement/
  
  
          public ActionResult SendPdfStatement(string InvoiceNumber) {
              try {
                  InvoiceNumber = InvoiceNumber.Trim();
  
                  ObjectParameter[] parameters = new ObjectParameter[1];
                  parameters[0] = new ObjectParameter("InvoiceNumber", InvoiceNumber);
  
                  List&lt;Models.Statement&gt; statementList = new List&lt;Models.Statement&gt;();
                  statementList = _db.ExecuteFunction&lt;Models.Statement&gt;("uspInvoiceStatement", parameters).ToList&lt;Models.Statement&gt;();
                  pdfStatementController.WriteInTemplate(statementList);
  
                  return RedirectToAction("Invoice", "Invoice", new { id = statementList.FirstOrDefault().Customer_ID.ToString().Trim() });
              } catch (Exception e) {
                  return View("Error");
              }
          } 
  
      public static void WriteInTemplate(List&lt;Models.Statement&gt; statementList) {
          string invoiceNumber = statementList.FirstOrDefault().Invoice.ToString().Trim();
          string month = null;
          string day = null;
          string year = null;
  
          PdfReader pdfReader = new PdfReader(
                                    HostingEnvironment.MapPath(
                                         "~/Content/InvoiceTemplate/invoiceTemplate.pdf"));
          FileStream fileStream = new FileStream(
                                     HostingEnvironment.MapPath(
                                        "~/Content/reports/" + invoiceNumber + ".pdf"),
                                        FileMode.Create);
          PdfStamper pdfStamper = new PdfStamper(pdfReader, fileStream);
          AcroFields pdfFields = pdfStamper.AcroFields;
  
          pdfFields.SetField("BillToCompany", statementList.FirstOrDefault().BillToCompany.ToString().Trim().ToUpper());
          pdfFields.SetField("BillToContact", statementList.FirstOrDefault().BillToContact.ToString().Trim().ToUpper());
          pdfFields.SetField("CustomerId", statementList.FirstOrDefault().Customer_ID);
          pdfFields.SetField("InvoiceNumber", statementList.FirstOrDefault().Invoice.ToString().Trim());
          pdfFields.SetField("JobNumber", statementList.FirstOrDefault().JobNumber.ToString().Trim());
          pdfFields.SetField("Caller", statementList.FirstOrDefault().Caller.ToString().Trim());
  
          pdfStamper.FormFlattening = true; // generate a flat PDF 
          pdfStamper.Close();
          pdfReader.Close();
      }
  }
  </code></pre>