Hi ilanocf,
Using this article i have created the example. Refer the example modified as per your requirement.
HTML
<asp:Button ID="btnReport" runat="server" Text="Generate Report" OnClick="GenerateReport" />
Namespaces
C#
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
VB.Net
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.IO
Code
C#
protected void GenerateReport(object sender, EventArgs e)
{
DataTable dt = new DataTable();
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlCommand cmd = new SqlCommand("SELECT TOP 2 CustomerId, ContactName, Country FROM Customers");
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
Document document = new Document(PageSize.A4, 88f, 88f, 10f, 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;
Color color = null;
document.Open();
for (int i = 0; i < dt.Rows.Count; i++)
{
/* Common Header on each page start*/
//Header Table
table = new PdfPTable(2);
table.TotalWidth = 500f;
table.LockedWidth = true;
table.SetWidths(new float[] { 0.3f, 0.7f });
//Company Logo
cell = ImageCell("~/images/northwindlogo.gif", 30f, PdfPCell.ALIGN_CENTER);
table.AddCell(cell);
//Company Name and Address
phrase = new Phrase();
phrase.Add(new Chunk("Microsoft Northwind Traders Company\n\n", FontFactory.GetFont("Arial", 16, Font.BOLD, Color.RED)));
phrase.Add(new Chunk("107, Park site,\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
phrase.Add(new Chunk("Salt Lake Road,\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
phrase.Add(new Chunk("Seattle, USA", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfCell.ALIGN_TOP;
table.AddCell(cell);
//Separater Line
color = new Color(System.Drawing.ColorTranslator.FromHtml("#A9A9A9"));
DrawLine(writer, 25f, document.Top - 79f, document.PageSize.Width - 25f, document.Top - 79f, color);
DrawLine(writer, 25f, document.Top - 80f, document.PageSize.Width - 25f, document.Top - 80f, color);
document.Add(table);
/* Common Header on each page end*/
/* Data for each page start*/
phrase = new Phrase();
phrase.Add(new Chunk("\n\nId:", FontFactory.GetFont("Arial", 16, Font.BOLD, Color.RED)));
document.Add(phrase);
phrase = new Phrase();
phrase.Add(new Chunk("000" + dt.Rows[i]["CustomerId"], FontFactory.GetFont("Arial", 12, Font.NORMAL, Color.BLACK)));
document.Add(phrase);
phrase = new Phrase();
phrase.Add(new Chunk("\n\nName:", FontFactory.GetFont("Arial", 16, Font.BOLD, Color.RED)));
document.Add(phrase);
phrase = new Phrase();
phrase.Add(new Chunk(dt.Rows[i]["ContactName"].ToString(), FontFactory.GetFont("Arial", 12, Font.NORMAL, Color.BLACK)));
document.Add(phrase);
phrase = new Phrase();
phrase.Add(new Chunk("\n\nCountry:", FontFactory.GetFont("Arial", 16, Font.BOLD, Color.RED)));
document.Add(phrase);
phrase = new Phrase();
phrase.Add(new Chunk(dt.Rows[i]["Country"].ToString(), FontFactory.GetFont("Arial", 12, Font.NORMAL, Color.BLACK)));
document.Add(phrase);
/* Data for each page end*/
// Add new page.
document.NewPage();
}
document.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=Employee.pdf");
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
Response.Close();
}
}
private static void DrawLine(PdfWriter writer, float x1, float y1, float x2, float y2, Color color)
{
PdfContentByte contentByte = writer.DirectContent;
contentByte.SetColorStroke(color);
contentByte.MoveTo(x1, y1);
contentByte.LineTo(x2, y2);
contentByte.Stroke();
}
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;
}
private static PdfPCell ImageCell(string path, float scale, int align)
{
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath(path));
image.ScalePercent(scale);
PdfPCell cell = new PdfPCell(image);
cell.BorderColor = Color.WHITE;
cell.VerticalAlignment = PdfCell.ALIGN_TOP;
cell.HorizontalAlignment = align;
cell.PaddingBottom = 0f;
cell.PaddingTop = 0f;
return cell;
}
VB.Net
Protected Sub GenerateReport(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = New DataTable()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim cmd As SqlCommand = New SqlCommand("SELECT TOP 2 CustomerId, ContactName, Country FROM Customers")
Using con As SqlConnection = New SqlConnection(conString)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
sda.Fill(dt)
End Using
End Using
Dim document As Document = New Document(PageSize.A4, 88.0F, 88.0F, 10.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
Dim color As Color = Nothing
document.Open()
For i As Integer = 0 To dt.Rows.Count - 1
'Common Header on each page start
'Header Table
table = New PdfPTable(2)
table.TotalWidth = 500.0F
table.LockedWidth = True
table.SetWidths(New Single() {0.3F, 0.7F})
'Company Logo
cell = ImageCell("~/images/northwindlogo.gif", 30.0F, PdfPCell.ALIGN_CENTER)
table.AddCell(cell)
'Company Name and Address
phrase = New Phrase()
phrase.Add(New Chunk("Microsoft Northwind Traders Company" & vbLf & vbLf, FontFactory.GetFont("Arial", 16, Font.BOLD, Color.RED)))
phrase.Add(New Chunk("107, Park site," & vbLf, FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)))
phrase.Add(New Chunk("Salt Lake Road," & vbLf, FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)))
phrase.Add(New Chunk("Seattle, USA", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
cell.VerticalAlignment = PdfCell.ALIGN_TOP
table.AddCell(cell)
'Separater Line
color = New Color(System.Drawing.ColorTranslator.FromHtml("#A9A9A9"))
DrawLine(writer, 25.0F, document.Top - 79.0F, document.PageSize.Width - 25.0F, document.Top - 79.0F, color)
DrawLine(writer, 25.0F, document.Top - 80.0F, document.PageSize.Width - 25.0F, document.Top - 80.0F, color)
document.Add(table)
'Common Header on each page end
'Data for each page start
phrase = New Phrase()
phrase.Add(New Chunk(vbLf & vbLf & "Id:", FontFactory.GetFont("Arial", 16, Font.BOLD, Color.RED)))
document.Add(phrase)
phrase = New Phrase()
phrase.Add(New Chunk("000" & dt.Rows(i)("CustomerId").ToString(), FontFactory.GetFont("Arial", 12, Font.NORMAL, Color.BLACK)))
document.Add(phrase)
phrase = New Phrase()
phrase.Add(New Chunk(vbLf & vbLf & "Name:", FontFactory.GetFont("Arial", 16, Font.BOLD, Color.RED)))
document.Add(phrase)
phrase = New Phrase()
phrase.Add(New Chunk(dt.Rows(i)("ContactName").ToString(), FontFactory.GetFont("Arial", 12, Font.NORMAL, Color.BLACK)))
document.Add(phrase)
phrase = New Phrase()
phrase.Add(New Chunk(vbLf & vbLf & "Country:", FontFactory.GetFont("Arial", 16, Font.BOLD, Color.RED)))
document.Add(phrase)
phrase = New Phrase()
phrase.Add(New Chunk(dt.Rows(i)("Country").ToString(), FontFactory.GetFont("Arial", 12, Font.NORMAL, Color.BLACK)))
document.Add(phrase)
'Data for each page end
'Add new page.
document.NewPage()
Next
document.Close()
Dim bytes As Byte() = memoryStream.ToArray()
memoryStream.Close()
Response.Clear()
Response.ContentType = "application/pdf"
Response.AddHeader("Content-Disposition", "attachment; filename=Employee.pdf")
Response.Buffer = True
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.BinaryWrite(bytes)
Response.End()
Response.Close()
End Using
End Sub
Private Shared Sub DrawLine(writer As PdfWriter, x1 As Single, y1 As Single, x2 As Single, y2 As Single, color As Color)
Dim contentByte As PdfContentByte = writer.DirectContent
contentByte.SetColorStroke(color)
contentByte.MoveTo(x1, y1)
contentByte.LineTo(x2, y2)
contentByte.Stroke()
End Sub
Private Shared Function PhraseCell(phrase As Phrase, align As Integer) As PdfPCell
Dim cell As New PdfPCell(phrase)
cell.BorderColor = Color.WHITE
cell.VerticalAlignment = PdfCell.ALIGN_TOP
cell.HorizontalAlignment = align
cell.PaddingBottom = 2.0F
cell.PaddingTop = 0.0F
Return cell
End Function
Private Shared Function ImageCell(path As String, scale As Single, align As Integer) As PdfPCell
Dim image As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath(path))
image.ScalePercent(scale)
Dim cell As New PdfPCell(image)
cell.BorderColor = Color.WHITE
cell.VerticalAlignment = PdfCell.ALIGN_TOP
cell.HorizontalAlignment = align
cell.PaddingBottom = 0.0F
cell.PaddingTop = 0.0F
Return cell
End Function
Screenshot