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 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) {}

img1.jpg

Image 1

img2.jpg

Image 2

For more information, download the attached sample application.


Similar Articles