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 into A PDF file.
You can download IT from here
Now add that DLL to the application.
Getting started
Start Visual Studio 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
<div id="employeelistDiv" runat="server">
<table border="1">
<tr>
<td colspan="2"> <b>Employee Detail</b> </td>
</tr>
<tr>
<td><b>EmployeeID:</b></td>
<td>
<asp:Label ID="lblEmployeeId" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td><b>FirstName:</b></td>
<td>
<asp:Label ID="lblFirstName" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td><b>LastName:</b></td>
<td>
<asp:Label ID="lblLastName" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td><b>City:</b></td>
<td>
<asp:Label ID="lblCity" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td><b>Region:</b></td>
<td>
<asp:Label ID="lblState" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td><b>Postal Code:</b></td>
<td>
<asp:Label ID="lblPostalCode" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td><b>Country:</b></td>
<td>
<asp:Label ID="lblCountry" runat="server"></asp:Label>
</td>
</tr>
</table>
</div>
<asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" />
Code behind
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
string ConString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(ConString);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT TOP 1 EmployeeID, LastName, FirstName, Title, BirthDate, City, Region, PostalCode, Country FROM Employees";
cmd.Connection = con;
con.Open();
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
if (dt.Rows.Count > 0) {
// Bind DataTable to Labels
lblEmployeeId.Text = dt.Rows[0]["EmployeeID"].ToString();
lblFirstName.Text = dt.Rows[0]["FirstName"].ToString();
lblLastName.Text = dt.Rows[0]["LastName"].ToString();
lblCity.Text = dt.Rows[0]["City"].ToString();
lblState.Text = dt.Rows[0]["Region"].ToString();
lblPostalCode.Text = dt.Rows[0]["PostalCode"].ToString();
lblCountry.Text = dt.Rows[0]["Country"].ToString();
}
con.Close();
}
}
protected void btnExport_Click(object sender, EventArgs e) {
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
employeelistDiv.RenderControl(htmlTextWriter);
StringReader stringReader = new StringReader(stringWriter.ToString());
Document Doc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(Doc);
PdfWriter.GetInstance(Doc, Response.OutputStream);
Doc.Open();
htmlparser.Parse(stringReader);
Doc.Close();
Response.Write(Doc);
Response.End();
}
public override void VerifyRenderingInServerForm(Control control) {}

Image 1

Image 2
For more information, download the attached sample application.

srinivas prabhuPosted May 2, 2024, 12:23 PM
Hi, i am trying to export the letter containing header and footer to pdf but it is not working and it is saying that the file is damaged.
Abdul Amin KhanPosted Aug 15, 2023, 7:22 AM
Can you do this for Me in Asp.net MVC
Nor Azlina MahmudPosted Apr 19, 2022, 7:35 AM
What if the page contain image logo?
Nabin GhimirePosted Nov 10, 2019, 10:53 PM
I got whole aspx page downlaoded
Tileshwari PATELPosted Jul 27, 2019, 11:49 PM
Sir dll file download nhi ho rha what i do...plz help me...
Ashish SharmaPosted Jun 14, 2019, 6:34 AM
Type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"Line 15: warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/> Line 16: <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" Line 17: type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Line 18: warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/>
Ashish SharmaPosted Jun 14, 2019, 6:33 AM
Not working in vb.net..........showing error point on Document(PageSize........)
Naresh SinghalPosted Mar 6, 2018, 12:44 AM
Very important ...this also can be helpful to print html report....thanks...Sir Raj Kumar..
Sunit KumarPosted Aug 14, 2017, 7:05 AM
How can i export div content to pdf using itextsharp having images?
Ashish SrivastavaPosted Nov 17, 2015, 3:36 AM
How can i export div with textbox data in PDF?
Rajeesh MenothPosted Sep 4, 2015, 3:14 AM
Thanks for Sharing sir
Saineshwar BageriPosted Jan 21, 2015, 3:06 AM
for unicode charater how should i use this
Jermaine GrayPosted Nov 29, 2014, 9:25 PM
hello sir am try to used you code but am getting.Control 'ctl00_MainContent_ASPxPopupControl1_GridView1' of type 'GridView' must be placed inside a form tag with runat=server.