Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
Hi everyone,
I m working in asp.net from last 4 months. now i m arrive at the place where there is a need to convert a page from aspx to pdf. I tried using itextsharp but it fires error.

Actually my page contents some value from database that are bind in lables, one dynamic table generated according to the database values (sometimes 2 rows or 5 or more - all the data in table are in string format ) and some static part. Now i want to convert the whole page into pdf. Please help me to do this.

i tried so many codes suggested by the professionals but fail to do work. currently i used following code that fires the error - Input string was not in a correct format.
when converting a stirng to datetime, parse the string to take the date before putting each variable into the Datetime object. i m using below code to convert:
C#
Response.ContentType = "application/pdf";

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

        Response.Cache.SetCacheability(HttpCacheability.NoCache);

iTextSharp.text.Document docc = new iTextSharp.text.Document(PageSize.A4);

        //Render PlaceHolder to temporary stream

        System.IO.StringWriter stringWrite = new StringWriter();

        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

        //PlaceholderPdf.RenderControl(htmlWrite);
        PrintMemo.RenderControl(htmlWrite);

        string strrrr = stringWrite.ToString();
        strrrr = System.Text.RegularExpressions.Regex.Replace(strrrr, "", "");

        StringReader reader = new StringReader(stringWrite.ToString());

HTMLWorker parser = new HTMLWorker(docc);

PdfWriter.GetInstance(docc, Response.OutputStream);

        docc.Open();
        parser.Parse(reader);
        docc.Close();

Please help me.
Posted
v2

Found similar question here....

How to export aspx page(HTML content) in pdf[^]

abcpdf [^]is a good third party tool for doing this.

Hope this helps...

Sebastian
 
Share this answer
 
Comments
kailash_tandel87 18-Jul-12 4:28am    
thanx for you valuable effort but its not working in my case, i m still trying with itextsharp. if you have any idea related to itextsharp or simillar then help me plz
kailash_tandel87 18-Jul-12 5:06am    
<td id="PrintMemo">
<table border="0" cellpadding="0" cellspacing="0" width="100%" >
<tr>
<td align="center">
<table border="0" cellpadding="2px" cellspacing="1px" width="1024px">
<tr>
<td align="center">
<table border="0" cellpadding="2px" cellspacing="5px" width="90%">

<tr>
<td align="center">
DEPARTMENT OF POSTS, INDIA
</td>
</tr>
<tr>
<td align="center">
O/o the  <span id="lbl_deptname_a1">SSPOs</span>
  <span id="lbl_division_a2">Gandhinagar</span>
,
</td>
</tr>
<tr>
<td align="center">
<span id="lbl_city_a3" class="style2">Gandhinagar</span>
<span class="style2"> - <span id="lbl_pincode_a4">382024</span>
</span>
</td>
</tr>
</table>
</td>
</tr>

<tr>
<td align="center">
<table border="0" cellpadding="2px" cellspacing="2px" width="80%">
<tr>
<td align="left">
No. :  <span id="lbl_memoNo">Trg/B2/VDRW</span>
</td>
<td align="right">
Dated  
<span id="lbl_date_a5">10/07/2012</span>
</td>
</tr>
<tr>
<td align="left" colspan="2">
Ref :  <span id="lbl_reference">PTC Vadpdara letter no: A2/Trg dated 25.06.2012</span>
</td>
</tr>
</table>
</td>
</tr>

<tr>
<td align="center">
<table border="0" cellpadding="2px" cellspacing="2px" width="80%">
<tr>
C#
protected void btnExportFromAspxtoPdf_Click(object sender, EventArgs e)
{
    Response.Write("<script language='javascript'> { self.close() }</script>");
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    this.Page.RenderControl(hw);

    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();
}
 
Share this answer
 
v2
Comments
shiva2239 9-Apr-15 8:11am    
Thanks....Its working but having a huge alignment problem comparing with aspx web page and not getting borders between data in PDF like my webpage.Plz suggest me.
Use the following code in the Aspx to Pdf conversion button click..

C#
protected void btnExport_Click(object sender, EventArgs e)
{
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    this.Page.RenderControl(hw);

    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();
}
 
Share this answer
 
v2
Comments
nagrajpoddar 18-Dec-13 4:39am    
Nice
Dinesh Padupalli 6-Sep-17 0:34am    
Nice it is working for me.

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