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?

8 February 2017

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>
div id="Content">
table cellspacing="0" cellpadding="2" style font-size: 9.5pt; font-family: Consolas; color: red; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">border-collapse: collapse; border: 1px solid #2595de; font-size: 11pt;">
tr>
th style font-size: 9.5pt; font-family: Consolas; color: red; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">background-color: #B8DBFD; border: 1px solid #2595de">Employee IDth>
th style font-size: 9.5pt; font-family: Consolas; color: red; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">background-color: #B8DBFD; border: 1px solid #2595de">Nameth>
th style font-size: 9.5pt; font-family: Consolas; color: red; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">background-color: #B8DBFD; border: 1px solid #2595de">Cityth>
tr>
tr>
td style font-size: 9.5pt; font-family: Consolas; color: red; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">width: 120px; border: 1px solid #2595de">111td>
td style font-size: 9.5pt; font-family: Consolas; color: red; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">width: 150px; border: 1px solid #2595de">Mohamed Mohideentd>
td style font-size: 9.5pt; font-family: Consolas; color: red; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">width: 120px; border: 1px solid #2595de">Chennaitd>
tr>
tr>
td style font-size: 9.5pt; font-family: Consolas; color: red; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">width: 120px; border: 1px solid #2595de">112td>
td style font-size: 9.5pt; font-family: Consolas; color: red; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">width: 150px; border: 1px solid #2595de">Manzoortd>
td style font-size: 9.5pt; font-family: Consolas; color: red; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">width: 120px; border: 1px solid #2595de">Mumbaitd>
tr>
tr>
td style font-size: 9.5pt; font-family: Consolas; color: red; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">width: 120px; border: 1px solid #2595de">113td>
td style font-size: 9.5pt; font-family: Consolas; color: red; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">width: 150px; border: 1px solid #2595de">Thivan Mydeentd>
td style font-size: 9.5pt; font-family: Consolas; color: red; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">width: 120px; border: 1px solid #2595de">Bangloretd>
tr>
tr>
td style font-size: 9.5pt; font-family: Consolas; color: red; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">width: 120px; border: 1px solid #2595de">114td>
td style font-size: 9.5pt; font-family: Consolas; color: red; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">width: 150px; border: 1px solid #2595de">Mohamed Rasiktd>
td style font-size: 9.5pt; font-family: Consolas; color: red; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">width: 120px; border: 1px solid #2595de">Tirunelvelitd>
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>
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 font-size: 9.5pt; font-family: Consolas; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;"> + 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

Related Article