Add " iTextSharp " dll in your project...
then add
then add
using iTextSharp.text;
using iTextSharp.text.pdf;
Namespace
Then Give The Query Same As Given Below...
public partial
class VideoPlayer
: System.Web.UI.Page
{
protected void Page_Load(object
sender, EventArgs e)
{
DataTable
dt = new DataTable();
dt.Columns.Add("Name",
typeof(string));
dt.Columns.Add("Qualification",
typeof(string));
dt.Rows.Add("Raja",
"M.B.A");
dt.Rows.Add("Durai",
"M.B.B.S");
dt.Rows.Add("SRD",
"BE");
GeneratePDF(dt);
}
private void GeneratePDF(DataTable
dataTable)
{
Document
pdfDoc = new Document(PageSize.A4, 30, 30, 40, 25);
System.IO.MemoryStream mStream = new
System.IO.MemoryStream();
PdfWriter
writer = PdfWriter.GetInstance(pdfDoc,
mStream);
int
cols = dataTable.Columns.Count;
int
rows = dataTable.Rows.Count;
pdfDoc.Open();
iTextSharp.text.Table pdfTable = new
iTextSharp.text.Table(cols, rows);
pdfTable.BorderWidth = 1;
pdfTable.Padding = 1;
pdfTable.Spacing = 1;
//creating
table headers
for (int i = 0; i < cols; i++)
{
Cell
cellCols = new Cell();
Font
ColFont = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.BOLD);
Chunk
chunkCols = new Chunk(dataTable.Columns[i].ColumnName,
ColFont);
cellCols.Add(chunkCols);
pdfTable.AddCell(cellCols);
}
//creating
table data (actual result)
for (int k = 0; k < rows; k++)
{
for
(int j = 0; j < cols; j++)
{
Cell
cellRows = new Cell();
Font
RowFont = FontFactory.GetFont(FontFactory.HELVETICA, 12);
Chunk
chunkRows = new Chunk(dataTable.Rows[k][j].ToString(),
RowFont);
cellRows.Add(chunkRows);
pdfTable.AddCell(cellRows);
}
}
pdfDoc.Add(pdfTable);
pdfDoc.Close();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=Report.pdf");
Response.Clear();
Response.BinaryWrite(mStream.ToArray());
Response.End();
}
}
No comments:
Post a Comment