Converting ASP.net Repeater HTML to PDF using iTextSharp

This article describes you how to convert asp.net repeater controls html or entire webpage to pdf using iTextSharp. It will also give introduction to iTextSharp library.


Introduction to iTextSharp

iTextSharp is a free library to create PDF documents using C#.net. It gives you more flexibility to documents in terms of look and feel and overall customization of PDF documents. iTextSharp's objects like Table, Cell, paragraph, phrase etc. makes things easy to create professional pdf documents. It allows you to control every pixel and line of PDF file. Using iTextSharp you can only create PDF files.
Download the latest iTextSharp dll by clicking here.


Create Webpage with repeater control

Follow the article Repeater control with custom paging to create aspx page with repeater control.


Export Customers

Add a button control to the aspx page created in previous step. The functionality for this button would be to export details of customers displayed on current page. The page has the feature of paging so only customers shown for current page will be export to PDF using iTextSharp dll.

iTextSharp.text.html.simpleparser.HTMLWorker has method Parse which parses string to HTML.

Repeater control's method RenderControl gives us the required HTML for parsing and with the help of Response objects methods and properties you can convert the HTML to PDF.

Add the below code for btnExport click event.

    
protected void btnPDF_Click(object sender, EventArgs e)
{
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", 
        "attachment;filename=Customers.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
            
    //this.Page.RenderControl(hw);            
    this.repCustomers.RenderControl(hw);
    StringReader sr = new StringReader
        (sw.ToString().Replace("\r", "")
        .Replace("\n", "").Replace("  ", ""));
    
    Document pdfDoc = 
        new Document(iTextSharp.text.PageSize.A4,
                     10f, 10f, 10f, 0.0f);

    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();
}       
            

convert Customer HTML page to PDF


Export All Customers

Add a button to the aspx page for converting all customers HTML to PDF.

Due to paging, at any given time you will have only customers which are displayed for current page index. So you will have to create a new Repeater control and bind it to all customers using C# code.

Replace below code with this.repCustomers.RenderControl(hw); from previous step and add it for btnExportAllCustomer_Click event;

It uses RenderControl method of new repeater control.


Repeater repAllCustomers = this.repCustomers;

repAllCustomers.DataSource = 
    CustomerService.GetAllCustomers
        (0, CustomerService.GetCustomerCount());
    
repAllCustomers.DataBind();
repAllCustomers.RenderControl(hw); 
    
            

It converts the structure of existing repeater repCustomers control and bind to all customers which are in Northwind database.

Convert all Customers HTML to PDF


Convert entire Webpage HTML to PDF

If you want to convert the Webpage's entire HTML to PDF using iTextSharp. Use this.Page.RenderControl(hw); instead of using repAllCustomers.RenderControl.

Whenever you convert HTML to PDF make sure all the links to images, other webpages are accessible or relative address used to link.

Export this html to Excel or show it in Crystal Report

Download source code and required stored proc.

Speak your mind :
Leave a comment for this article on dotnetbloogers.com
User profile picture on dotnetmentors.com

by ali at 12/13/2013 12:07:00 AM
i've a problem whn i convert html to pdf with itexSharp in C# i,ve not the real image of html for example the table isn't matching give me an ideam inf2050@yahoo.fr thanks
post comment on dotnetmentors.com
User profile picture on dotnetmentors.com

by Laxmikant at 12/15/2013 7:13:00 AM
As we are using ASP.NET Response object to convert html to PDF so it depends on various factor which Response object will use while converting html to PDF. If you want to have absolute control o PDF formatting and data you will have to use iTextSharps object. Go through for more info ... http://www.c-sharpcorner.com/UploadFile/f2e803/basic-pdf-creation-using-itextsharp-part-i/
post comment on dotnetmentors.com
User profile picture on dotnetmentors.com

by CooperCloud at 12/29/2013 5:24:00 AM
Is there any way to change the font type using this method?
post comment on dotnetmentors.com
User profile picture on dotnetmentors.com

by Laxmikant at 12/29/2013 7:13:00 AM
@CooperCloud, you have to set it your font to page and its control before you call RenderControl() method of GridView. If you want to have more control on formatting, display, font, color use iTextSharp libary and its table objects
post comment on dotnetmentors.com
User profile picture on dotnetmentors.com

by chart_dev04 at 11/19/2014 1:34:00 AM
Indeed iTextSharp works fine with simple html. I had to convert a more complicated page lately containing some SVG charts and for that needed to use a more complicated html to pdf converter. ExpertPdf (http://www.html-to-pdf.net/) proved to work just fine and I recommend it as an alternative.
post comment on dotnetmentors.com
User profile picture on dotnetmentors.com

by chart_dev04 at 11/19/2014 1:35:00 AM
Indeed iTextSharp works fine with simple html. I had to convert a more complicated page lately containing some SVG charts and for that needed to use a more complicated html to pdf converter. ExpertPdf (http://www.html-to-pdf.net/) proved to work just fine and I recommend it as an alternative.
post comment on dotnetmentors.com