locked
print pdf RRS feed

  • Question

  • User736276958 posted

    hi all

    iam displaying 3 table names in dropdownlist, when user selects the item in the ddl then that table details have to be displayed in gridview and also user clicks a button(print pdf) then that gridview has to be printed in pdf foormat.

    for me every thing is working but print is not working , it is displaying error as 

    An exception of type 'System.IO.IOException' occurred in itextsharp.dll but was not handled in user code

    Additional information: The document has no pages.

    my code is

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Npgsql;
    using System.Data;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using iTextSharp.text.html.simpleparser;
    using System.IO;

    public partial class _Default : System.Web.UI.Page
    {
    private DataSet ds = new DataSet();
    private DataTable dt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {





    }
    public override void VerifyRenderingInServerForm(Control control)
    {
    /* Verifies that the control is rendered */
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    GridView1.AllowPaging = false;
    GridView1.DataBind();
    GridView1.RenderControl(hw);
    GridView1.HeaderRow.Style.Add("width", "15%");
    GridView1.HeaderRow.Style.Add("font-size", "10px");
    GridView1.Style.Add("text-decoration", "none");
    GridView1.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
    GridView1.Style.Add("font-size", "8px");
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();  ------------------------------------------------------------here iam getting error
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    string connstring = String.Format("Server=localhost;Port=5432;User Id=xxx;Password=xxxx;Database=mpdb;");

    NpgsqlConnection conn = new NpgsqlConnection(connstring);
    conn.Open();

    if (DropDownList1.SelectedValue=="1")
    {
    string sql = "SELECT * FROM dev_ind_land";

    NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, conn);

    ds.Reset();

    da.Fill(ds);

    dt = ds.Tables[0];

    GridView1.DataSource = dt;

    GridView1.DataBind();


    }

    if (DropDownList1.SelectedValue =="2")
    {
    string sql = "SELECT * FROM exi_ind_land";

    NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, conn);

    ds.Reset();

    da.Fill(ds);

    dt = ds.Tables[0];

    GridView1.DataSource = dt;

    GridView1.DataBind();
    }

    if (DropDownList1.SelectedValue == "3")
    {
    string sql = "SELECT * FROM ide_ind_land";

    NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, conn);

    ds.Reset();

    da.Fill(ds);

    dt = ds.Tables[0];

    GridView1.DataSource = dt;

    GridView1.DataBind();
    }
    conn.Close();
    }
    }

    plz tell me where iam doing mistake

    Friday, February 13, 2015 12:18 AM

All replies

  • User-760709272 posted

    Your code is running on the server so it is trying to print on the server.  You can't print from your server code, and to pre-empt your next question you can't "force" the client to print your PDF either.

    Friday, February 13, 2015 3:44 AM
  • User2052419148 posted

    You can always try a 3rd party app that does the printing, sorting, and filtering for you.  http://csdrsolutions.com/Pages/Products/.

    Just bind your data and let the custom gridview do the rest.

    Friday, March 13, 2015 12:25 AM