HTML to PDF using iTextSharp Library In ASP.NET
Our article about How to convert HTML to PDF using iTextSharp Library In ASP.NET. We will show you how to Export HTML DIV contents to PDF using iTextSharp or how to export ASP.Net Panel control (which is rendered as HTML DIV) to PDF (Portable Document Format) Document or entire webpage to PDF using iTextSharp Free PDF Library. We will also give introduction to iTextSharp library.
In previous articles we explained Export HTML to Excel, Expand Collapse Nested Gridview, Search Location in Google Map API, WPF Multithreading Backgroundworker, Auto Refresh Page Every 5 Seconds, Color Picker using JavaScript, Hindi TextBox using Google Transliterate API, Image Gallery, Responsive Slider, and many more. Now we will move on HTML to PDF using iTextSharp Library In ASP.NET.
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.
Following are the steps to convert HTML to PDF using iTextSharp Library In ASP.NET
CREATE A NEW WEB APPLICATION
If you are sound with the ASP.NET then you know very well how to create a web application.
LEARN MORE : LEARN MORE ABOUT HOW TO CREATE AN ASP.NET WEB APPLICATION
DOWNLOAD “itextsharp.dll” & “GeneratePDF.cs”
You can download “itextsharp.dll” & “GeneratePDF.cs” file from given link :
DOWNLOAD “itextsharp.dll” & “GeneratePDF.cs”
Create folder and add .cs file

Create folder and add cs file
Add itextsharp dll into project

Add Reference

Select dll and add into project
Add .aspx code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ConvertHTMLtoPDF.aspx.cs" Inherits="HTMLtoPDF.ConvertHTMLtoPDF" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div id="dvHtml" runat="server" style="margin: 0 0 0 0;"> <div> <b>CODE SCRATCHER</b><br /> <br /> Dear candidate,<br /> <br /> <b>After get material don't forget to Like on FB :</b> <a href="https://www.facebook.com/thecodescratcher" target="_blank" style="color: #292929; text-decoration: none;">https://www.facebook.com/thecodescratcher</a><br /> <b>Visit this website:</b> <a href="http://www.codescratcher.com" target="_blank" style="color: #292929; text-decoration: none;">http://www.codescratcher.com</a><br /> <br /> You can get the different solutions with demo example source code from <a href="http://www.codescratcher.com" target="_blank" style="color: #292929; text-decoration: none;"><b>CODE SCRATCHER</b></a>. We will always with you, so share your problem with us and we will give you best solutions as soon as possible.<br /> <br /> Complete project download section comming soon.<br /> <br /> <b>Material with the download links are listed below.</b><br /> <b>ASP.NET :</b><a href="http://www.codescratcher.com/asp-net-interview-questions" target="_blank" style="color: #292929; text-decoration: none;">http://www.codescratcher.com/asp-net-interview-questions</a><br /> <b>C++ Language :</b> <a href="http://www.codescratcher.com/c-language-interview-questions-2" target="_blank" style="color: #292929; text-decoration: none;">http://www.codescratcher.com/c-language-interview-questions-2</a> <br /> <b>C Language :</b> <a href="http://www.codescratcher.com/c-language-interview-questions" target="_blank" style="color: #292929; text-decoration: none;">http://www.codescratcher.com/c-language-interview-questions</a> <br /> <b>SQL Server :</b><a href="http://www.codescratcher.com/sql-server-interview-questions" target="_blank" style="color: #292929; text-decoration: none;">http://www.codescratcher.com/sql-server-interview-questions</a><br /> <b>HR Interview :</b> <a href="http://www.codescratcher.com/hr-interview-questions" target="_blank" style="color: #292929; text-decoration: none;">http://www.codescratcher.com/hr-interview-questions</a> <br /> <br /> <b>Like us on facebook :</b> <a href="https://www.facebook.com/thecodescratcher" target="_blank" style="color: #292929; text-decoration: none;">https://www.facebook.com/thecodescratcher</a><br /> <b>Follow on Twitter :</b> <a href="https://twitter.com/codescratcher" target="_blank" style="color: #292929; text-decoration: none;">https://twitter.com/codescratcher</a><br /> <b>Follow on Google+ :</b> <a href="https://plus.google.com/+Codescratcher" target="_blank" style="color: #292929; text-decoration: none;">https://plus.google.com/+Codescratcher</a><br /> <br /> <b>Thanks,<br /> Code Scratcher Team</b> </div> </div> <br /> <br /> <center> <asp:Button ID="btnClick" runat="server" OnClick="btnClick_Click" style="Height:30px" Text="HTML Download as PDF" /> </center> </form> </body> </html> |
Add code-behind code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; using System.Data; using iTextSharp.text; using iTextSharp.text.html.simpleparser; using iTextSharp.text.pdf; namespace HTMLtoPDF { public partial class ConvertHTMLtoPDF : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnClick_Click(object sender, EventArgs e) { DownloadAsPDF(); } public void DownloadAsPDF() { try { string strHtml = string.Empty; string pdfFileName = Request.PhysicalApplicationPath + "\\files\\" + "GenerateHTMLTOPDF.pdf"; StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); dvHtml.RenderControl(hw); StringReader sr = new StringReader(sw.ToString()); strHtml = sr.ReadToEnd(); sr.Close(); CreatePDFFromHTMLFile(strHtml, pdfFileName); Response.ContentType = "application/x-download"; Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", "GenerateHTMLTOPDF.pdf")); Response.WriteFile(pdfFileName); Response.Flush(); Response.End(); } catch (Exception ex) { Response.Write(ex.Message); } } public void CreatePDFFromHTMLFile(string HtmlStream, string FileName) { try { object TargetFile = FileName; string ModifiedFileName = string.Empty; string FinalFileName = string.Empty; GeneratePDF.HtmlToPdfBuilder builder = new GeneratePDF.HtmlToPdfBuilder(iTextSharp.text.PageSize.A4); GeneratePDF.HtmlPdfPage first = builder.AddPage(); first.AppendHtml(HtmlStream); byte[] file = builder.RenderPdf(); File.WriteAllBytes(TargetFile.ToString(), file); iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(TargetFile.ToString()); ModifiedFileName = TargetFile.ToString(); ModifiedFileName = ModifiedFileName.Insert(ModifiedFileName.Length - 4, "1"); iTextSharp.text.pdf.PdfEncryptor.Encrypt(reader, new FileStream(ModifiedFileName, FileMode.Append), iTextSharp.text.pdf.PdfWriter.STRENGTH128BITS, "", "", iTextSharp.text.pdf.PdfWriter.AllowPrinting); reader.Close(); if (File.Exists(TargetFile.ToString())) File.Delete(TargetFile.ToString()); FinalFileName = ModifiedFileName.Remove(ModifiedFileName.Length - 5, 1); File.Copy(ModifiedFileName, FinalFileName); if (File.Exists(ModifiedFileName)) File.Delete(ModifiedFileName); } catch (Exception ex) { throw ex; } } } } |
RUN PROJECT AND CHECK FINAL OUTPUT

HTML to PDF using iTextSharp Library In ASP – Output
Source Code
help@codescratcher.com
Incoming search terms
HTML to PDF using iTextSharp Library In ASP.NET, Export HTML DIV contents to PDF using iTextSharp in ASP.NET, Converting HTML to PDF using iTextSharp, HTML to PDF using iTextSharp Library In Dot Net, Creating PDF Documents with ASP.NET and iTextSharp, ITextSharp HTML to PDF, Export Div Content to PDF Using ITextSharp, Convert HTML to PDF using iTextSharp, HTML to PDF conversion using iTextsharp, iTextSharp HTML record to PDF.
Hi there, just became alert to your blog through Google, and found that it is truly informative.
I’m going to watch out for brussels. I’ll appreciate if you continue this in future.
Lots of people will be benefited from your writing.
Cheers!
Usually I don’t read post on blogs, however I wish to say that this write-up very pressured me to
check out and do so! Your writing taste has been surprised me.
Thank you, quite great post.
hello
when I add Unicode characters like “فق” pdf file don’t show this characters.
maybe cant render this characters.
We will post new article related to your query in next few days.
So keep in touch…
Share articles with your friends…
Thanks!
Thank you for this article. This helped me.
Hi there, your code works perfect but some character does not appear on pdf. For example turkish character(ş,iö,ğ,ü). Can you help me?
We will post new article related to your query in next few days.
So keep in touch…
Share articles with your friends…
Thanks!
how to add hindi unicode in pdf for unicode font print anf if we use table how to repeat table header
Keep in touch…
We will post new article related to your query in next few days.
Share articles with your friends…
Thanks!
hii
i am getting error for following
GeneratePDF.HtmlToPdfBuilder builder = new GeneratePDF.HtmlToPdfBuilder(iTextSharp.text.PageSize.A4);
GeneratePDF.HtmlPdfPage first = builder.AddPage();
Share your error message with us.
Thanks!
even iam getting the same error
GeneratePDF.HtmlToPdfBuilder builder = new GeneratePDF.HtmlToPdfBuilder(iTextSharp.text.PageSize.A4);
GeneratePDF.HtmlPdfPage first = builder.AddPage()
what is there in GeneratePDF class
hiii
got error while open pdf file
“either not supported file or it will be a attached file”
Please check your pdf file. Is it in correct format?
Also please compare your code with our working demo.
If anything then let us know.
Thanks!
Hi, I am getting an error stating
“Error 4 The name ‘Markup’ does not exist in the current context C:\Proj\WebApplication1\WebApplication1\GeneratePDF.cs 198 41 WebApplication1″
Please help.
Hello Deekshit,
Can you please download the demo of this article. and try to compare it with your code.
Hope it will help to you.
Thanks!
I am getting the same error in generatepdf method, i have added the two dll eventhough I’m getting pls help me
Hello Manikandan,
Can you please download the demo of this article. and try to compare it with your code.
Hope it will help to you.
Also you can check below link to Generate PDF.
http://www.codescratcher.com/asp-net/display-unicode-characters-in-converting-html-to-pdf
Thanks!
Great article.
Not working for unicoded text like हिंदी ?? How to overcome this issue ?
Hello Sunil,
Please check below link to Generate PDF.
http://www.codescratcher.com/asp-net/display-unicode-characters-in-converting-html-to-pdf
Hope you will get the solution.
Let us know if you have any query.
Thanks!
Your Code is working perfectly, But is not supporting CSS styles and images for me ..can u help me out
Thanks
You need to add style inside the tag and give the full path of image to display in PDF.
Hello author..!!
I’d like to know what is dvHtml,due to this class error occur “dvHtml keyword doesn’t exist in project ”
Please Help me out ASAP..
Thanks in advanced.
Hi Tarun,
You can see dvHtml is available in .aspx page. It’s available with runat tag.
Thanks!
Guys I’m getting Script controls must be registered using RegisterScriptControl() before calling RegisterScriptDescriptors() while importing my HTML code , the exception seems to be triggered by this function. RenderControl(hw); Do you have a clue about how to sort this out. There are a lot of info within the Web but nothing works.
Thanks in advance.
is possible to add header that should repeat in all the pages of pdf?
Yes, you can use PdfPTable and set the table.HeaderRows = 3;
How to Convert/Export an ASPX page with texboxes and other controls(Checkbox,Radio buttons etc) to PDF using itextsharp
Can any one help?
Aloha,
You make learning and reading addictive. All eyes fixed on you. Thank you being such a good and trust worthy guide.
I have this Splitter. Which have first pane 25% width. In first pane is grid which i want 100% width and height. But on page load the first pane have width 100% and in this time the grid is rendered. Then the pane shrink to the 25% but the grid do not refresh his size. After i do some resize with page the grid refresh well. Also please how to add 100% height for the grid? Pictures attached ASP .Net
But great job man, do keep posted with the new updates.
Best Regards,
Ajeeth
Thanx for sharing it….You can also used zetpdf to convert Html to PDF. I am using it and experience of using it is awesome…
converting html to pdf using another third part library in c# https://www.iditect.com/tutorial/html-to-pdf/