Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i search it in Google but cannot get solution :(
please help
here is my code.

C#
protected void LinkButton1_Click(object sender, EventArgs e)
{
   Response.ContentType = "application/pdf";
   Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
   Response.Cache.SetCacheability(HttpCacheability.NoCache);

   string htmlstr = "title=’Header’ align=’Center’> Writing To PDF Using ASP.NET> <br><table align="’Center’"><tr><td style="’width:100px;color:green’"> iTextSharp</td><td style="’width:100px;color:red’">mypdftest</td></tr></table>";

   StringReader sr = new StringReader(htmlstr);
   Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0.0f);
   HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
   PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
   pdfDoc.Open();
   htmlparser.Parse(sr);   //Parse cannot take input as string reader (sr)  how to solve?
   pdfDoc.Close();
   Response.Write(pdfDoc);
   Response.End();
}
Posted
Updated 22-Apr-14 23:57pm
v4
Comments
Sergey Alexandrovich Kryukov 22-Apr-14 9:28am    
What is this htmlparser? A link, please.
And just read the documentation on it.
—SA
utm 22-Apr-14 9:38am    
plz its urgent ... provide solution.
thanks.
Sergey Alexandrovich Kryukov 22-Apr-14 12:25pm    
No, it is not urgent. If it was, you would answer my question.
—SA

it seems your html is incorrect, try with simple html first

C#
string htmlstr =  @"<html><body>
                   <h1>My First Heading</h1>
                   <p>My first paragraph.</p>
                   </body>
                   </html>";


if it is success you can fix the issue in your html.

hope this helps
 
Share this answer
 
Comments
utm 22-Apr-14 9:53am    
sorry its not working..i got errors

1Error 83 The best overloaded method match for 'iTextSharp.text.html.simpleparser.HTMLWorker.Parse(System.IO.StreamReader)' has some invalid arguments

it is error of itextsharp but i can't get it..


2.Error 84 Argument 1: cannot convert from 'System.IO.StringReader' to 'System.IO.StreamReader'
Sergey Alexandrovich Kryukov 22-Apr-14 12:26pm    
Please use StreamReader as required by this API. Read the documentation.
—SA
C#
protected void imgbtnPDF_Click(object sender, ImageClickEventArgs e)
   {

       string imagepath = Server.MapPath("Images");
       Response.ContentType = "application/pdf";
       Response.AddHeader("content-disposition", "attachment;filename=CellzoneSalesReport.pdf");
       Response.Cache.SetCacheability(HttpCacheability.NoCache);
       StringWriter sw = new StringWriter();
       HtmlTextWriter hw = new HtmlTextWriter(sw);
       GridView1.AllowPaging = false;
       GridView1.DataBind();
       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, Response.OutputStream);
       pdfDoc.Open();
       pdfDoc.Add(new Paragraph("HEADER"));
       htmlparser.Parse(sr);
       pdfDoc.Close();
       Response.Write(pdfDoc);
       Response.End();
   }


Try this. I hope this will help. :)
 
Share this answer
 
Comments
utm 22-Apr-14 10:29am    
sorry .. i get error

htmlparser.Parse(sr);//red line

same error as above...
DLSU-D Student 22-Apr-14 10:35am    
I used this code for exporting gridview.

protected void Page_Load(object sender, EventArgs e)
{
string strQuery = "SQLCOMMAND HERE";
SqlCommand cmd = new SqlCommand(strQuery);
DataTable dt = GetData(cmd);
GridView1.DataSource = dt;
GridView1.DataBind();
}
private DataTable GetData(SqlCommand cmd)
{
DataTable dt = new DataTable();
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["ProjectNameConnectionString1"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
return dt;
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
sda.Dispose();
con.Dispose();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}

protected void OnPaging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}


Try this bro. :)
DLSU-D Student 22-Apr-14 10:39am    
Make sure your itextsharp.dll is in folder Bin. :)
utm 22-Apr-14 11:00am    
yes i have...i add it as reference.
utm 22-Apr-14 10:44am    
there no problem of binding the grid view...i have use all above code..but not working..
i have problem in itextsharp...somewhere HTMLWorker htmlparser = new HTMLWorker(pdfDoc);and
htmlparser.Parse(sr); but i am not getting it ... vs intellisense ask for stream reader in place of string reader ...in htmlparser.parse(?)..

thank u dear for reply..
it is problem of itextsharp version...
when i update it to v.5.0 ..now it not showing errors.
thanks to all
 
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