Click here to Skip to main content
15,886,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want create pdf as it is web page...mns i have web page and when i click on button then page should download as pdf format..plz help me?
Posted

protected void ButtonCreatePdf_Click(object sender, EventArgs e)
{
//Set content type in response stream
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=FileName.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
//Render PlaceHolder to temporary stream
System.IO.StringWriter stringWrite = new StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
PlaceholderPdf.RenderControl(htmlWrite);
StringReader reader = new StringReader(stringWrite.ToString());
//Create PDF document
Document doc = new Document(PageSize.A4);
HTMLWorker parser = new HTMLWorker(doc);
PdfWriter.GetInstance(doc, Response.OutputStream);
doc.Open();
try
{
//Create a footer that will display page number
HeaderFooter footer = new HeaderFooter(new Phrase("This is page: "), true)
{ Border = Rectangle.NO_BORDER };
doc.Footer = footer;
//Parse Html
parser.Parse(reader);
}
catch (Exception ex)
{
//Display parser errors in PDF.
//Parser errors will also be wisible in Debug.Output window in VS
Paragraph paragraph = new Paragraph("Error! " + ex.Message);
paragraph.SetAlignment("center");
Chunk text = paragraph.Chunks[0] as Chunk;
if (text != null)
{
text.Font.Color = Color.RED;
}
doc.Add(paragraph);
}
finally
{
doc.Close();
}
}

Reference : http://hamang.net/2008/08/14/html-to-pdf-in-net/[^]
 
Share this answer
 
Comments
ArpitDubey 28-Dec-12 15:53pm    
perffact & small

nice
[no name] 29-Dec-12 0:55am    
If you're satisfied with the given answer, Accept the Solution :)
-Krunal R.
this should get you started. but i do beleive this code is to be used in conjunction with itextsharp.dll. If i remeber correctly you'll have to use a 3rd party function to create pdf's.

you can probably put the page in a div or some sort of container on render, my example uses a gridview.

VB
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Response.ContentType = "application/pdf"
    Response.AddHeader("content-disposition", "attachment;filename=example" & Year(Now) & Month(Now) & Day(Now) & Hour(Now) & Minute(Now) & Second(Now) & ".pdf")
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Dim sw As New StringWriter()
    Dim hw As New HtmlTextWriter(sw)
    Dim frm As New HtmlForm()
    GridView2.AllowPaging = False
    GridView2.Parent.Controls.Add(frm)
    frm.Attributes("runat") = "server"
    GridView2.HeaderRow.Style.Add("color", "#62A265")
    GridView2.Style.Add("font-family", "Arial, Helvetica, sans-serif;")
    GridView2.Style.Add("font-size", "8px")
    frm.Controls.Add(GridView2)
    frm.RenderControl(hw)
    Dim sr As New StringReader(sw.ToString())
    Dim pdfDoc As New Document(PageSize.LETTER, 10.0F, 10.0F, 10.0F, 0.0F)
    Dim htmlparser As New HTMLWorker(pdfDoc)
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
    pdfDoc.Open()
    htmlparser.Parse(sr)
    pdfDoc.Close()
    Response.Write(pdfDoc)
    Response.[End]()
End Sub
 
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