Click here to Skip to main content
Sign Up to vote bad
good
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:07


3 solutions

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:53
perffact & small nice
Krunal Rohit - 29 Dec '12 - 0:55
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.
 
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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 8,123
1 OriginalGriff 6,040
2 CPallini 3,432
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 25 Dec 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid