Click here to Skip to main content
12,289,466 members (33,113 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET
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 23-Dec-12 18:07pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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/[^]
  Permalink  
Comments
ArpitDubey 28-Dec-12 15:53pm
   
perffact & small

nice
Krunal Rohit 29-Dec-12 0:55am
   
If you're satisfied with the given answer, Accept the Solution :)
-Krunal R.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.

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
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160518.1 | Last Updated 25 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100