You need to set the PdfPCell Border, BorderColor and BorderWidth. So that the gridline will be visible.
cell.Border = Rectangle.BOX;
cell.BorderWidth = 1;
cell.BorderColor = BaseColor.BLACK;
Refer below sample.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:Button Text="Export" runat="server" OnClick="Export" />
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using iTextSharp.text;
using iTextSharp.text.pdf;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Code
C#
protected void Export(object sender, EventArgs e)
{
DataTable dt = GetData("SELECT EmployeeId,FirstName,City,Country FROM Employees");
Document document = new Document(PageSize.A3, 10f, 10f, 10f, 1f);
iTextSharp.text.Font NormalFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, BaseColor.BLACK);
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
Phrase phrase = null;
PdfPCell cell = null;
PdfPTable table = null;
document.Open();
table = new PdfPTable(1);
table.TotalWidth = 400f;
table.LockedWidth = true;
table.SetWidths(new float[] { 1f });
cell = ImageCell(@"~/img/NoImage.jpg", 40f, PdfPCell.ALIGN_CENTER);
table.AddCell(cell);
document.Add(table);
table = new PdfPTable(1);
table.TotalWidth = 400f;
table.LockedWidth = true;
table.SetWidths(new float[] { 1f });
phrase = new Phrase();
phrase.Add(new Chunk("Meja Thermal Power Project", FontFactory.GetFont("Arial", 16, Font.BOLD, BaseColor.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_TOP;
table.AddCell(cell);
document.Add(table);
table = new PdfPTable(1);
table.TotalWidth = 400f;
table.LockedWidth = true;
table.SetWidths(new float[] { 1f });
phrase = new Phrase();
phrase.Add(new Chunk("A Joint Venture of NTPC Ltd & UPRVUNL", FontFactory.GetFont("Arial", 10, BaseColor.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_TOP;
table.AddCell(cell);
document.Add(table);
table = new PdfPTable(4);
table.TotalWidth = 400f;
table.LockedWidth = true;
table.HorizontalAlignment = Element.ALIGN_LEFT;
table.SetWidths(new float[] { 0.3f, 0.5f, 0.5f, 0.5f });
table.SpacingBefore = 20f;
for (int j = 0; j < dt.Columns.Count; j++)
{
phrase = new Phrase();
phrase.Add(new Chunk(dt.Columns[j].ToString(), FontFactory.GetFont("Arial", 11, Font.BOLD, BaseColor.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
cell.Border = Rectangle.BOX;
cell.BorderWidth = 1;
cell.BorderColor = BaseColor.BLACK;
table.AddCell(cell);
}
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
phrase = new Phrase();
phrase.Add(new Chunk(dr[0].ToString(), FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
cell.Border = Rectangle.BOX;
cell.BorderWidth = 1;
cell.BorderColor = BaseColor.BLACK;
table.AddCell(cell);
phrase = new Phrase();
phrase.Add(new Chunk(dr[1].ToString(), FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
cell.Border = Rectangle.BOX;
cell.BorderWidth = 1;
cell.BorderColor = BaseColor.BLACK;
table.AddCell(cell);
phrase = new Phrase();
phrase.Add(new Chunk(dr[2].ToString(), FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
cell.Border = Rectangle.BOX;
cell.BorderWidth = 1;
cell.BorderColor = BaseColor.BLACK;
table.AddCell(cell);
phrase = new Phrase();
phrase.Add(new Chunk(dr[3].ToString(), FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
cell.Border = Rectangle.BOX;
cell.BorderWidth = 1;
cell.BorderColor = BaseColor.BLACK;
table.AddCell(cell);
}
document.Add(table);
table = new PdfPTable(1);
table.TotalWidth = 320f;
table.LockedWidth = true;
table.SetWidths(new float[] { 1f });
phrase = new Phrase();
phrase.Add(new Chunk("\n\n**This is an electronically generated report, hence does not require a signature**", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_LEFT;
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=Employee.pdf");
Response.ContentType = "application/pdf";
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
Response.Close();
}
}
private static PdfPCell PhraseCell(Phrase phrase, int align)
{
PdfPCell cell = new PdfPCell(phrase);
cell.BorderColor = BaseColor.WHITE;
cell.VerticalAlignment = PdfPCell.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.Border = Rectangle.BOX;
cell.BorderWidth = 1;
cell.BorderColor = BaseColor.WHITE;
cell.VerticalAlignment = PdfPCell.ALIGN_TOP;
cell.HorizontalAlignment = align;
cell.PaddingBottom = 0f;
cell.PaddingTop = 0f;
return cell;
}
private DataTable GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query);
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
VB.Net
Protected Sub Export(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = GetData("SELECT EmployeeId,FirstName,City,Country FROM Employees")
Dim document As Document = New Document(PageSize.A3, 10.0F, 10.0F, 10.0F, 1.0F)
Dim NormalFont As iTextSharp.text.Font = FontFactory.GetFont("Arial", 12, Font.NORMAL, BaseColor.BLACK)
Using memoryStream As System.IO.MemoryStream = New System.IO.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(1)
table.TotalWidth = 400.0F
table.LockedWidth = True
table.SetWidths(New Single() {1.0F})
cell = ImageCell("~/img/NoImage.jpg", 40.0F, PdfPCell.ALIGN_CENTER)
table.AddCell(cell)
document.Add(table)
table = New PdfPTable(1)
table.TotalWidth = 400.0F
table.LockedWidth = True
table.SetWidths(New Single() {1.0F})
phrase = New Phrase()
phrase.Add(New Chunk("Meja Thermal Power Project", FontFactory.GetFont("Arial", 16, Font.BOLD, BaseColor.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
cell.VerticalAlignment = PdfPCell.ALIGN_TOP
table.AddCell(cell)
document.Add(table)
table = New PdfPTable(1)
table.TotalWidth = 400.0F
table.LockedWidth = True
table.SetWidths(New Single() {1.0F})
phrase = New Phrase()
phrase.Add(New Chunk("A Joint Venture of NTPC Ltd & UPRVUNL", FontFactory.GetFont("Arial", 10, BaseColor.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
cell.VerticalAlignment = PdfPCell.ALIGN_TOP
table.AddCell(cell)
document.Add(table)
table = New PdfPTable(4)
table.TotalWidth = 400.0F
table.LockedWidth = True
table.HorizontalAlignment = Element.ALIGN_LEFT
table.SetWidths(New Single() {0.3F, 0.5F, 0.5F, 0.5F})
table.SpacingBefore = 20.0F
For j As Integer = 0 To dt.Columns.Count - 1
phrase = New Phrase()
phrase.Add(New Chunk(dt.Columns(j).ToString(), FontFactory.GetFont("Arial", 11, Font.BOLD, BaseColor.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE
cell.Border = Rectangle.BOX
cell.BorderWidth = 1
cell.BorderColor = BaseColor.BLACK
table.AddCell(cell)
Next
For i As Integer = 0 To dt.Rows.Count - 1
Dim dr As DataRow = dt.Rows(i)
phrase = New Phrase()
phrase.Add(New Chunk(dr(0).ToString(), FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE
cell.Border = Rectangle.BOX
cell.BorderWidth = 1
cell.BorderColor = BaseColor.BLACK
table.AddCell(cell)
phrase = New Phrase()
phrase.Add(New Chunk(dr(1).ToString(), FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE
cell.Border = Rectangle.BOX
cell.BorderWidth = 1
cell.BorderColor = BaseColor.BLACK
table.AddCell(cell)
phrase = New Phrase()
phrase.Add(New Chunk(dr(2).ToString(), FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE
cell.Border = Rectangle.BOX
cell.BorderWidth = 1
cell.BorderColor = BaseColor.BLACK
table.AddCell(cell)
phrase = New Phrase()
phrase.Add(New Chunk(dr(3).ToString(), FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE
cell.Border = Rectangle.BOX
cell.BorderWidth = 1
cell.BorderColor = BaseColor.BLACK
table.AddCell(cell)
Next
document.Add(table)
table = New PdfPTable(1)
table.TotalWidth = 320.0F
table.LockedWidth = True
table.SetWidths(New Single() {1.0F})
phrase = New Phrase()
phrase.Add(New Chunk(vbLf & vbLf & "**This is an electronically generated report, hence does not require a signature**", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
cell.VerticalAlignment = PdfPCell.ALIGN_LEFT
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=Employee.pdf")
Response.ContentType = "application/pdf"
Response.Buffer = True
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.BinaryWrite(bytes)
Response.End()
Response.Close()
End Using
End Sub
Private Shared Function PhraseCell(ByVal phrase As Phrase, ByVal align As Integer) As PdfPCell
Dim cell As PdfPCell = New PdfPCell(phrase)
cell.BorderColor = BaseColor.WHITE
cell.VerticalAlignment = PdfPCell.ALIGN_TOP
cell.HorizontalAlignment = align
cell.PaddingBottom = 2.0F
cell.PaddingTop = 0F
Return cell
End Function
Private Shared Function ImageCell(ByVal path As String, ByVal scale As Single, ByVal 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 PdfPCell = New PdfPCell(image)
cell.Border = Rectangle.BOX
cell.BorderWidth = 1
cell.BorderColor = BaseColor.WHITE
cell.VerticalAlignment = PdfPCell.ALIGN_TOP
cell.HorizontalAlignment = align
cell.PaddingBottom = 0F
cell.PaddingTop = 0F
Return cell
End Function
Private Function GetData(ByVal query As String) As DataTable
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand(query)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Screenshot