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

    How to show pdfdoc in iframe when button is clicked

    i have written code i have make the pdf doc to be download to a specific folder and that folder path i have given as source to iframe but when button is clicked pdfdoc is downloading to a specific folder but not showing in iframe
    below is the code so any body helpme where my problem is


    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Data;
    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.Net;

    public partial class dropdown_to_gridview : System.Web.UI.Page
    {

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
    GridViewBind();
    }
    public void GridViewBind()
    {
    string cs = "Data Source=HOME;Initial Catalog=Registration;Integrated Security=True";
    SqlConnection con = new SqlConnection(cs);
    SqlCommand cmd = new SqlCommand("select * from employeep where Salary ='" + DropDownList1.SelectedValue + "'", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();
    }
    protected void btnExportPDF_Click(object sender, EventArgs e)
    {

    // Response.ContentType = "application/pdf";
    // Response.AddHeader("content-disposition",
    // "attachment;filename=report.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);

    FileStream fs = new FileStream(@"D:\Programs\raj.pdf", System.IO.FileMode.Create);
    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, fs);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    fs.Close();

    iframepdf.Attributes["src"] = @"D:\Programs\raj.pdf";
    }


    public override void VerifyRenderingInServerForm(Control control)
    {
    /* Verifies that the control is rendered */
    }
  • #761570

    Use the ASHX file for reading the PDF and response it.
    You can assign the iframe url in the client side also.


    By Nathan
    Direction is important than speed

  • #761573

    Hi
    Velin

    You can try this code working for me.I have mention below

    Server Side


    protected void bt11_Click(object sender, EventArgs e)
    {
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    string Filepath = Server.MapPath("//Programs//raj.pdf");
    FileStream fs = new FileStream(Filepath, System.IO.FileMode.Create);
    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, fs);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    fs.Close();
    iframepdf.Attributes.Add("src", @"/Programs/raj.pdf");
    }


    Client side


    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
    <asp:ListItem Text="12500.00" Value="12500.00"></asp:ListItem>
    <asp:ListItem Text="11500.00" Value="11500.00"></asp:ListItem>
    <asp:ListItem Text="8500.00" Value="8500.00"></asp:ListItem>
    </asp:DropDownList>

    <asp:Button ID="bt11" runat="server" Text="PDF-CLICK" OnClick="bt11_Click" />
    <asp:GridView ID="GridView1" runat="server"></asp:GridView>
    <br />
    <iframe id="iframepdf" runat="server"></iframe>


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

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

  • #761575

    Hi

    I have attached snapshot give below check this file
    for loading pdf in iframe


    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


Sign In to post your comments