Hi ibalochie,
Check the below sample. In this sample i have send id as file name. You need to replace id with invoiceno as file name.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
<br />
<asp:Button Text="Send Email" runat="server" OnClick="OnSend" />
Namespaces
C#
using System.Data;
using System.IO;
using System.Net.Mail;
using iTextSharp.text;
using iTextSharp.text.pdf;
VB.Net
Imports System.Data
Imports System.IO
Imports System.Net.Mail
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
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");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void OnSend(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Id", typeof(string)));
dt.Columns.Add(new DataColumn("Data", typeof(byte[])));
foreach (GridViewRow row in GridView1.Rows)
{
string id = row.Cells[0].Text.Trim();
string name = row.Cells[1].Text.Trim();
string country = row.Cells[2].Text.Trim();
dt.Rows.Add(id, GenerateReport(id, name, country));
}
SendEmail("Unpaid invoices", "Refer attached invoices", "test@gmail.com", dt);
}
private void SendEmail(string subect, string body, string to, DataTable data)
{
using (MailMessage mailMessage = new MailMessage())
{
mailMessage.From = new MailAddress("test1@gmail.com");
mailMessage.Subject = subect;
mailMessage.Body = body;
mailMessage.IsBodyHtml = true;
mailMessage.To.Add(new MailAddress(to));
foreach (DataRow dr in data.Rows)
{
Attachment attachment = new Attachment(new MemoryStream((byte[])dr["Data"]), dr["Id"].ToString() + ".pdf");
mailMessage.Attachments.Add(attachment);
}
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = mailMessage.From.Address;
NetworkCred.Password = "password";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mailMessage);
}
}
private byte[] GenerateReport(string id, string name, string country)
{
byte[] bytes = null;
Document document = new Document(PageSize.A4, 88f, 88f, 100f, 10f);
Font NormalFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, Color.BLACK);
using (MemoryStream memoryStream = new MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
Phrase phrase = null;
PdfPCell cell = null;
PdfPTable table = null;
document.Open();
table = new PdfPTable(3);
table.TotalWidth = 500f;
table.LockedWidth = true;
phrase = new Phrase();
phrase.Add(new Chunk("Id", FontFactory.GetFont("Arial", 16, Font.BOLD, Color.RED)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
table.AddCell(cell);
phrase = new Phrase();
phrase.Add(new Chunk("Name", FontFactory.GetFont("Arial", 16, Font.BOLD, Color.RED)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
table.AddCell(cell);
phrase = new Phrase();
phrase.Add(new Chunk("Country", FontFactory.GetFont("Arial", 16, Font.BOLD, Color.RED)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
table.AddCell(cell);
phrase = new Phrase();
phrase.Add(new Chunk(id, FontFactory.GetFont("Arial", 16, Font.NORMAL, Color.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
table.AddCell(cell);
phrase = new Phrase();
phrase.Add(new Chunk(name, FontFactory.GetFont("Arial", 16, Font.NORMAL, Color.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
table.AddCell(cell);
phrase = new Phrase();
phrase.Add(new Chunk(country, FontFactory.GetFont("Arial", 16, Font.NORMAL, Color.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
table.AddCell(cell);
document.Add(table);
document.Close();
bytes = memoryStream.ToArray();
memoryStream.Close();
}
return bytes;
}
private static PdfPCell PhraseCell(Phrase phrase, int align)
{
PdfPCell cell = new PdfPCell(phrase);
cell.BorderColor = Color.WHITE;
cell.VerticalAlignment = PdfCell.ALIGN_TOP;
cell.HorizontalAlignment = align;
cell.PaddingBottom = 2f;
cell.PaddingTop = 0f;
return cell;
}
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 = 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")
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Protected Sub OnSend(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = New DataTable()
dt.Columns.Add(New DataColumn("Id", GetType(String)))
dt.Columns.Add(New DataColumn("Data", GetType(Byte())))
For Each row As GridViewRow In GridView1.Rows
Dim id As String = row.Cells(0).Text.Trim()
Dim name As String = row.Cells(1).Text.Trim()
Dim country As String = row.Cells(2).Text.Trim()
dt.Rows.Add(id, GenerateReport(id, name, country))
Next
SendEmail("Unpaid invoices", "Refer attached invoices", "test@gmail.com", dt)
End Sub
Private Sub SendEmail(ByVal subect As String, ByVal body As String, ByVal [to] As String, ByVal data As DataTable)
Using mailMessage As MailMessage = New MailMessage()
mailMessage.From = New MailAddress("test1@gmail.com")
mailMessage.Subject = subect
mailMessage.Body = body
mailMessage.IsBodyHtml = True
mailMessage.To.Add(New MailAddress([to]))
For Each dr As DataRow In data.Rows
Dim attachment As Attachment = New Attachment(New MemoryStream(CType(dr("Data"), Byte())), dr("Id").ToString() & ".pdf")
mailMessage.Attachments.Add(attachment)
Next
Dim smtp As SmtpClient = New SmtpClient()
smtp.Host = "smtp.gmail.com"
smtp.EnableSsl = True
Dim NetworkCred As Net.NetworkCredential = New Net.NetworkCredential()
NetworkCred.UserName = mailMessage.From.Address
NetworkCred.Password = "password"
smtp.UseDefaultCredentials = True
smtp.Credentials = NetworkCred
smtp.Port = 587
smtp.Send(mailMessage)
End Using
End Sub
Private Function GenerateReport(ByVal id As String, ByVal name As String, ByVal country As String) As Byte()
Dim bytes As Byte() = Nothing
Dim document As Document = New Document(PageSize.A4, 88.0F, 88.0F, 100.0F, 10.0F)
Dim NormalFont As Font = FontFactory.GetFont("Arial", 12, Font.NORMAL, Color.BLACK)
Using memoryStream As MemoryStream = New MemoryStream()
Dim writer As PdfWriter = PdfWriter.GetInstance(document, memoryStream)
Dim phrase As Phrase = Nothing
Dim cell As PdfPCell = Nothing
Dim table As PdfPTable = Nothing
document.Open()
table = New PdfPTable(3)
table.TotalWidth = 500.0F
table.LockedWidth = True
phrase = New Phrase()
phrase.Add(New Chunk("Id", FontFactory.GetFont("Arial", 16, Font.BOLD, Color.RED)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
table.AddCell(cell)
phrase = New Phrase()
phrase.Add(New Chunk("Name", FontFactory.GetFont("Arial", 16, Font.BOLD, Color.RED)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
table.AddCell(cell)
phrase = New Phrase()
phrase.Add(New Chunk("Country", FontFactory.GetFont("Arial", 16, Font.BOLD, Color.RED)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
table.AddCell(cell)
phrase = New Phrase()
phrase.Add(New Chunk(id, FontFactory.GetFont("Arial", 16, Font.NORMAL, Color.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
table.AddCell(cell)
phrase = New Phrase()
phrase.Add(New Chunk(name, FontFactory.GetFont("Arial", 16, Font.NORMAL, Color.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
table.AddCell(cell)
phrase = New Phrase()
phrase.Add(New Chunk(country, FontFactory.GetFont("Arial", 16, Font.NORMAL, Color.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
table.AddCell(cell)
document.Add(table)
document.Close()
bytes = memoryStream.ToArray()
memoryStream.Close()
End Using
Return bytes
End Function
Private Shared Function PhraseCell(ByVal phrase As Phrase, ByVal align As Integer) As PdfPCell
Dim cell As PdfPCell = New PdfPCell(phrase)
cell.BorderColor = Color.WHITE
cell.VerticalAlignment = PdfCell.ALIGN_TOP
cell.HorizontalAlignment = align
cell.PaddingBottom = 2.0F
cell.PaddingTop = 0F
Return cell
End Function
For more details on PDF generation refer below article.