Hi Fahimahmed,
Pleaser refer below sample.
HTML Page
<img src="https://www.aspsnippets.com/images/Blue/Logo.png" alt="Alternate Text" />
<br />
<br />
<div>
<div style="border-top: 3px solid #22BCE5">
</div>
<span class="btn btn-info">
Hello <b>{UserName}</b>
<br /><br />
A new article has been published on ASPSnippets.
<br />
<a style="color: #22BCE5" href="{Url}">{Title}</a><br />
{Description}
<br /><br />
Thanks<br />
ASPSnippets
</span>
</div>
HTML
<asp:Button Text="Export" ID="btnExport" runat="server" OnClick="Export" />
Namespaces
C#
using System.IO;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml.parser;
using iTextSharp.tool.xml;
using iTextSharp.tool.xml.pipeline.css;
using iTextSharp.tool.xml.pipeline.html;
using iTextSharp.tool.xml.html;
using iTextSharp.tool.xml.pipeline.end;
VB.Net
Imports System.IO
Imports System.Text
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.tool.xml.parser
Imports iTextSharp.tool.xml
Imports iTextSharp.tool.xml.pipeline.css
Imports iTextSharp.tool.xml.pipeline.html
Imports iTextSharp.tool.xml.html
Imports iTextSharp.tool.xml.pipeline.end
Code
C#
protected void Export(object sender, EventArgs e)
{
string body = string.Empty;
using (StreamReader reader = new StreamReader(Server.MapPath("~/EmailTemplate.html")))
{
body = reader.ReadToEnd();
}
body = body.Replace("{UserName}", "Mudassar");
body = body.Replace("{Title}", "Convert HTML File to PDF using C# and VB.Net in ASP.Net");
body = body.Replace("{Url}", "https://www.aspsnippets.com/Articles/Send-Email-using-HTML-Templates-in-ASPNet-with-C-and-VBNet.aspx");
body = body.Replace("{Description}", "Here Mudassar Ahmed Khan has explained with an example, how to send Email using HTML Templates in ASP.Net with C# and VB.Net.");
byte[] byteArray = Encoding.UTF8.GetBytes(body);
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
using (StreamReader sr = new StreamReader(new MemoryStream(byteArray)))
{
Document pdfDoc = new Document(PageSize.A2, 80f, 80f, 80f, 80f);
PdfWriter PdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
cssResolver.AddCssFile("https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css", true);
IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(pdfDoc, PdfWriter)));
var worker = new XMLWorker(pipeline, true);
var xmlParse = new XMLParser(true, worker);
pdfDoc.Open();
xmlParse.Parse(sr);
xmlParse.Flush();
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 Export(ByVal sender As Object, ByVal e As EventArgs)
Dim body As String = String.Empty
Using reader As StreamReader = New StreamReader(Server.MapPath("~/EmailTemplate.html"))
body = reader.ReadToEnd()
End Using
body = body.Replace("{UserName}", "Mudassar")
body = body.Replace("{Title}", "Convert HTML File to PDF using C# and VB.Net in ASP.Net")
body = body.Replace("{Url}", "https://www.aspsnippets.com/Articles/Send-Email-using-HTML-Templates-in-ASPNet-with-C-and-VBNet.aspx")
body = body.Replace("{Description}", "Here Mudassar Ahmed Khan has explained with an example, how to send Email using HTML Templates in ASP.Net with C# and VB.Net.")
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(body)
Using sw As StringWriter = New StringWriter()
Using hw As HtmlTextWriter = New HtmlTextWriter(sw)
Using sr As StreamReader = New StreamReader(New MemoryStream(byteArray))
Dim pdfDoc As Document = New Document(PageSize.A2, 80.0F, 80.0F, 80.0F, 80.0F)
Dim PdfWriter As PdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
Dim htmlContext As HtmlPipelineContext = New HtmlPipelineContext(Nothing)
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory())
Dim cssResolver As ICSSResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(False)
cssResolver.AddCssFile("https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css", True)
Dim pipeline As IPipeline = New CssResolverPipeline(cssResolver, New HtmlPipeline(htmlContext, New PdfWriterPipeline(pdfDoc, PdfWriter)))
Dim worker = New XMLWorker(pipeline, True)
Dim xmlParse = New XMLParser(True, worker)
pdfDoc.Open()
xmlParse.Parse(sr)
xmlParse.Flush()
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