You must Sign In to post a response.
  • Category: .NET

    How to display pdfdata from database in browser

    actually my code is working for opening pdf format in file but i want retrieved data from databse in pdf format in browser
    below is the code any one helpme or give me examples
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    using System.IO;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using iTextSharp.text.html;
    using iTextSharp.text.html.simpleparser;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;

    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    string cs = "Data Source=HOME;Initial Catalog=Registration;Integrated Security=True";
    SqlConnection con = new SqlConnection(cs);
    SqlCommand cmd = new SqlCommand("select * from employeep",con);
    DataTable dt = GetData(cmd);
    GridView1.DataSource = dt;
    GridView1.DataBind();

    }
    private DataTable GetData(SqlCommand cmd)
    {
    DataTable dt = new DataTable();
    String strConnString = "Data Source=HOME;Initial Catalog=Registration;Integrated Security=True";
    SqlConnection con = new SqlConnection(strConnString);
    SqlDataAdapter sda = new SqlDataAdapter();
    cmd.CommandType = CommandType.Text;
    cmd.Connection = con;
    try
    {
    con.Open();
    sda.SelectCommand = cmd;
    sda.Fill(dt);
    return dt;
    }
    catch (Exception ex)
    {
    throw ex;
    }
    finally
    {
    con.Close();
    sda.Dispose();
    con.Dispose();
    }
    }


    public override void VerifyRenderingInServerForm(Control control)
    {
    /* Verifies that the control is rendered */
    }
    protected void OnPaging(object sender, GridViewPageEventArgs e)
    {
    GridView1.PageIndex = e.NewPageIndex;
    GridView1.DataBind();
    }

    void btnExportPDF_Click(object sender, EventArgs e)
    {
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    GridView1.AllowPaging = false;
    GridView1.DataBind();
    GridView1.RenderControl(hw);
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 10f,10f,10f,0f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.End();
    }
  • #761448
    Hi

    You have asked the same requirement in your previous thread which is in the below link.

    www.dotnetspider.com/forum/342633-how-to-retrieve-data-from-database-in-pdf-format.aspx

    Here you posted the code. Dont create duplicate posts.

    Regards

    Thota Sridhar.
    If you learn from defeat..
    You haven't really lost..

    Sridhar Thota.
    Editor: DNS Forum.

  • #761450

    Hi
    Velin

    You can try this code working for pdf Conversion from Datatable


    if (!IsPostBack)
    {
    DataTable dt1 = new DataTable();
    DataRow dr;
    dt1.Columns.Add("lblsectn");
    dt1.Columns.Add("lblproductname");
    dt1.Columns.Add("lbllotno");
    dt1.Columns.Add("lblmrp");
    dt1.Columns.Add("lblsupliername");
    dt1.Columns.Add("lblsizee");
    dr = dt1.NewRow();
    dr[0] = 1;
    dr[1] = "Mouse";
    dr[2] = 180;
    dr[3] = 123;
    dr[4] = "HCL";
    dr[5] = 196;
    dt1.Rows.Add(dr);

    dr = dt1.NewRow();
    dr[0] = 2;
    dr[1] = "Keyboard";
    dr[2] = 280;
    dr[3] = 323;
    dr[4] = "HCL";
    dr[5] = 496;
    dt1.Rows.Add(dr);


    dr = dt1.NewRow();
    dr[0] = 3;
    dr[1] = "PenDrive";
    dr[2] = 380;
    dr[3] = 423;
    dr[4] = "HCL";
    dr[5] = 696;
    dt1.Rows.Add(dr);

    ddsales.DataSource = dt1;
    ddsales.DataBind();
    }

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


    I have attached Snapshots Given Below


    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

    Delete Attachment

  • #761452

    Hello,

    Refer the below link :

    http://www.aspsnippets.com/Articles/Retrieve-and-display-PDF-Files-from-database-in-browser-in-ASPNet.aspx

    They explained it very well.

    Hope this will help you.


    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #761466
    Hi,

    Refer below link this will helpful to you, While exporting data into PDF.

    https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=export%20data%20to%20pdf%20in%20asp.net

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


Sign In to post your comments