How to Generate Pdf Invoice Dynamically in Asp.Net


Description:-

Here we will Create Invoice File as Pdf file dynamically we will Generate Pdf file as Design with tabular format in dot net. Here we create Dynamically Data Table and Add Some Rows in Table for Checking in Pdf Format. Using StringWriter, HtmlTextWriter, and StringBuilder Generate Pdf file with Some Design in Html Controls Dynamically.

Default.aspx:-

In your Webpage Drag and Drop Button Control and Generate Click event.
Now go to Code behind and Code in Generated button Click Event and Code for Generate Pdf File Dynamically in dot net.

protected void GenerateInvoicePDF(object sender, EventArgs e)
{
  //Dummy data for Invoice (Bill).
  string CustomerName = "Umesh Patel";
  string companyName = "ASPCodder";
  int CustomerID = 1243;
  DataTable dt = new DataTable();
  dt.Columns.AddRange(new DataColumn[5] {
      new DataColumn("Item Id", typeof(string)),
      new DataColumn("Item Name", typeof(string)),
      new DataColumn("Price", typeof(int)),
      new DataColumn("Quantity",  typeof(int)),
      new DataColumn("Total",  typeof(int))});
  dt.Rows.Add(5001, "Wires", 150, 15, 7500);
  dt.Rows.Add(5005, "Cables", 500, 3, 1500);
  dt.Rows.Add(5003, "Pins", 20, 30, 6000);
  dt.Rows.Add(5002, "Lan Wires", 50, 24, 1200);

  using (StringWriter sw = new StringWriter())
  {
    using (HtmlTextWriter hw = new HtmlTextWriter(sw))
    {
      StringBuilder sb = new StringBuilder();

      //Generate Invoice (Bill) Header.
      sb.Append("<table width='100%' cellspacing='0' cellpadding='2'>");
      sb.Append("<tr><td align='center' style='background-color: Blue;color:#ffffff' colspan = '2'><b>CUSTOMER ORDER SHEET</b></td></tr>");
      sb.Append("<tr><td colspan = '2'></td></tr>");

      sb.Append("<tr><td><b>Customer Name: </b>");
      sb.Append(CustomerName);
      sb.Append("</td><td align = 'right'><b>Date: </b>");
      sb.Append(DateTime.Now);
      sb.Append(" </td></tr>");
      sb.Append("<tr><td colspan = '2'>");
      sb.Append("</td></tr>");
      sb.Append("<br />");

      sb.Append("<tr><td><b>Order No: </b>");
      sb.Append(CustomerID);
      sb.Append("</td><td align = 'right'>");
      sb.Append(" </td></tr>");
      sb.Append("<tr><td colspan = '2'><b>Company Name: </b>");
      sb.Append(companyName);
      sb.Append("</td></tr>");
      sb.Append("</table>");
      sb.Append("<br />");

      //Generate Invoice (Bill) Items Grid.
      sb.Append("<table border = '1'>");
      sb.Append("<tr>");
      foreach (DataColumn column in dt.Columns)
      {
        sb.Append("<th style = 'background-color: Blue;color:#ffffff'>");
        sb.Append(column.ColumnName);
        sb.Append("</th>");
      }
      sb.Append("</tr>");
      foreach (DataRow row in dt.Rows)
      {
        sb.Append("<tr>");
        foreach (DataColumn column in dt.Columns)
        {
          sb.Append("<td>");
          sb.Append(row[column]);
          sb.Append("</td>");
        }
        sb.Append("</tr>");
      }
      sb.Append("<tr><td align = 'right' colspan = '");
      sb.Append(dt.Columns.Count - 1);
      sb.Append("'>Total</td>");
      sb.Append("<td>");
      sb.Append(dt.Compute("sum(Total)", ""));
      sb.Append("</td>");
      sb.Append("</tr></table>");

      //Export HTML String as PDF.
      StringReader sr = new StringReader(sb.ToString());
      Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
      HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
      PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
      pdfDoc.Open();
      htmlparser.Parse(sr);
      pdfDoc.Close();
      Response.ContentType = "application/pdf";
      Response.AddHeader("content-disposition", "attachment;filename=" + CustomerName + "-" + CustomerID + ".pdf");
      Response.Cache.SetCacheability(HttpCacheability.NoCache);
      Response.Write(pdfDoc);
      Response.End();
    }
  }
}

Now Add Code in your Webpage and Run in Browser to Generate Pdf File. 

Related Posts

Previous
Next Post »

2 comments

comments
July 6, 2018 at 6:41:00 PM GMT+5:30 delete

Nice Work done....You can also use zetpdf to Generate PDF Invoice. I'll suggest other to use zetpdf in Asp.net

Reply
avatar
May 13, 2020 at 11:15:00 PM GMT+5:30 delete

getting error in document..
document library???

Reply
avatar

Thanks for comments.....