title: Creating a PDF with itextsharp works fine in development but fails when deployed to IIS 7
body: <p>I am deploying and testing an application using IIS 7. It creates a PDF using ITextSharp, which works fine when running from VS2010. When running from IIS, I get an error: The document has no pages.
  Using the same data with the VS2010 development server, the PDF is printed OK. The error has nothing to do with a lack of pages.
  The itextsharp.dll is included in the bin folder under the IIS Web Site.
  What am I missing?</p>
  
  <p>Here is my code (though I do not tjhink it is relevant);</p>
  
  <pre><code>    protected void TextSharpMethod()
      {
          // get order details
          #region getOrdersData
          var ordersData = DataAccessLayer.OrdersDataHelpers.getOrderDataByOrderId(_intOrderId);
  
          #endregion
  
  
          if (ordersData != null)
          {
              #region getSupplierDetails
              // get supplier details
              var rowSupplier = (from s in _dataContextOrders.FMSSuppliers
                                 where s.ID == ordersData.SupplierID
                                 select new
                                     {
                                         s.ID,
                                         s.CommonShortName,
                                         s.ExternalRef,
                                         s.Name,
                                         s.Address,
                                         s.SupplierDetail.Telephone,
                                         s.SupplierDetail.WebAddress,
                                         s.SupplierDetail.Email,
                                         s.SupplierDetail.Fax
                                     }).FirstOrDefault();
              #endregion
  
              #region getOrderLineData
              var orderLineData = (from ol in _dataContextOrders.OrderLines
                                   join o in _dataContextOrders.Orders on ol.OrderID equals o.ID
                                   join ec in _dataContextOrders.FMSExpenseCodes on ol.OrderLineExpenseCodeID equals ec.ID into group1
                                   from g1 in group1.DefaultIfEmpty()
                                   join cc in _dataContextOrders.FMSCostCentres on ol.OrderLineCostCentreID equals cc.ID into group2
                                   from g2 in group2.DefaultIfEmpty()
  
                                   select new
                                       {
                                           ol.ID,
                                           ol.OrderID,
                                           ol.OrderLineNumber,
                                           ol.OrderLineQty,
                                           ol.OrderLineDescription,
                                           ol.OrderLineUnitCost,
                                           ol.OrderLineUnitTax,
                                           ol.OrderLineTotal,
                                           ol.OrderLineExpenseCodeID,
                                           ol.GoodsReceivedQty,
                                           ol.InvoicedQty,
                                           ol.OrderLineCostCentreID,
                                           ol.OrderLineVatRate,
                                           ol.OrderLineUnitGross,
  
                                           ExpenseCode = g1.ExternalRef == null ? "" : g1.ExternalRef,
                                           CostCodeRef = g2.ExternalRef == null ? "" : g2.ExternalRef
                                       }).Where(ol =&gt; ol.OrderID == _intOrderId).OrderBy(ol =&gt; ol.OrderLineNumber);
              #endregion
  
              using (MemoryStream ms = new MemoryStream())
              using (Document document = new Document(PageSize.A4, 25, 25, 30, 30))
              using (PdfWriter writer = PdfWriter.GetInstance(document, ms))
              {
                  document.AddCreator("SVSIT");
                  document.AddTitle("PURCHASE ORDER");
                  document.Open();
  
                  #region setupPdfDoc
  
                  // LOGO
                  string path = Server.MapPath("/");
  
                  string imageLocation = path + "/Images/southView_integration_logo_applications.jpg";
                  imageLocation = imageLocation.Replace("\\", "/");
                  iTextSharp.text.Image bmp =
                      iTextSharp.text.Image.GetInstance(imageLocation);
                  // caution - image may not work if app is moved
                  bmp.Alignment = Element.ALIGN_LEFT;
                  bmp.ScalePercent(50f);
                  document.Add(bmp);
  
                  // COMPANY ADDRESS
  
                  PdfPTable table = new PdfPTable(2);
                  PdfPCell cell;
                  //actual width of table in points (A4=595points)
                  table.TotalWidth = 550f;
                  table.LockedWidth = true;
  
                  float[] widths = new float[] {2.75f, 2.75f};
                  table.SetWidths(widths);
                  // 0=no border, 1=border
                  table.DefaultCell.BorderWidth = 0;
                  PdfBlankCell(table);
                  PdfAddTableCell(table, "PURCHASE ORDER", _bold);
                  PdfBlankCell(table);
  
  
                  float flXPos = 842; // 842 - height of A4 in points
                  table.WriteSelectedRows(0, -1, document.Left, flXPos, writer.DirectContent);
  
                  // CONTACT PERSON
                  table = new PdfPTable(4);
                  //actual width of table in points (A4=595points)
  
                  table.TotalWidth = 550f;
                  table.LockedWidth = true;
  
                  widths = new float[] {1f, 1.75f, 1.35f, 1.35f};
                  table.SetWidths(widths);
                  // 0=no border, 1=border
                  table.DefaultCell.BorderWidth = 0;
  
                  PdfAddTableCell(table, "Contact Person", _norm);
  
                  PdfAddTableCell(table, ordersData.OrderContactName, _norm);
  
                  PdfAddTableCell(table, "Tel: " + ordersData.OrderContactTel, _norm);
  
                  PdfAddTableCell(table, "Fax: " + ordersData.OrderContactFax, _norm);
  
                  flXPos = 715;
                  table.WriteSelectedRows(0, -1, document.Left, flXPos, writer.DirectContent);
  
                  // SUPPLIER DETAILS
  
                  table = new PdfPTable(1);
                  table.TotalWidth = 270f;
                  table.LockedWidth = true;
                  table.DefaultCell.BorderWidth = 1; // has border
                  PdfAddTableCell(table, "To (Supplier)", _bold);
  
                  table.AddCell(ordersData.SupplierName);
  
                  if (rowSupplier != null)
                  {
                      table.AddCell(rowSupplier.Address); // might want to break this up by line breaks
                  }
                  else
                  {
                      table.AddCell(" ");
                  }
  
                  flXPos = 695;
                  table.WriteSelectedRows(0, -1, document.Left, flXPos, writer.DirectContent);
  
                  // ORDER NO
                  table = new PdfPTable(1);
                  table.TotalWidth = 275f;
                  table.LockedWidth = true;
  
                  table.DefaultCell.BorderWidth = 0;
                  table.DefaultCell.BorderWidthTop = 1;
                  table.DefaultCell.BorderWidthLeft = 1;
                  table.DefaultCell.BorderWidthRight = 1;
  
  
                  PdfAddTableCell(table, "Order No: " + ordersData.PORef + " / 100630", _bold);
  
                  table.DefaultCell.BorderWidthTop = 0;
                  table.DefaultCell.BorderWidthLeft = 1;
                  table.DefaultCell.BorderWidthRight = 1;
  
                  PdfAddTableCell(table, " ", _bold);
                  PdfBlankCell(table);
  
                  PdfAddTableCell(table, "Date: " + ordersData.CreatedDate.ToShortDateString (), _bold);
  
                  PdfAddTableCell(table, "Your ref: " + ordersData.OtherRef, _bold);
  
                  PdfAddTableCell(table, " ", _bold);
                  PdfAddTableCell(table, "PURCHASE ORDER NUMBER MUST BE", _bold);
  
                  table.DefaultCell.BorderWidthTop = 0;
                  table.DefaultCell.BorderWidthLeft = 1;
                  table.DefaultCell.BorderWidthRight = 1;
                  table.DefaultCell.BorderWidthBottom = 1;
  
                  PdfAddTableCell(table, "QUOTED ON INVOICE TO ENSURE PAYMENT", _bold);
  
  
                  table.WriteSelectedRows(0, -1, document.Left + 275, flXPos, writer.DirectContent);
                  flXPos = 595;
  
                  // DELIVERY ADDRESS
  
                  table = new PdfPTable(1);
                  table.TotalWidth = 270f;
                  table.LockedWidth = true;
                  table.DefaultCell.BorderWidth = 1; // has border
                  PdfAddTableCell(table, "Delivery Address:", _bold);
  
                  table.AddCell(ordersData.DeliveryContactName);
  
                  table.AddCell(ordersData.DeliveryAddress);
  
  
  
                  table.WriteSelectedRows(0, -1, document.Left, flXPos, writer.DirectContent);
  
                  // INVOICE ADDRESS
  
                  table = new PdfPTable(1);
                  table.TotalWidth = 275f;
                  table.LockedWidth = true;
  
                  table.DefaultCell.BorderWidth = 0;
  
                  table.DefaultCell.BorderWidthTop = 1;
                  table.DefaultCell.BorderWidthLeft = 1;
                  table.DefaultCell.BorderWidthRight = 1;
  
                  PdfAddTableCell(table, "Invoice Address:", _bold);
  
                  table.DefaultCell.BorderWidthTop = 0;
                  table.DefaultCell.BorderWidthLeft = 1;
                  table.DefaultCell.BorderWidthRight = 1;
  
                  PdfAddTableCell(table, "          CVCHA", _norm);
                  PdfAddTableCell(table, "          11 High Street", _norm);
                  PdfAddTableCell(table, "          Castle Vale", _norm);
                  PdfAddTableCell(table, "          Birmingham", _norm);
  
                  table.DefaultCell.BorderWidthTop = 0;
                  table.DefaultCell.BorderWidthLeft = 1;
                  table.DefaultCell.BorderWidthRight = 1;
                  table.DefaultCell.BorderWidthBottom = 1;
  
                  PdfAddTableCell(table, "          B35 7PR", _norm);
  
                  table.WriteSelectedRows(0, -1, document.Left + 275, flXPos, writer.DirectContent);
  
                  // ORDER LINES
  
                  table = new PdfPTable(5);
                  table.TotalWidth = 550f;
                  table.LockedWidth = true;
                  widths = new float[] {2.70f, .7f, .7f, .7f, .7f};
                  table.SetWidths(widths);
                  table.DefaultCell.BorderWidth = 1;
  
                  PdfAddTableCell(table, "Description", _bold);
                  PdfAddTableCell(table, "Code", _bold);
                  PdfAddTableCell(table, "Quantity", _bold);
                  PdfAddTableCell(table, "Unit Cost", _bold);
                  PdfAddTableCell(table, "Total Cost", _bold);
                  // 13 rows
                  // get order lines data
                  int i1 = 0;
                  foreach (var orderLine in orderLineData)
                  {
                      if (orderLine != null)
                      {
                          PdfAddTableCell(table, orderLine.OrderLineDescription, _norm);
                          PdfAddTableCell(table, orderLine .CostCodeRef +", " + orderLine.ExpenseCode , _norm); // code - to do
                          PdfAlignRightTableCell(table, orderLine.OrderLineQty.ToString(), _norm);
                          PdfAlignRightTableCell (table, orderLine.OrderLineUnitCost.ToString(), _norm);
                          PdfAlignRightTableCell(table, orderLine.OrderLineTotal.ToString(), _norm);
                          i1++;
                      }
                  }
                  // fill remaining grid
                  for (int i = i1; i &lt;= 13; i++)
                  {
                      PdfAddTableCell(table, " ", _norm);
                      PdfAddTableCell(table, " ", _norm);
                      PdfAddTableCell(table, " ", _norm);
                      PdfAddTableCell(table, " ", _norm);
                      PdfAddTableCell(table, " ", _norm);
                  }
  
                  // DELIVERY DATE / TOTAL LINE
  
  
                  PdfAddTableCell(table, "Required Delivery Date: " + ordersData.ExpectedDeliveryDate, _bold);
  
                  _chunk = new Chunk("Total:                £", _bold );
                  cell = new PdfPCell(new Phrase(_chunk));
                  cell.Colspan = 3;
                  table.DefaultCell.BorderWidth = 1;
                  table.AddCell(cell);
  
                  _chunk = new Chunk(ordersData.TotalAmount.ToString("0.00"), _bold);
                  cell = new PdfPCell(new Phrase(_chunk));
                  cell.Colspan = 1;
  
                  table.DefaultCell.VerticalAlignment  = Element.ALIGN_RIGHT;
                  table.DefaultCell.HorizontalAlignment = Element.ALIGN_RIGHT;
                  table.AddCell(cell);
  
                  flXPos = 490;
                  table.WriteSelectedRows(0, -1, document.Left, flXPos, writer.DirectContent);
  
                  // PREPARED BY / AUTHORISED BY
  
                  table = new PdfPTable(3);
                  table.TotalWidth = 550f;
                  table.LockedWidth = true;
                  widths = new float[] {1.83f, 1.83f, 1.83f};
                  table.SetWidths(widths);
                  table.DefaultCell.BorderWidth = 1;
  
                  PdfAddTableCell(table, "Prepared By:" + "", _bold);
                  PdfAddTableCell(table, "Signature:" + "", _bold);
                  PdfAddTableCell(table, "Date:" + "", _bold);
  
                  PdfAddTableCell(table, "Authorised By:" + "", _bold); // ApproverPersonID
                  PdfAddTableCell(table, "Signature:" + "", _bold);
                  PdfAddTableCell(table, "Date:" + "", _bold);
  
                  flXPos = 195;
                  table.WriteSelectedRows(0, -1, document.Left, flXPos, writer.DirectContent);
  
                  // FOOTER
  
                  table = new PdfPTable(1);
                  table.TotalWidth = 550f;
                  table.LockedWidth = true;
                  table.DefaultCell.BorderWidth = 0;
                  PdfAddTableCell(table, "Distibution of Copies", _norm);
                  PdfAddTableCell(table,
                                  "1. White - Supplier     2. Yellow - Finance Section     3. Pink - Originator (Budget Holder)",
                                  _norm);
                  PdfAddTableCell(table, "Industrial &amp; Provident Society No. 28414R     Housing Corporation No. L4118",
                                  _norm);
  
                  flXPos = 95;
                  table.WriteSelectedRows(0, -1, document.Left, flXPos, writer.DirectContent);
  
                  #endregion
  
                  document.Close();
                  writer.Close();
  
                  #region writeToDatabase
  
                  MemoryStream ms2 = new MemoryStream(ms.ToArray());
  
                  WritePdfToDatabase(ms2);
  
                  #endregion
  
                  Response.ContentType = "pdf/application";
                  Response.AddHeader("content-disposition", "attachment;filename=PurchaseOrder.pdf");
                  Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
  
              }
          }
      }
  
      protected void WritePdfToDatabase(MemoryStream ms2)
      {
          BinaryReader br = new BinaryReader(ms2);
          Byte[] bytes = br.ReadBytes((Int32)ms2.Length);
          br.Close();
  
          DocumentsDataHelper.UpdateDocument(bytes, "pdf/application", "Purchase Order",_intOrderId ,1);
      }
  
  
      protected void PdfBlankCell(PdfPTable table)
      {
          _chunk = new Chunk("", _norm);
          _phrase = new Phrase(_chunk);
          table.AddCell(_phrase);
      }
  
      protected void PdfAddTableCell(PdfPTable table, string s, Font f)
      {
          _chunk = new Chunk(s, f);
          _phrase = new Phrase(_chunk);
          table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
          table.AddCell(_phrase);
      }
      protected void PdfAlignRightTableCell(PdfPTable table, string s, Font f)
      {
          _chunk = new Chunk(s, f);
          _phrase = new Phrase(_chunk);
          table.DefaultCell.HorizontalAlignment = Element.ALIGN_RIGHT;
          table.AddCell(_phrase);
      }
      protected void PdfTableFieldAndHeader(string heading, string value, PdfPTable table)
      {
          PdfAddTableCell(table, heading, _bold);
          PdfAddTableCell(table, value, _norm);
      }
      protected void PdfBoldAndNormLine(string text1, string text2, Document document)
      {
  
          Chunk c1 = new Chunk(text1, FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12.0f, iTextSharp.text.Font.BOLD));
          Chunk c2 = new Chunk(text2, FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12.0f, iTextSharp.text.Font.NORMAL));
          Phrase p1 = new Phrase();
          p1.Add(c1);
          p1.Add(c2);
          Paragraph p = new Paragraph();
  
          p.Add(p1);
          p.Alignment = Element.ALIGN_LEFT;
          document.Add(p);
      }
  </code></pre>