locked
Pdf report not displaying in browser RRS feed

  • Question

  • User1839056048 posted

    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

    Saturday, February 6, 2016 9:56 AM

Answers

  • User1814019480 posted

    hi Baiju,

    According to my experience, if we want to show the PDF in our browser, we only set the code like this :

                    Response.AddHeader("content-disposition", "attachment;filename=file.pdf");

    If we want to download it, we can use "inline":

                    Response.AddHeader("content-disposition", "inline;filename=file.pdf");

    If you want to see the PDF file on your browser, we should install the plugin for the browser

    Please change your code to test it. As below, I shared my code for you, please refer to it:

    using (MemoryStream ms = new MemoryStream())
                using (Document document = new Document(PageSize.A4, 25, 25, 30, 30))
                using (PdfWriter writer = PdfWriter.GetInstance(document, ms))
                {
                    document.Open();
                    document.Add(new Paragraph("Hello World"));
                    document.Close();
                    writer.Close();
                    ms.Close();
                    Response.ContentType = "pdf/application";
                    //for downloading
                    //Response.AddHeader("content-disposition", "attachment;filename=file.pdf");
                    //for browser
                    Response.AddHeader("content-disposition", "inline;filename=file.pdf");
                    Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
                }

    Any concerns, please feel free to let me know.

    Regards,

    Will

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 8, 2016 8:52 AM

All replies

  • User1814019480 posted

    hi Baiju,

    According to my experience, if we want to show the PDF in our browser, we only set the code like this :

                    Response.AddHeader("content-disposition", "attachment;filename=file.pdf");

    If we want to download it, we can use "inline":

                    Response.AddHeader("content-disposition", "inline;filename=file.pdf");

    If you want to see the PDF file on your browser, we should install the plugin for the browser

    Please change your code to test it. As below, I shared my code for you, please refer to it:

    using (MemoryStream ms = new MemoryStream())
                using (Document document = new Document(PageSize.A4, 25, 25, 30, 30))
                using (PdfWriter writer = PdfWriter.GetInstance(document, ms))
                {
                    document.Open();
                    document.Add(new Paragraph("Hello World"));
                    document.Close();
                    writer.Close();
                    ms.Close();
                    Response.ContentType = "pdf/application";
                    //for downloading
                    //Response.AddHeader("content-disposition", "attachment;filename=file.pdf");
                    //for browser
                    Response.AddHeader("content-disposition", "inline;filename=file.pdf");
                    Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
                }

    Any concerns, please feel free to let me know.

    Regards,

    Will

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 8, 2016 8:52 AM
  • User1839056048 posted

    your code not working

    Monday, February 8, 2016 3:59 PM
  • User1814019480 posted

    hi Baiju,

    What's error message does your code occur? If you want to test my code, you may need install the itextsharp from NuGet packages.

    Any information, please let me know.

    Regards,
    Will

    Tuesday, February 9, 2016 7:05 AM