Skip to main content

How to export Html page to pdf using itextsharp


In this article I will explain how to export ASP.Net Web Page to PDF i.e. Portable Document Format.

So let’s start building the sample.



Step1: you need to download Itextsharp you can refer to this article how you can download Itextsharp and add its reference to your project

Step2: Create a page with image some text and an HTML table. Also there’s an export button on click of which we will export the page to PDF.
<div>
         
        <img  alt="" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjGdMyPJD_N0rauzUJj2vqH70orjQZrthEpaYoYNn00xuR300bAHEC1lxKteRlpzPwzUe2DAyC8gp_8GY9JkRlu8Qdxp6OIyUA_5OjPmiK17u4jWG-4bsjumMBSZ-304WTeIO3PIoJY-8/s1600/happy-face.png" />
       
        </div>
        <br />
&nbsp;<div style = "font-family:Arial; color:Fuchsia;">This is Demo to convert html page to pdf using ItextSharp<br />
            <br />
        </div>
     <div>
     <table border = "1" width = "100" style="background-color:green;color:white;" >
     <tr><td>Name</td><td>Age</td></tr>
     <tr><td>Tes1</td><td>14</td></tr>
     <tr><td>Tes2</td><td>13</td></tr>
     <tr><td>Tes3</td><td>12</td></tr>
     </table>
     </div>
     <div>
    <asp:Button ID="btnExport" runat="server" Text="Export"
         onclick="btnExport_Click" /></div>
        <asp:Label ID="Label1" runat="server" Text="This label and Above button will not appear in exported file"></asp:Label>

Step3:
You will need to import the following namespaces in your code.
C#
using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;

Step4:
The below event handler is executed on the click of the export button.

 public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }
    protected void btnExport_Click(object sender, EventArgs e)
    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=ConvertedPDF.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, 10f, 10f, 100f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();
    }


If you observe above code I added one function that is VerifyRenderingInServerForm this function is used to avoid the error like “control must be placed in inside of form tag”. If we set VerifyRenderingInServerForm function then compiler will think that controls rendered before exporting and our functionality will work perfectly

Note: If in your Exported pdf images are coming this is due to path issue you must check image path you have this issue






Comments

  1. i m getting error:
    the error is:input string was not in correct format

    ReplyDelete
    Replies
    1. can you please give me line which is giving error

      Delete
  2. Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type 'iTextSharp.text.Rectangle'.

    ReplyDelete
    Replies
    1. Hello Mansi

      this error this is due to different itextsharp version. if you use same version of dll that is given in code than this code will work. in latest version

      `HTMLWorker' has been deprecated in favor of XMLWorker


      Thanks

      Delete
  3. when the above code using
    im geting this type of error:
    RegisterForEventValidation can only be called during Render();
    kindly help

    ReplyDelete
    Replies
    1. either use

      public override void VerifyRenderingInServerForm(Control control)
      {
      /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
      server control at run time. */
      }

      Delete
    2. or

      @ Page Language="C#" AutoEventWireup="true" EnableEventValidation = "false"

      Delete
  4. I want to convert panel to the pdf but in that panel there is one image tag & it gives error :
    could not find the part of path of image placed inside the panel

    ReplyDelete
    Replies
    1. please check your image path it is not pointing to correct path
      or you can give me the code

      Delete
    2. Response.ContentType = "application/pdf";
      Response.AddHeader("content-disposition", "attachment;filename=Receipt.pdf");
      Response.Cache.SetCacheability(HttpCacheability.NoCache);
      StringWriter sw = new StringWriter();
      HtmlTextWriter hw = new HtmlTextWriter(sw);
      receipt.RenderControl(hw);
      PanelPayment1.RenderControl(hw);
      StringReader sr = new StringReader(sw.ToString());
      Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
      HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
      PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
      pdfDoc.Open();
      htmlparser.Parse(sr);
      pdfDoc.Close();
      Response.Write(pdfDoc);
      Response.End();

      Delete
    3. I put image tag in the receipt panel & assign the ImageUrl from the .cs file
      as the code below
      logo.ImageUrl = "~/Profile_Pic/" + dr["Logo"].ToString();

      Delete
    4. & when i tried to convert into the pdf the error occor as:
      Could not find a part of the path 'C:\Program Files\Common Files\Microsoft Shared\DevServer\Profile_Pic\index.jpg'.
      it search the image on wrong path

      Delete
    5. The easiest solution should be to change: use

      img src="http://www.yourwebsite.com/Images/flower.jpg" />

      Otherwise you can use Server.MapPath("Profile_Pic/index.jpg) to get the full URL by code. However that implies you having to know the relative path to the image (meaning access to the raw html) at compile-time, which makes your code less maintainable.

      you can refer to below link for more information

      http://www.mikesdotnetting.com/article/87/itextsharp-working-with-images

      Delete
    6. thanks for the reply
      I prefer first solution & that error solved but new error occur now:
      Font size too small: 0

      Delete
  5. but now this same code is executed in another form & generated pdf
    but in that pdf the alignment & size of image is changed even alignment of the text content also not proper

    ReplyDelete
  6. i'm getting error " Illegal characters in path"
    on the line
    htmlparser.Parse(sr);

    please provide the solution ASP
    thanks

    ReplyDelete
  7. i'm getting error " Illegal characters in path"
    on the line
    htmlparser.Parse(sr);

    please provide the solution ASP
    thanks

    ReplyDelete
    Replies
    1. am also getting the same error.did u clear ur error? how?

      Delete
  8. i am getting this error "A page can have only one server-side Form tag."
    Can you please help me

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. When I try to run the progrm I am getting an error saying
    An exception of type 'System.ArgumentException' occurred in System.Web.Extensions.dll but was not handled in user code

    Additional information: Extender control 'MaskedEditExtenderCP' is not a registered extender control. Extender controls must be registered using RegisterExtenderControl() before calling RegisterScriptDescriptors().

    ReplyDelete
  11. Manish I'm getting register through RegisterScriptControl() before calling RegisterScriptDescriptors(). I followed the above steps: public override void VerifyRenderingInServerForm(Control control)
    {
    /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
    server control at run time. */
    }
    EnableValidation=false nothing seems to be working

    ReplyDelete
  12. hi, i want the outside border in pdf generation. want to remove the td and tr borders.

    ReplyDelete
  13. multiple language not suppported..how do u do make it as multiple language supported one

    ReplyDelete
  14. Hi, i have a task to export html code to pdf file. i tried it with your code, but there is an error at the line " HTMLWorker htmlparser = new HTMLWorker(pdfDoc);" , the 'HTMLWorker' has a green line. tq :)

    ReplyDelete
  15. am getting a warning of htmlworker is obsolete

    ReplyDelete

Post a Comment