Hi fahimahmed,
Please refer below Sample.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
You can download the database table SQL by clicking the download link below.
Download SQL file
HTML Page
<div class="container-fluid">
<div class="row" style="padding:40px 40px 5px 50px;">
<div class="row">
<div class="col-sm-12" style="padding:5px; ">
<div class="card bg-light">
<div class="card-body" style="text-align:center;height:100px; margin-top:-10px;">
<img src="https://w7.pngwing.com/pngs/786/126/png-transparent-logo-contracting-photography-logo-symbol.png" height="50" width="100" />
<p style="font-size:8px; margin-top:5px;">Warehouse # 3, Industrial Area 15, Sharjah - UAE</p>
<p style="font-size:8px; margin-top:-15px; "><i class="bi bi-telephone-inbound"></i> +971 50 5221797 | <i class="bi bi-envelope"></i> info@shipments.care | <i class="bi bi-globe"></i> https://shipments.care</p>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-4" style="padding:5px;">
<div class="card bg-light" style="text-align:center;">
<div class="card-header" style="padding:1px; font-size:10px; text-align:center;">WayBill #</div>
<div class="card-body">
<p class="card-text" style="padding:5px; margin-left:-15px; font-family: IDAutomationHC39M; font-size:medium;">1</p>
</div>
</div>
</div>
<div class="col-sm-4" style="padding:5px;">
<div class="card bg-light" style="text-align:center;">
<div class="card-header" style="padding:1px; font-size:10px; text-align:center;">Reference #</div>
<div class="card-body">
<p class="card-text" style="padding:5px; margin-left:-15px; font-family: IDAutomationHC39M; font-size:small;">1</p>
</div>
</div>
</div>
<div class="col-sm-4" style="padding:5px;">
<div class="card bg-light" style="text-align:center;">
<div class="card-header" style="padding:1px; font-size:10px; text-align:center;">Courier: DTDC</div>
<div class="card-body">
<p class="card-text" style="padding: 5px; margin-left: -15px; font-family: IDAutomationHC39M; font-size: small;">1</p>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6" style="padding:5px;">
<div class="card bg-light">
<div class="card-header" style="padding:1px; font-size:10px; text-align:center;">SENDER</div>
<div class="card-body" style="padding:5px 10px 5px 10px">
<div class="card-text" style="font-size:10px;">
<table>
<tr>
<td class="tableheading">Customer Id </td>
<td>:</td>
<td class="tablerows">{CustomerId}</td>
</tr>
<tr>
<td class="tableheading">Name</td>
<td>:</td>
<td class="tablerows">{Name}</td>
</tr>
<tr>
<td class="tableheading">Country</td>
<td>:</td>
<td class="tablerows OverflowText">{Country}</td>
</tr>
</table>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<footer class="blockquote-footer" style="font-size:10px; padding-left:10px;">Generated on {Date_Time}</footer>
</div>
</div>
</div>
</div>
HTML
<asp:GridView runat="server" ID="gvCustomers" AutoGenerateColumns="false" CssClass="table table-responsive" HeaderStyle-BackColor="#3AC0F2"
HeaderStyle-ForeColor="White" RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White"
AlternatingRowStyle-ForeColor="#000" AllowPaging="true" PageSize="1">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="ID" ItemStyle-Width="150px" />
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150px" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150px" />
</Columns>
</asp:GridView>
<br />
<asp:Button Text="Export" ID="btnExport" runat="server" OnClick="Export" />
Namespaces
C#
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.IO;
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.Data.SqlClient
Imports System.Data
Imports System.Configuration
Imports System.IO
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 Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
protected void Export(object sender, EventArgs e)
{
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
cssResolver.AddCssFile(Server.MapPath("~/Barcode.css"), true);
IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(pdfDoc, writer)));
var worker = new XMLWorker(pipeline, true);
var xmlParse = new XMLParser(true, worker);
pdfDoc.Open();
for (int i = 0; i < gvCustomers.PageCount; i++)
{
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
gvCustomers.PageIndex = i;
gvCustomers.PagerSettings.Visible = false;
this.BindGrid();
gvCustomers.RenderControl(hw);
string body = string.Empty;
using (StreamReader reader = new StreamReader(Server.MapPath("~/EmailTemplate.html")))
{
body = reader.ReadToEnd();
}
body = body.Replace("{CustomerId}", gvCustomers.Rows[0].Cells[0].Text);
body = body.Replace("{Name}", gvCustomers.Rows[0].Cells[1].Text);
body = body.Replace("{Country}", gvCustomers.Rows[0].Cells[2].Text);
body = body.Replace("{Date_Time}", DateTime.Now.ToString("dd/MM/yyyy"));
body += "<br /><br />";
StringReader sr = new StringReader(body + "<br /><br />" + sw.ToString());
pdfDoc.NewPage();
xmlParse.Parse(sr);
}
}
}
xmlParse.Flush();
pdfDoc.Close();
gvCustomers.PagerSettings.Visible = true;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
private void BindGrid()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Protected Sub Export(ByVal sender As Object, ByVal e As EventArgs)
Dim pdfDoc As Document = New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 10.0F)
Dim writer 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(Server.MapPath("~/Barcode.css"), True)
Dim pipeline As IPipeline = New CssResolverPipeline(cssResolver, New HtmlPipeline(htmlContext, New PdfWriterPipeline(pdfDoc, writer)))
Dim worker = New XMLWorker(pipeline, True)
Dim xmlParse = New XMLParser(True, worker)
pdfDoc.Open()
For i As Integer = 0 To gvCustomers.PageCount - 1
Using sw As StringWriter = New StringWriter()
Using hw As HtmlTextWriter = New HtmlTextWriter(sw)
gvCustomers.PageIndex = i
gvCustomers.PagerSettings.Visible = False
Me.BindGrid()
gvCustomers.RenderControl(hw)
Dim body As String = String.Empty
Using reader As StreamReader = New StreamReader(Server.MapPath("~/EmailTemplate.html"))
body = reader.ReadToEnd()
End Using
body = body.Replace("{CustomerId}", gvCustomers.Rows(0).Cells(0).Text)
body = body.Replace("{Name}", gvCustomers.Rows(0).Cells(1).Text)
body = body.Replace("{Country}", gvCustomers.Rows(0).Cells(2).Text)
body = body.Replace("{Date_Time}", DateTime.Now.ToString("dd/MM/yyyy"))
body += "<br /><br />"
Dim sr As StringReader = New StringReader(body & "<br /><br />" & sw.ToString())
pdfDoc.NewPage()
xmlParse.Parse(sr)
End Using
End Using
Next
xmlParse.Flush()
pdfDoc.Close()
gvCustomers.PagerSettings.Visible = True
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.Write(pdfDoc)
Response.End()
End Sub
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
End Sub
Private Sub BindGrid()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Using
End Using
End Using
End Using
End Sub