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

    Pdf report not displaying in Browser

    Hi following is my code for creating a pdf report.

    string strQuery = "select stdid,class name1,name2,surname,fname,mname,section,room,house from stdmst1 where class=10 ";
    //SqlCommand cmd = new SqlCommand(strQuery, con);
    SqlCommand cmd = new SqlCommand(strQuery);

    DataTable dt = GetData(cmd);
    GridView GridView1 = new GridView();
    GridView1.AllowPaging = false;
    GridView1.DataSource = dt;
    GridView1.DataBind();
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=DataTable.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    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, HttpContext.Current.Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();

    the code doesn't contains any error.

    problem is it will not download to browser or directly views in browser.

    my requirement is to view in browser directly.

    it will work only if there exists a third party downloadmanager like IDM.

    How to solve this

    Regards

    Baiju
  • #764470
    Hi baiju,
    Try this:
    Open Adobe acrobat/reader.
    Click on Edit menu-->Preferences(Ctrl+K)-->Select "Internet" in left panel.
    On right panel select checkbox "Display PDF in Browser".
    OR
    Open Internet explorer-->Tools-->Manage add-ons-->Select "Toolbars and extensions" in left side panel.
    On right panel select "Adobe PDF Reader"-->right click and enable.
    Hope it helps.
    Regards,
    Shashikant Gurav
    shashikantgurav22@gmail.com

  • #764473
    Basically display PDF in browser is a PDF editor setting and we need to set it manually,
    alternatively you can do it using following ways,
    To indicate to the browser that the file should be viewed in the browser:
    Content-Type: application/pdf
    Content-Disposition: inline; filename="filename.pdf"

    To have the file downloaded rather than viewed:
    Content-Type: application/pdf
    Content-Disposition: attachment; filename="filename.pdf"

    EDIT: The quotes around the filename are required if the filename contains special characters such as filename[1].pdf which may otherwise break the browser's ability to handle the response.

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #764497
    use the inline for "content-disposition"
    Try the following code

    string strQuery = "select stdid,class name1,name2,surname,fname,mname,section,room,house from stdmst1 where class=10 ";
    //SqlCommand cmd = new SqlCommand(strQuery, con);
    SqlCommand cmd = new SqlCommand(strQuery);

    DataTable dt = GetData(cmd);
    GridView GridView1 = new GridView();
    GridView1.AllowPaging = false;
    GridView1.DataSource = dt;
    GridView1.DataBind();
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "inline;");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    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, HttpContext.Current.Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();

    By Nathan
    Direction is important than speed


Sign In to post your comments