0

Question:

I want a button on my page on clicking which the whole content of the page is saved in the form of pdf.

Queries:

So any one has any idea that, is there any library or assembly or any piece of code that can provide me with this feature?

Thanks in advance

Anirudh Agarwal
  • 655
  • 2
  • 7
  • 29
  • possible duplicate of [Convert HTML to PDF in .NET](http://stackoverflow.com/questions/564650/convert-html-to-pdf-in-net) – Krzusztof Nov 13 '13 at 07:34

1 Answers1

1

body part

 <asp:PlaceHolder ID="PlaceholderPdf" runat="server">   
 <asp:Label ID="Label1" runat="server" Text="hi hw r u ...i m fine wht about u"></asp:Label>
</asp:PlaceHolder>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click1" 
        Text="converting to pdf" />

download itextsharp.dll

some necessary name space are

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.IO;
using System.Text;

.cs part

protected void Button2_Click1(object sender, EventArgs e)
{
//SIMPLE TEXT IS CONVERTING
    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
        //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);
        Chunk text = paragraph.Chunks[0] as Chunk;
        if (text != null)
        {
        }
        doc.Add(paragraph);
    }
    finally
    {
        doc.Close();
    }
}
 public override void VerifyRenderingInServerForm(Control control)
{
}

try this and let me know

  • Thanks is working great but any image included on the page is not rendered. Any suggestions for that. – Anirudh Agarwal Nov 13 '13 at 08:42
  • this following links may help you ...http://stackoverflow.com/questions/1642280/jpg-to-pdf-convertor-in-c-sharp or http://forums.asp.net/t/1876445.aspx –  Nov 13 '13 at 09:10