Convert HTML File to PDF using C# and VB.Net in ASP.Net

Last Reply 6 months ago By dharmendr

Posted 6 months ago

Hi,

I   need  to  covert a .html  file  to  .pdf  or .jpg  file  using   c# code.

Please  help

 

Posted 6 months ago Modified on 6 months ago

Hi salini,

Check this example. Now please take its reference and correct your code.

You will need to download ITextSharp and add its reference to your project. ITextSharp is a free HTML to PDF Library. You can download it using the following download link.

Download ITextSharp

Html File

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <table border="1" cellpadding="1" cellspacing="1" style="width: 300px;">
        <thead>
            <tr>
                <th>Id</th>
                <th>Name</th>
                <th>Country</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>1</td>
                <td>John Hammond</td>
                <td>United States</td>
            </tr>
            <tr>
                <td>2</td>
                <td>Mudassar Khan</td>
                <td>India</td>
            </tr>
            <tr>
                <td>3</td>
                <td>Suzanne Mathews</td>
                <td>France</td>
            </tr>
            <tr>
                <td>4</td>
                <td>Robert Schidner</td>
                <td>Russia</td>
            </tr>
        </tbody>
    </table>
</body>
</html>

HTML

<asp:Button Text="Convert" runat="server" OnClick="Download" />

Namespaces

C#

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

VB.Net

Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.html.simpleparser
Imports iTextSharp.text.pdf

Code

C#

protected void Download(object sender, EventArgs e)
{
    using (StringWriter sw = new StringWriter())
    {
        using (HtmlTextWriter hw = new HtmlTextWriter(sw))
        {
            using (StreamReader sr = new StreamReader(Server.MapPath("~/Customers.htm")))
            {
                Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
                HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                pdfDoc.Open();
                htmlparser.Parse(sr);
                pdfDoc.Close();
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=HTMLExport.pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.Write(pdfDoc);
                Response.End();
            }
        }
    }
}

VB.Net

Protected Sub Download(ByVal sender As Object, ByVal e As EventArgs)
    Using sw As StringWriter = New StringWriter()
        Using hw As HtmlTextWriter = New HtmlTextWriter(sw)
            Using sr As StreamReader = New StreamReader(Server.MapPath("~/Customers.htm"))
                Dim pdfDoc As Document = New Document(PageSize.A2, 10.0F, 10.0F, 10.0F, 0.0F)
                Dim htmlparser As HTMLWorker = New HTMLWorker(pdfDoc)
                PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
                pdfDoc.Open()
                htmlparser.Parse(sr)
                pdfDoc.Close()
                Response.ContentType = "application/pdf"
                Response.AddHeader("content-disposition", "attachment;filename=HTMLExport.pdf")
                Response.Cache.SetCacheability(HttpCacheability.NoCache)
                Response.Write(pdfDoc)
                Response.End()
            End Using
        End Using
    End Using
End Sub

Screenshot