Export to PDF Using iTextSharp from ASP.NET Application

Application

I am using the data from the RadGrid here to export.

export

Steps:

Download and add reference to iTextSharp.dll files and others to the project.

Code at the .cs file,

  1. privatevoidExport_ToPDF_InHtmlFormat()  
  2. {#region--Exporting RadGrid Data in Html format to PDF--  
  3.   
  4.         try {  
  5.             string _Name = string.Empty;  
  6.   
  7.             string _ShippingAddress = string.Empty;  
  8.   
  9.             string _CityName = string.Empty;  
  10.   
  11.             string _PinCode = string.Empty;  
  12.   
  13.             string _StateName = string.Empty;  
  14.   
  15.             string _ContactNo = string.Empty;  
  16.   
  17.             // Formatting the body of the Document  
  18.   
  19.             string body = "";  
  20.   
  21.             body = "<html><body style='margin-left:20px;'><img src='http://www.swashconvergence.com/images/logo-kencloud.png'/></body></html>";  
  22.   
  23.             body += "<br />";  
  24.   
  25.             body += "<h1>KenPartner Contact Details</h1>";  
  26.   
  27.             body += "<br />";  
  28.   
  29.             // Looping through the Grid Data  
  30.   
  31.             //RadGridTotalCOD.AllowPaging = false;  
  32.   
  33.             foreach(GridDataItem item inRadGridTotalCOD.MasterTableView.Items)  
  34.             {  
  35.                 // retrieving data from the grid  
  36.   
  37.                 _Name = item["ApplicantName"].Text;  
  38.   
  39.                 _ShippingAddress = item["ShippingAddress"].Text;  
  40.   
  41.                 _CityName = item["CityName"].Text;  
  42.   
  43.                 _PinCode = item["Pincode"].Text;  
  44.   
  45.                 _StateName = item["StateName"].Text;  
  46.   
  47.                 _ContactNo = item["ContactNo"].Text;  
  48.   
  49.   
  50.                 // Configuring the Body of PDF  
  51.   
  52.                 body += "<br /><br />";  
  53.   
  54.                 body += "<h1>To, </h1>";  
  55.   
  56.                 body += "<br /><h1>" + _Name + "</h1> ";  
  57.   
  58.                 body += "<br /><h1>" + _ShippingAddress + "</h1> ";  
  59.   
  60.                 body += "<br /><h1>" + _CityName + " " + "-" + " " + _PinCode + " " + "</h1>";  
  61.   
  62.                 body += "<br /><h1>" + _StateName + "</h1> ";  
  63.   
  64.                 body += "<br /><h1>Contact No" + " " + "-" + " " + _ContactNo + "</h1> ";  
  65.   
  66.                 body += "<br /><br />";  
  67.   
  68.   
  69.             }  
  70.   
  71.             // Export ToPDf  
  72.   
  73.             using(StringWritersw = newStringWriter()) {  
  74.   
  75.                 using(HtmlTextWriterhtw = newHtmlTextWriter(sw)) {  
  76.   
  77.                     htw.Write(body);  
  78.   
  79.                     StringReadersr = newStringReader(sw.ToString());  
  80.   
  81.                     DocumentpdfDoc = newDocument(PageSize.A2, 10 f, 10 f, 10 f, 0 f);  
  82.   
  83.                     HTMLWorkerhtmlparser = newHTMLWorker(pdfDoc);  
  84.   
  85.                     PdfWriter.GetInstance(pdfDoc, Response.OutputStream);  
  86.   
  87.                     // Open The Pdf   
  88.   
  89.                     pdfDoc.Open();  
  90.                     htmlparser.Parse(sr);  
  91.                     pdfDoc.Close();  
  92.   
  93.                     // Current Date  
  94.   
  95.                     stringCurrentDate = DateTime.Now.ToString("dd/MMM/yyyy");  
  96.   
  97.                     Response.ContentType = "application/pdf";  
  98.   
  99.                     // Give the Dacument Name  
  100.   
  101.                     Response.AddHeader("content-disposition""attachment;filename=MyContact " + CurrentDate + ".pdf");  
  102.   
  103.                     Response.Cache.SetCacheability(HttpCacheability.NoCache);  
  104.   
  105.                     Response.Write(pdfDoc);  
  106.   
  107.                     Response.End();  
  108.                 }  
  109.             }  
  110.         } catch (ArgumentNullExceptionExc)   
  111.         {  
  112.         LblErrorMsg.Text = "Application Error : " + Exc.Message;  
  113.     } catch (InvalidCastExceptionExc)  
  114.     {  
  115.         LblErrorMsg.Text = "Application Error : " + Exc.Message;  
  116.     } catch (InvalidOperationExceptionExc)  
  117.     {  
  118.         LblErrorMsg.Text = "Application Error : " + Exc.Message;  
  119.     } catch (FormatExceptionExc)  
  120.     {  
  121.         LblErrorMsg.Text = "Application Error : " + Exc.Message;  
  122.     } catch (ExceptionExc)  
  123.     {  
  124.         LblErrorMsg.Text = "Application Error : " + Exc.Message;  
  125.     }  
  126.  
  127.     #endregion  
  128. }  

Next Recommended Reading Add image in PDF using iTextSharp