Hi sureshMGR,
Check this example. Now please take its reference and correct your code.
Namespaces
C#
using iTextSharp.text;
using iTextSharp.text.pdf;
VB.Net
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
Document document = new Document(PageSize.A4, 88f, 88f, 10f, 10f);
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
PdfPCell cell = null;
PdfPTable table = null;
document.Open();
table = new PdfPTable(3);
table.TotalWidth = 400f;
table.LockedWidth = true;
table.SetWidths(new float[] { 4f, 4f, 4f });
table.HorizontalAlignment = 0;
table.SpacingBefore = 20f;
table.SpacingAfter = 30f;
cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
cell.Colspan = 3;
cell.BackgroundColor = new BaseColor(System.Drawing.Color.Red);
cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
table.AddCell(cell);
cell = new PdfPCell(new Phrase("Col 1 Row 1"));
cell.BackgroundColor = new BaseColor(System.Drawing.Color.Yellow);
table.AddCell(cell);
cell = new PdfPCell(new Phrase("Col 2 Row 1"));
cell.BackgroundColor = new BaseColor(System.Drawing.Color.Gray);
table.AddCell(cell);
cell = new PdfPCell(new Phrase("Col 3 Row 1"));
cell.BackgroundColor = new BaseColor(System.Drawing.Color.LimeGreen);
table.AddCell(cell);
cell = new PdfPCell(new Phrase("Col 1 Row 2"));
cell.BackgroundColor = new BaseColor(System.Drawing.Color.Yellow);
table.AddCell(cell);
cell = new PdfPCell(new Phrase("Col 2 Row 2"));
cell.BackgroundColor = new BaseColor(System.Drawing.Color.Gray);
table.AddCell(cell);
cell = new PdfPCell(new Phrase("Col 3 Row 2"));
cell.BackgroundColor = new BaseColor(System.Drawing.Color.LimeGreen);
table.AddCell(cell);
document.Add(table);
document.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=Test.pdf");
Response.ContentType = "application/pdf";
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
Response.Close();
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim document As Document = New Document(PageSize.A4, 88.0F, 88.0F, 10.0F, 10.0F)
Using memoryStream As System.IO.MemoryStream = New System.IO.MemoryStream()
Dim writer As PdfWriter = PdfWriter.GetInstance(document, memoryStream)
Dim cell As PdfPCell = Nothing
Dim table As PdfPTable = Nothing
document.Open()
table = New PdfPTable(3)
table.TotalWidth = 400.0F
table.LockedWidth = True
table.SetWidths(New Single() {4.0F, 4.0F, 4.0F})
table.HorizontalAlignment = 0
table.SpacingBefore = 20.0F
table.SpacingAfter = 30.0F
cell = New PdfPCell(New Phrase("Header spanning 3 columns"))
cell.Colspan = 3
cell.BackgroundColor = New BaseColor(System.Drawing.Color.Red)
cell.HorizontalAlignment = 1
table.AddCell(cell)
cell = New PdfPCell(New Phrase("Col 1 Row 1"))
cell.BackgroundColor = New BaseColor(System.Drawing.Color.Yellow)
table.AddCell(cell)
cell = New PdfPCell(New Phrase("Col 2 Row 1"))
cell.BackgroundColor = New BaseColor(System.Drawing.Color.Gray)
table.AddCell(cell)
cell = New PdfPCell(New Phrase("Col 3 Row 1"))
cell.BackgroundColor = New BaseColor(System.Drawing.Color.LimeGreen)
table.AddCell(cell)
cell = New PdfPCell(New Phrase("Col 1 Row 2"))
cell.BackgroundColor = New BaseColor(System.Drawing.Color.Yellow)
table.AddCell(cell)
cell = New PdfPCell(New Phrase("Col 2 Row 2"))
cell.BackgroundColor = New BaseColor(System.Drawing.Color.Gray)
table.AddCell(cell)
cell = New PdfPCell(New Phrase("Col 3 Row 2"))
cell.BackgroundColor = New BaseColor(System.Drawing.Color.LimeGreen)
table.AddCell(cell)
document.Add(table)
document.Close()
Dim bytes As Byte() = memoryStream.ToArray()
memoryStream.Close()
Response.Clear()
Response.ContentType = "application/pdf"
Response.AddHeader("Content-Disposition", "attachment; filename=Test.pdf")
Response.ContentType = "application/pdf"
Response.Buffer = True
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.BinaryWrite(bytes)
Response.End()
Response.Close()
End Using
End Sub
Screenshot