Hey nadeem1218,
Please refer below smaple.
HTML
<asp:ListView ID="lvImages" runat="server" GroupPlaceholderID="groupPlaceHolder1"
ItemPlaceholderID="itemPlaceHolder1">
<LayoutTemplate>
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<th>
Photos
</th>
</tr>
<asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<GroupTemplate>
<tr>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
</tr>
</GroupTemplate>
<ItemTemplate>
<td>
<asp:Image ID="Image1" ImageUrl='<%# Eval("Value") %>' runat="server" Height="50"
Width="50" />
</td>
</ItemTemplate>
</asp:ListView>
<br />
<asp:Button Text="Export To Pdf" runat="server" OnClick="ExportPdf" />
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System.Collections.Generic;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.IO
Imports iTextSharp.text.pdf
Imports iTextSharp.text
Imports iTextSharp.text.html.simpleparser
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = GetData();
string[] filePaths = Directory.GetFiles(@"\\144.44.0.100\Share\USERS\Path\");
List<System.Web.UI.WebControls.ListItem> files = new List<System.Web.UI.WebControls.ListItem>();
foreach (string filePath in filePaths)
{
foreach (DataRow row in dt.Rows)
{
string fileName = Path.GetFileName(filePath);
if (fileName.Split('.', ' ')[0] == row["Id"].ToString())
{
files.Add(new System.Web.UI.WebControls.ListItem(fileName, @"\\144.44.0.100\Share\USERS\Path\" + fileName));
}
}
}
lvImages.DataSource = files;
lvImages.DataBind();
}
}
private DataTable GetData()
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("SELECT * FROM tblFilespath", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
return dt;
}
protected void ExportPdf(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
DataTable dt = GetData();
string[] filePaths = Directory.GetFiles(@"\\144.44.0.100\Share\USERS\Path\");
List<System.Web.UI.WebControls.ListItem> files = new List<System.Web.UI.WebControls.ListItem>();
foreach (string filePath in filePaths)
{
foreach (DataRow row in dt.Rows)
{
string fileName = Path.GetFileName(filePath);
if (fileName.Split('.', ' ')[0] == row["Id"].ToString())
{
files.Add(new System.Web.UI.WebControls.ListItem(fileName, @"\\144.44.0.100\Share\USERS\Path\" + fileName));
}
}
}
lvImages.DataSource = files;
lvImages.DataBind();
lvImages.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = GetData()
Dim filePaths As String() = Directory.GetFiles("\\144.44.0.100\Share\USERS\Path\")
Dim files As List(Of System.Web.UI.WebControls.ListItem) = New List(Of System.Web.UI.WebControls.ListItem)()
For Each filePath As String In filePaths
For Each row As DataRow In dt.Rows
Dim fileName As String = Path.GetFileName(filePath)
If fileName.Split("."c, " "c)(0) = row("Id").ToString() Then
files.Add(New System.Web.UI.WebControls.ListItem(fileName, "\\144.44.0.100\Share\USERS\Path\" & fileName))
End If
Next
Next
lvImages.DataSource = files
lvImages.DataBind()
End If
End Sub
Private Function GetData() As DataTable
Dim dt As DataTable = New DataTable()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim con As SqlConnection = New SqlConnection(constr)
Dim cmd As SqlCommand = New SqlCommand("SELECT * FROM tblFilespath", con)
Dim sda As SqlDataAdapter = New SqlDataAdapter(cmd)
sda.Fill(dt)
Return dt
End Function
Protected Sub ExportPdf(ByVal sender As Object, ByVal e As EventArgs)
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim sw As StringWriter = New StringWriter()
Dim hw As HtmlTextWriter = New HtmlTextWriter(sw)
Dim dt As DataTable = GetData()
Dim filePaths As String() = Directory.GetFiles("\\144.44.0.100\Share\USERS\Path\")
Dim files As List(Of System.Web.UI.WebControls.ListItem) = New List(Of System.Web.UI.WebControls.ListItem)()
For Each filePath As String In filePaths
For Each row As DataRow In dt.Rows
Dim fileName As String = Path.GetFileName(filePath)
If fileName.Split("."c, " "c)(0) = row("Id").ToString() Then
files.Add(New System.Web.UI.WebControls.ListItem(fileName, "\\144.44.0.100\Share\USERS\Path\" & fileName))
End If
Next
Next
lvImages.DataSource = files
lvImages.DataBind()
lvImages.RenderControl(hw)
Dim sr As StringReader = New StringReader(sw.ToString())
Dim pdfDoc As Document = New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F)
Dim htmlparser As HTMLWorker = New HTMLWorker(pdfDoc)
PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
htmlparser.Parse(sr)
pdfDoc.Close()
Response.Write(pdfDoc)
Response.End()
End Sub
Screenshot