Hi engzezo83,
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="50" />
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="100" />
<asp:HyperLinkField DataTextField="Link" DataNavigateUrlFields="Link" HeaderText="Link" />
<asp:TemplateField HeaderText="Url">
<ItemTemplate>
<asp:HyperLink NavigateUrl='<%#Eval("Link") %>' runat="server" Text='<%#Eval("Link") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button Text="Export to PDF" runat="server" OnClick="OnExport" />
Namespaces
C#
using System.Data;
using iTextSharp.text;
using iTextSharp.text.pdf;
VB.Net
Imports System.Data
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BindGrid();
}
}
protected void OnExport(object sender, EventArgs e)
{
GridView1.AllowPaging = false;
BindGrid();
PdfPTable table = new PdfPTable(GridView1.Columns.Count);
float[] widths = new float[] { 10f, 20f, 30f, 30f };
table.SetWidths(widths);
BaseFont bf = BaseFont.CreateFont("C:\\Windows\\Fonts\\Arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
for (int i = 0; i < GridView1.Columns.Count; i++)
{
string headerText = GridView1.Columns[i].HeaderText;
PdfPCell cell = new PdfPCell(new Phrase(8, headerText, new Font(bf, 8, Font.BOLD, BaseColor.BLACK)));
table.AddCell(cell);
}
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
{
for (int j = 0; j < GridView1.Columns.Count; j++)
{
string cellText = Server.HtmlDecode(GridView1.Rows[i].Cells[j].Text);
foreach (Control control in GridView1.Rows[i].Cells[j].Controls)
{
switch (control.GetType().Name)
{
case "HyperLink":
cellText = (control as HyperLink).NavigateUrl;
break;
case "TextBox":
cellText = (control as TextBox).Text;
break;
case "LinkButton":
cellText = (control as LinkButton).Text;
break;
}
}
PdfPCell cell = new PdfPCell(new Phrase(8, cellText, new Font(bf, 6, Font.NORMAL, BaseColor.BLACK)));
table.AddCell(cell);
}
}
}
Document pdfDoc = new Document(PageSize.A5, 10f, 10f, 50f, 10f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
pdfDoc.Add(table);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;" + "filename=GridLink.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
private void BindGrid()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("Id"),
new DataColumn("Name"),
new DataColumn("Link") });
dt.Rows.Add(1, "ASPForums", "https://www.aspforums.net");
dt.Rows.Add(2, "ASPSnippets", "https://www.aspsnippets.com/");
dt.Rows.Add(3, "jQueryFaqs", "http://www.jqueryfaqs.com/");
GridView1.DataSource = dt;
GridView1.DataBind();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
BindGrid()
End If
End Sub
Protected Sub OnExport(ByVal sender As Object, ByVal e As EventArgs)
GridView1.AllowPaging = False
BindGrid()
Dim table As PdfPTable = New PdfPTable(GridView1.Columns.Count)
Dim widths As Single() = New Single() {10.0F, 20.0F, 30.0F, 30.0F}
table.SetWidths(widths)
Dim bf As BaseFont = BaseFont.CreateFont("C:\Windows\Fonts\Arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED)
For i As Integer = 0 To GridView1.Columns.Count - 1
Dim headerText As String = GridView1.Columns(i).HeaderText
Dim cell As PdfPCell = New PdfPCell(New Phrase(8, headerText, New Font(bf, 8, Font.BOLD, BaseColor.BLACK)))
table.AddCell(cell)
Next
For i As Integer = 0 To GridView1.Rows.Count - 1
If GridView1.Rows(i).RowType = DataControlRowType.DataRow Then
For j As Integer = 0 To GridView1.Columns.Count - 1
Dim cellText As String = Server.HtmlDecode(GridView1.Rows(i).Cells(j).Text)
For Each control As Control In GridView1.Rows(i).Cells(j).Controls
Select Case control.[GetType]().Name
Case "HyperLink"
cellText = (TryCast(control, HyperLink)).NavigateUrl
Case "TextBox"
cellText = (TryCast(control, TextBox)).Text
Case "LinkButton"
cellText = (TryCast(control, LinkButton)).Text
End Select
Next
Dim cell As PdfPCell = New PdfPCell(New Phrase(8, cellText, New Font(bf, 6, Font.NORMAL, BaseColor.BLACK)))
table.AddCell(cell)
Next
End If
Next
Dim pdfDoc As Document = New Document(PageSize.A5, 10.0F, 10.0F, 50.0F, 10.0F)
PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
pdfDoc.Add(table)
pdfDoc.Close()
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;" & "filename=GridLink.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.Write(pdfDoc)
Response.[End]()
End Sub
Private Sub BindGrid()
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {
New DataColumn("Id"),
New DataColumn("Name"),
New DataColumn("Link")})
dt.Rows.Add(1, "ASPForums", "https://www.aspforums.net")
dt.Rows.Add(2, "ASPSnippets", "https://www.aspsnippets.com/")
dt.Rows.Add(3, "jQueryFaqs", "http://www.jqueryfaqs.com/")
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Screenshot