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 a 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 the previous step. The functionality for this button would be to export details of customers displayed on the current page. The page has the feature of paging so only customers shown for the 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 that 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 the previous step and add it for btnExportAllCustomer_Click event;

It uses the 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 binds to all customers which are in the 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 addresses used to link.

Export this repeater html to Excel

Source code on Git hub Source Code on Github

Speak your mind
Please login to post your comment!


  • geeksarray user
    12/13/2013 12:07 AM ali

    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

  • geeksarray user
    12/15/2013 07:13 AM Laxmikant

    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/

  • geeksarray user
    12/29/2013 05:24 AM CooperCloud

    Is there any way to change the font type using this method?

  • geeksarray user
    12/29/2013 07:13 AM Laxmikant

    @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

  • geeksarray user
    11/19/2014 01:34 AM chart_dev04

    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.

Blog Search





If you like my content please feel free to buy me coffee. Buy Me A Coffee