I
wrote series of articles about create PDF document in ASP.NET with C# using iTextSharp. In this
article I’m going to explain how to create Tables in PDF document. Other articles related to iTextSharp
How
to create PDF document using iTextSharp
How
to change PDF document page size using iTextSharp
How
to use Images in PDF document using iTextSharp
Image
alignment in PDF document using iTextSharp
How
to export GridView records to PDF document using iTextSharp
Here
I’m going to explain how to create Table in PDF document using iTextSharp. First we have to download
iTextSharp.dll class library and include to our project. You can download iTextSharp.dll
class library here
http://sourceforge.net/projects/itextsharp/
Then open your project and include
that iTextSharp.dll class library.

Browse the iTextSharp.dll file which
is downloaded now and click ok.

iTextSharp allows you to create Table
in PDF document. PdfPTable and PdfPCell classes used to
create a table.
Sample code:
PdfPTable :
PdfPTable
table = new PdfPTable(3);
table.TotalWidth
= 400f;
//fix the absolute width of the table
table.LockedWidth
= true;
//relative col widths in proportions - 1/3 and 2/3
float[]
widths = new float[]
{ 2f, 4f, 6f };
table.SetWidths(widths);
table.HorizontalAlignment
= 0;
//leave a gap before and after the table
table.SpacingBefore
= 20f;
table.SpacingAfter
= 30f;
PdfPCell :
PdfPCell
cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
cell.Colspan
= 3;
cell.HorizontalAlignment
= 1; //0=Left, 1=Centre, 2=Right
table.AddCell(cell);
table.AddCell("Col 1 Row 1");
table.AddCell("Col 2 Row 1");
table.AddCell("Col 3 Row 1");
table.AddCell("Col 1 Row 2");
table.AddCell("Col 2 Row 2");
table.AddCell("Col 3 Row 2");
You can create your designer page now
Designer source code:
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="_Default"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnShow"
runat="server"
Text="Show
PDF"
OnClick="btnShow_OnClick"/>
</div>
</form>
</body>
</html>
Here
you have to use following namespaces and create one folder for store PDF
documents
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
public partial class _Default :
System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
}
protected void
Button1_Click(object sender, EventArgs e)
{
//server folder path which is stored your PDF
documents
string path = Server.MapPath("PDF-Files");
string filename = path + "/Doc1.pdf";
//Create new PDF document
Document document = new
Document(PageSize.A4,
20f, 20f, 20f, 20f);
try
{
PdfWriter.GetInstance(document, new FileStream(filename,
FileMode.Create));
PdfPTable table = new PdfPTable(3);
table.TotalWidth = 400f;
//fix the absolute width of the table
table.LockedWidth = true;
//relative col
widths in proportions - 1/3 and 2/3
float[] widths = new float[] { 2f, 4f, 6f };
table.SetWidths(widths);
table.HorizontalAlignment = 0;
//leave a gap before and after the table
table.SpacingBefore = 20f;
table.SpacingAfter = 30f;
PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
cell.Colspan = 3;
cell.HorizontalAlignment = 1; //0=Left,
1=Centre, 2=Right
table.AddCell(cell);
table.AddCell("Col 1 Row 1");
table.AddCell("Col 2 Row 1");
table.AddCell("Col 3 Row 1");
table.AddCell("Col 1 Row 2");
table.AddCell("Col 2 Row 2");
table.AddCell("Col
3 Row 2");
document.Open();
document.Add(table);
}
catch (Exception
ex)
{
}
finally
{
document.Close();
ShowPdf(filename);
}
}
public void ShowPdf(string filename)
{
//Clears all content output from Buffer Stream
Response.ClearContent();
//Clears all headers from Buffer Stream
Response.ClearHeaders();
//Adds an HTTP header to the output stream
Response.AddHeader("Content-Disposition",
"inline;filename=" + filename);
//Gets or Sets the HTTP MIME type of the output stream
Response.ContentType = "application/pdf";
//Writes the content of the specified file directory to an
HTTP response output stream as a file block
Response.WriteFile(filename);
//sends all currently buffered output to the client
Response.Flush();
//Clears all content output from Buffer Stream
Response.Clear();
}
}