Hi ashhadsaud,
I have created one sample that full-fill your requirement.
Code
protected void AddDataTableToPDF(object sender, EventArgs e)
{
String pathin = @"F:\Users\dharmendra\Desktop\test.pdf";
String pathout = @"F:\Users\dharmendra\Desktop\test1.pdf";
PdfReader reader = new PdfReader(pathin);
PdfStamper stamper = new PdfStamper(reader, new FileStream(pathout, FileMode.Create));
PdfPCell cell = null;
PdfPTable table = null;
DataTable dt = GetDataTable();
if (dt != null)
{
Font font8 = FontFactory.GetFont("ARIAL", 7);
table = new PdfPTable(dt.Columns.Count);
cell = new PdfPCell(new Phrase(new Chunk("ID", font8)));
table.AddCell(cell);
cell = new PdfPCell(new Phrase(new Chunk("Name", font8)));
table.AddCell(cell);
cell = new PdfPCell(new Phrase(new Chunk("Country", font8)));
table.AddCell(cell);
for (int rows = 0; rows < dt.Rows.Count; rows++)
{
for (int column = 0; column < dt.Columns.Count; column++)
{
cell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows][column].ToString(), font8)));
table.AddCell(cell);
}
}
}
ColumnText ct = new ColumnText(stamper.GetOverContent(1));
ct.AddElement(table);
Rectangle rect = new Rectangle(46, 190, 530, 36);
ct.SetSimpleColumn(36, 36, PageSize.A4.Width - 36, PageSize.A4.Height - 175);
ct.Go();
stamper.Close();
reader.Close();
}
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 DataTable GetDataTable()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
return dt;
}
Screenshot
Before Adding DataTable

After Adding DataTable
