Export Div Content To PDF Using ITextSharp

This article explains how to export content in a div to a PDF using iTextSharp.

What is ITextSharp

iTextSharp is a free and open source assembly that helps to convert page output or HTML content in A PDF file.

You can download IT from here,

Now add that DLL to the application.

Getting Started

Start Visual Studio and create a new website in ASP.Net and add these 2 DLLs to the solution.

  • Itextsharp.dll
  • itextsharp.pdfa.dll

Now add these namespaces:

  • using iTextSharp.text; 
  • using iTextSharp.text.pdf;
  • using iTextSharp.text.html;
  • using iTextSharp.text.html.simpleparser;
  • using System.Data.SqlClient;
  • using System.Configuration;

This is my div content,

  1. <div id="employeelistDiv" runat="server">  
  2.     <table border="1">  
  3.         <tr>  
  4.             <td colspan="2"> <b>Employee Detail</b> </td>  
  5.         </tr>  
  6.         <tr>  
  7.             <td><b>EmployeeID:</b></td>  
  8.             <td>  
  9.                 <asp:Label ID="lblEmployeeId" runat="server"></asp:Label>  
  10.             </td>  
  11.         </tr>  
  12.         <tr>  
  13.             <td><b>FirstName:</b></td>  
  14.             <td>  
  15.                 <asp:Label ID="lblFirstName" runat="server"></asp:Label>  
  16.             </td>  
  17.         </tr>  
  18.         <tr>  
  19.             <td><b>LastName:</b></td>  
  20.             <td>  
  21.                 <asp:Label ID="lblLastName" runat="server"></asp:Label>  
  22.             </td>  
  23.         </tr>  
  24.         <tr>  
  25.             <td><b>City:</b></td>  
  26.             <td>  
  27.                 <asp:Label ID="lblCity" runat="server"></asp:Label>  
  28.             </td>  
  29.         </tr>  
  30.         <tr>  
  31.             <td><b>Region:</b></td>  
  32.             <td>  
  33.                 <asp:Label ID="lblState" runat="server"></asp:Label>  
  34.             </td>  
  35.         </tr>  
  36.         <tr>  
  37.             <td><b>Postal Code:</b></td>  
  38.             <td>  
  39.                 <asp:Label ID="lblPostalCode" runat="server"></asp:Label>  
  40.             </td>  
  41.         </tr>  
  42.         <tr>  
  43.             <td><b>Country:</b></td>  
  44.             <td>  
  45.                 <asp:Label ID="lblCountry" runat="server"></asp:Label>  
  46.             </td>  
  47.         </tr>  
  48.     </table>  
  49. </div>  
  50. <asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" />  

Code behind

  1. protected void Page_Load(object sender, EventArgs e) {  
  2.     if (!IsPostBack) {  
  3.         string ConString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;  
  4.         SqlConnection con = new SqlConnection(ConString);  
  5.         SqlCommand cmd = new SqlCommand();  
  6.         cmd.CommandText = "SELECT TOP 1 EmployeeID, LastName, FirstName, Title, BirthDate, City, Region, PostalCode, Country FROM Employees";  
  7.         cmd.Connection = con;  
  8.         con.Open();  
  9.         DataTable dt = new DataTable();  
  10.         dt.Load(cmd.ExecuteReader());  
  11.         if (dt.Rows.Count > 0) {  
  12.             //Bind Datatable to Labels  
  13.             lblEmployeeId.Text = dt.Rows[0]["EmployeeID"].ToString();  
  14.             lblFirstName.Text = dt.Rows[0]["FirstName"].ToString();  
  15.             lblLastName.Text = dt.Rows[0]["LastName"].ToString();  
  16.             lblCity.Text = dt.Rows[0]["City"].ToString();  
  17.             lblState.Text = dt.Rows[0]["Region"].ToString();  
  18.             lblPostalCode.Text = dt.Rows[0]["PostalCode"].ToString();  
  19.             lblCountry.Text = dt.Rows[0]["Country"].ToString();  
  20.         }  
  21.         con.Close();  
  22.     }  
  23. }  
  24. protected void btnExport_Click(object sender, EventArgs e) {  
  25.     Response.ContentType = "application/pdf";  
  26.     Response.AddHeader("content-disposition""attachment;filename=Panel.pdf");  
  27.     Response.Cache.SetCacheability(HttpCacheability.NoCache);  
  28.     StringWriter stringWriter = new StringWriter();  
  29.     HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);  
  30.     employeelistDiv.RenderControl(htmlTextWriter);  
  31.     StringReader stringReader = new StringReader(stringWriter.ToString());  
  32.     Document Doc = new Document(PageSize.A4, 10 f, 10 f, 100 f, 0 f);  
  33.     HTMLWorker htmlparser = new HTMLWorker(Doc);  
  34.     PdfWriter.GetInstance(Doc, Response.OutputStream);  
  35.     Doc.Open();  
  36.     htmlparser.Parse(stringReader);  
  37.     Doc.Close();  
  38.     Response.Write(Doc);  
  39.     Response.End();  
  40. }  
  41. public override void VerifyRenderingInServerForm(Control control) {}  

img1.jpg

Image 1.

img2.jpg

Image 2.

For more information, download the attached sample application.


Similar Articles