Hi baswal07,
You can't download each file separately. You need to make create zip file and then add all file to the zip and download.
For this i have used ItextSharp and DotNetZip library.
Check this example. Now please take its reference and correct your code.
For ITextSharp refer below article.
For DotNetZip refer below article.
HTML
<asp:Button ID="btnReport" runat="server" Text="Generate Report" OnClick="OnReportClick" />
Namespaces
C#
using System.Collections.Generic;
using System.Data;
using System.IO;
using Ionic.Zip;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
VB.Net
Imports System.Collections.Generic
Imports System.Data
Imports System.IO
Imports Ionic.Zip
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.tool.xml
Code
C#
protected void OnReportClick(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("Id"),
new DataColumn("Name"),
new DataColumn("Country") });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
List<FileData> files = new List<FileData>();
for (int i = 0; i < dt.Rows.Count; i++)
{
string myfile = dt.Rows[i][0] + "_" + DateTime.Now.ToString("ddMMyyyyHHms") + ".pdf";
DataRow[] dr = dt.Select("Id=" + dt.Rows[i][0]);
files.Add(new FileData { FileName = myfile, FileByte = ExportToPDF(myfile, dr.CopyToDataTable()) });
}
using (ZipFile zip = new ZipFile())
{
zip.AlternateEncodingUsage = ZipOption.AsNecessary;
foreach (FileData fd in files)
{
zip.AddEntry(fd.FileName, fd.FileByte);
}
Response.Clear();
Response.BufferOutput = false;
string zipName = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=" + zipName);
zip.Save(Response.OutputStream);
Response.End();
}
}
protected byte[] ExportToPDF(string myfile, DataTable dt)
{
GridView GridView1 = new GridView();
GridView1.DataSource = dt;
GridView1.DataBind();
byte[] bytes;
using (MemoryStream memoryStream = new MemoryStream())
{
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
pdfDoc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
pdfDoc.Close();
bytes = memoryStream.ToArray();
memoryStream.Close();
}
}
}
return bytes;
}
public class FileData
{
public string FileName { get; set; }
public byte[] FileByte { get; set; }
}
VB.Net
Protected Sub OnReportClick(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
Dim files As List(Of FileData) = New List(Of FileData)()
For i As Integer = 0 To dt.Rows.Count - 1
Dim myfile As String = dt.Rows(i)(0) & "_" + DateTime.Now.ToString("ddMMyyyyHHms") & ".pdf"
Dim dr As DataRow() = dt.Select("Id=" & dt.Rows(i)(0))
files.Add(New FileData With {
.FileName = myfile,
.FileByte = ExportToPDF(myfile, dr.CopyToDataTable())
})
Next
Using zip As ZipFile = New ZipFile()
zip.AlternateEncodingUsage = ZipOption.AsNecessary
For Each fd As FileData In files
zip.AddEntry(fd.FileName, fd.FileByte)
Next
Response.Clear()
Response.BufferOutput = False
Dim zipName As String = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"))
Response.ContentType = "application/zip"
Response.AddHeader("content-disposition", "attachment; filename=" & zipName)
zip.Save(Response.OutputStream)
Response.End()
End Using
End Sub
Protected Function ExportToPDF(ByVal myfile As String, ByVal dt As DataTable) As Byte()
Dim GridView1 As GridView = New GridView()
GridView1.DataSource = dt
GridView1.DataBind()
Dim bytes As Byte()
Using memoryStream As MemoryStream = New MemoryStream()
Using sw As StringWriter = New StringWriter()
Using hw As HtmlTextWriter = New HtmlTextWriter(sw)
GridView1.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 writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, memoryStream)
pdfDoc.Open()
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr)
pdfDoc.Close()
bytes = memoryStream.ToArray()
memoryStream.Close()
End Using
End Using
End Using
Return bytes
End Function
Public Class FileData
Public Property FileName As String
Public Property FileByte As Byte()
End Class
Screenshot
Generated Zip File with each record as separate PDF