Click here to Skip to main content
16,012,061 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,


I have an .aspx page with image, table contents and long strings.

Like it contains all the details of the site. Now I need to convert this page html(as after rendering .aspx file we will get it as html page) to PDF.

Can anyone help me with this. I searched in google n found some examples but all those are only some few line like 5 - 10 lines of normal string and converting it to pdf.


But i wanted to convert the contents of the whole page in pdf.

Thanks in advance.
Posted
Comments
Gooppoiner 5-Dec-19 4:32am    
I think this is exactly what you're looking for; Convert ASPX to PDF

I don't think you should call it "rendering", but I think I understand you right: you probably mean the HTML text as it appears in the HTTP response which comes from the server.

I would recommend to use the Open-Source product iText, more exactly, its .NET port called iTextSharp:
http://en.wikipedia.org/wiki/IText[^],
http://itextpdf.com/[^],
http://sourceforge.net/projects/itextsharp/[^].

The reference to original Java site of iText is not redundant: I found that it's hard to find documentation and samples in C# (at the moment I looked for last time), so you would be better off with Java documentation and samples, which is not a big problem as it all looks very similar to C#, and the syntax differences is not a big deal.

—SA
 
Share this answer
 
Converting html to pdf using iTextSharp


C#
using System.IO;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;



protected void btn1_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,35f,0f,0f,0f);        
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
       
        pdfDoc.Open();

        //Defining tables 
        //PdfPTable termsTable = new PdfPTable(2);
        //termsTable.SetWidths(new int[2] { 2, 5 });

       //Applying Styles
        StyleSheet ST = new StyleSheet();                
        FontFactory.Register("c:\\windows\\fonts\\Corbel.ttf");

        ST.LoadTagStyle("body", "face", "Corbel");
        ST.LoadTagStyle("body", "size", "12pt");
        ST.LoadTagStyle(HtmlTags.P, "face", "Corbel");
        ST.LoadTagStyle(HtmlTags.P, "size", "10pt");
        ST.LoadTagStyle(HtmlTags.H2, "face", "Corbel");
        ST.LoadTagStyle(HtmlTags.H2, "size", "12pt");
        ST.LoadTagStyle(HtmlTags.H2, "align", "center");
        ST.LoadTagStyle(HtmlTags.TD, "width", "10%");

        htmlparser.SetStyleSheet(ST);
        
        //Writing to PDF
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();
    
}
 
Share this answer
 
v2

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