Generate PDF from Label using C# in ASP.Net

Last Reply 21 days ago By Adnaan

Posted 21 days ago

I am having a Web Application form in C#.net

After filling the Form instead of Inserting the Data in Database i need to Generate PDF file on Submit Event.

I need to fetch details from the Form - Label

How to do.

Thanks in Advance

regards

Sandesh 

Posted 21 days ago Modified on 20 days ago

Hi learningmrs,

Refer below code.

HTML

<asp:Panel ID="pnlPerson" runat="server">
<table border="1" style="width: 300px;" celpadding="0" cellspacing="0">
    <tr>
        <th colspan="2" style="background-color: #B8DBFD; color: #333; font-weight: bold;">
            <b>Personal Details</b>
        </th>
    </tr>
    <tr>
        <td><b>Name:</b></td>
        <td>
            <asp:Label ID="lblName" runat="server"></asp:Label>
        </td>
    </tr>
    <tr>
        <td><b>Age:</b></td>
        <td>
            <asp:Label ID="lblAge" 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>Country:</b></td>
        <td>
            <asp:Label ID="lblCountry" runat="server"></asp:Label>
        </td>
    </tr>
</table>
</asp:Panel>
<br />
<asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" />

Namespaces

using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //Populate DataTable
        DataTable dt = new DataTable();
        dt.Columns.Add("Name");
        dt.Columns.Add("Age");
        dt.Columns.Add("City");
        dt.Columns.Add("Country");
        dt.Rows.Add();
        dt.Rows[0]["Name"] = "Adnaan Khan";
        dt.Rows[0]["Age"] = "27";
        dt.Rows[0]["City"] = "Mumbai";
        dt.Rows[0]["Country"] = "India";

        //Bind Datatable to Labels
        lblName.Text = dt.Rows[0]["Name"].ToString();
        lblAge.Text = dt.Rows[0]["Age"].ToString();
        lblCity.Text = dt.Rows[0]["City"].ToString();
        lblCountry.Text = dt.Rows[0]["Country"].ToString();
    } 
}

protected void btnExport_Click(object sender, EventArgs e)
{
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=Data.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    pnlPerson.RenderControl(hw);
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();
} 

Screenshot

PDF