Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
HTML
Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=Example.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            gridmeasuredetails.AllowPaging = false;
            gridmeasuredetails.DataBind();
            //BindGrid();
            gridmeasuredetails.RenderControl(hw);
            gridmeasuredetails.HeaderRow.Style.Add("width", "25%");
            gridmeasuredetails.HeaderRow.Style.Add("font-size", "11px");
            gridmeasuredetails.Style.Add("text-decoration", "none");
            gridmeasuredetails.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
            gridmeasuredetails.Style.Add("font-size", "9px");
            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();
            htmlparser.Parse(sr);
            pdfDoc.Close();
            Response.Write(pdfDoc);
            Response.End();

i am using this code exporting to the data into pdf format but i got The document has no pages how to solve this issue in c#
Posted
Updated 7-Mar-13 0:19am
v4
Comments
VishwaKL 6-Mar-13 4:28am    
check in your method where you binding values to grid view, as per your code there is no binding method exist, that's y it coming no data, check that by debugging,
OPees 6-Mar-13 5:27am    
please check your datasource! missing from you code!

Hi Krish,
and on .aspx page :
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="SendingMail.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">

    <asp:gridview ID="gvdetails" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:BoundField DataField="VendorId" HeaderText="VendorId"
                SortExpression="VendorId" />
            <asp:BoundField DataField="IncomeDay" HeaderText="IncomeDay"
                SortExpression="IncomeDay" />
            <asp:BoundField DataField="IncomeAmount" HeaderText="IncomeAmount"
                SortExpression="IncomeAmount" />
        </Columns>
        </asp:gridview>

        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:QSensorMonitorConnectionString %>"
            SelectCommand="SELECT * FROM [DailyIncome]"></asp:SqlDataSource>

    </form>
</body>
</html>



on.cs page :
C#
using System;
using System.Web;
using System.Web.UI;
using iTextSharp.text;//need to download .dll
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using System.IO;

namespace SendingMail
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        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);
            gvdetails.DataBind();
            gvdetails.RenderControl(hw);
            gvdetails.HeaderRow.Style.Add("width", "15%");
            gvdetails.HeaderRow.Style.Add("font-size", "10px");
            gvdetails.Style.Add("text-decoration", "none");
            gvdetails.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
            gvdetails.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();
            htmlparser.Parse(sr);
            pdfDoc.Close();
            Response.Write(pdfDoc);
            Response.End();
        }
    }
}<pre lang="c#">

Jmd:-)
 
Share this answer
 
v2
Comments
krish2013 7-Mar-13 1:07am    
Dear Giri thanks for the reply, Your code working fine.
Mine 4+

Here in your code we are using "SqlDataSource".

In my code I am using storedprocedure then binding to the Grid,In this case I am getting the error like "System.IO.IOException: The document has no pages."

How can resolve this, kindly reply me
giri001 7-Mar-13 1:45am    
ok..so you need to check that your grid have data or not so for that run make your code like this:
if (gvdetails.Rows.Count > 0)
{

//Todo:convert into pdf.
}
and Debug it carefully.
Thanks for your response.
krish2013 7-Mar-13 6:02am    
hi giri,
Thanking for u give a Suggestion. I tried that but I am getting error like "System.InvalidCastException: Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type 'iTextSharp.text.Paragraph'.
" please resolve this issue
giri001 7-Mar-13 7:19am    
http://forums.asp.net/p/1286980/2483338.aspx view this thread.and i am also looking into this.
Manpreet Singh Sandhu 23-Apr-13 4:10am    
im getting a error on this line:

htmlparser.Parse(sr);

the error is that it has some invalid arguments...

my code is:

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.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();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
Hello,

Please have a look at this article: Export Data to Excel, Word, PDF without Automation from DataBase[^]. You may give some suggestions from it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900