Convert HTML to pdf in asp.net c# using itextSharp

Convert HTML to pdf in asp.net c# using itextSharp, someone asked me to explain?

In this article, I will show you how to convert  html to pdf in c# using iTextSharp. For that, you need to Download the iTextSharp PDF library and unzip. Copy and paste the following dlls itextsharp and itextsharp.xmlworker in the project folder and reference it .

Step 1:Create a asp.net project and Create a new aspx page and name it as Default.aspx. Copy and paste the HTML markup code in the design page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" ValidateRequest="false" Inherits="MymvcApp.PrintData" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div id="Content">
            <table cellspacing="0" cellpadding="2" style="border-collapse: collapse; border: 1px solid #2595de; font-size: 11pt;">
                <tr>
                   <th style="background-color: #B8DBFD; border: 1px solid #2595de">Employee ID</th>
                    <th style="background-color: #B8DBFD; border: 1px solid #2595de">Name</th>
                    <th style="background-color: #B8DBFD; border: 1px solid #2595de">City</th>
                </tr>
                <tr>
                    <td style="width: 120px; border: 1px solid #2595de">111</td>
                    <td style="width: 150px; border: 1px solid #2595de">Mohamed Mohideen</td>
                    <td style="width: 120px; border: 1px solid #2595de">Chennai</td>
                </tr>
                <tr>
                    <td style="width: 120px; border: 1px solid #2595de">112</td>
                    <td style="width: 150px; border: 1px solid #2595de">Manzoor</td>
                    <td style="width: 120px; border: 1px solid #2595de">Mumbai</td>
                </tr>
                <tr>
                    <td style="width: 120px; border: 1px solid #2595de">113</td>
                    <td style="width: 150px; border: 1px solid #2595de">Thivan Mydeen</td>
                    <td style="width: 120px; border: 1px solid #2595de">Banglore</td>
                </tr>
                <tr>
                    <td style="width: 120px; border: 1px solid #2595de">114</td>
                    <td style="width: 150px; border: 1px solid #2595de">Mohamed Rasik</td>
                    <td style="width: 120px; border: 1px solid #2595de">Tirunelveli</td>
                </tr>
            </table>
        </div>
        <br />
        <asp:HiddenField ID="hfdContent" runat="server" />
        <asp:Button ID="btnExport" runat="server" Text="Export To PDF" OnClick="ExportToPDF" />
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $("#btnExport").click(function () {
                    $("#hfdContent").val($("#Content").html());
                });
            });
        </script>
    </form>
</body>
</html>

Step 2:  Press F7 ,Copy and paste the following code behind Default.aspx.cs.

   protected void ExportToPDF(object sender, EventArgs e)
        {
            Guid newID = Guid.NewGuid();
            StringReader sr = new StringReader(Request.Form[hfdContent.UniqueID]);
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
            PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
            pdfDoc.Close();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=" + newID + "HTML.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Write(pdfDoc);
            Response.End();
       } 

Description: When the button is clicked, The Html string value is extracted from the Hidden field. A new document will be created with formatted and convert Html to PDF in c# using itextsharp. The file name for the pdf file is generated as guid and the PDF is downloaded in the browser. 

c# html to pdf itextsharp

itextsharp create pdf from html

 

 

Post your comments / questions