Hi fareed.fd7,
Check with the below code.
protected void Page_Load(object sender, EventArgs e)
{
DataTable dtCustomers = GetDate("SELECT TOP 10 CustomerID,ContactName,Address FROM Customers");
DataTable dtEmployees = GetDate("SELECT TOP 10 EmployeeID,FirstName,LastName,Address FROM Employees");
DataSet ds = new DataSet();
ds.Tables.Add(dtCustomers);
ds.Tables.Add(dtEmployees);
Document document = new Document(PageSize.A4, 0, 0, 80, 80);
System.IO.MemoryStream msReport = new System.IO.MemoryStream();
FileStream fs = File.Open(Server.MapPath("~/Files/Test.pdf"), FileMode.OpenOrCreate);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();
PdfPTable table = null;
PdfPCell cell = null;
Phrase phrase = null;
for (int i = 0; i < ds.Tables.Count; i++)
{
document.NewPage();
table = new PdfPTable(ds.Tables[i].Columns.Count);
for (int row = 1; row <= ds.Tables[i].Rows.Count; row++)
{
for (int column = 1; column <= ds.Tables[i].Columns.Count; column++)
{
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_TOP;
phrase = new Phrase();
phrase.Add(new Chunk(ds.Tables[i].Rows[row - 1][column - 1].ToString(), FontFactory.GetFont("Arial", 5, Font.NORMAL, BaseColor.BLACK)));
table.AddCell(cell);
}
}
document.Add(table);
}
document.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 StringBuilder GenerateHtmlStringFromDataTable(DataTable dt)
{
StringBuilder sb = new StringBuilder();
sb.Append("<table border = '1'>");
sb.Append("<tr>");
foreach (DataColumn column in dt.Columns)
{
sb.Append("<th style = 'color:red'>");
sb.Append(column.ColumnName);
sb.Append("</th>");
}
sb.Append("</tr>");
foreach (DataRow row in dt.Rows)
{
sb.Append("<tr>");
foreach (DataColumn column in dt.Columns)
{
sb.Append("<td>");
sb.Append(row[column]);
sb.Append("</td>");
}
sb.Append("</tr>");
}
sb.Append("</table>");
return sb;
}
private DataTable GetDate(string query)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = query;
using (SqlDataAdapter da = new SqlDataAdapter())
{
con.Open();
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
con.Close();
return dt;
}
}
}
}